Files
classroomio/src/routes/profile/[id].svelte
T

240 lines
6.6 KiB
Svelte
Raw Normal View History

2021-08-18 19:47:02 +03:00
<script context="module">
export function preload({ params }) {
return { profileId: params.id };
}
</script>
<script>
import UserAvatar16 from 'carbon-icons-svelte/lib/UserAvatar16';
import Book16 from 'carbon-icons-svelte/lib/Book16';
import TextField from '../../components/Form/TextField.svelte';
import PrimaryButton from '../../components/PrimaryButton/index.svelte';
2021-08-26 13:35:56 +03:00
import UploadImage from '../../components/UploadImage/index.svelte';
2021-08-18 19:47:02 +03:00
import { supabase } from '../../utils/functions/supabase';
import { user, profile } from '../../utils/store/user';
export let profileId;
2021-08-26 13:35:56 +03:00
let avatar;
2021-08-18 19:47:02 +03:00
let loading = false;
let currentProfile = {};
let isOwner = false;
2021-08-26 13:35:56 +03:00
let initialValueOfUserName, initialValueOfFullName, initialValueOfAvatar;
2021-08-18 19:47:02 +03:00
async function getProfile(profileId) {
loading = true;
// Get user profile
// Check if user has profile
let {
data: profileData,
error,
status,
} = await supabase.from('profile').select(`*`).eq('id', profileId).single();
if (error && !profileData && status === 406 && $user.currentSession) {
// Not found
} else if (profileData) {
// Profile exists, go to profile page
currentProfile = profileData;
initialValueOfUserName = currentProfile.username;
initialValueOfFullName = currentProfile.fullname;
}
loading = false;
}
async function updateProfile() {
try {
loading = true;
const updates = {
fullname: currentProfile.fullname,
username: currentProfile.username,
};
2021-08-26 13:35:56 +03:00
if (avatar) {
const filename = `user/${currentProfile.username + Date.now()}.webp`;
const { data } = await supabase.storage
.from('avatars')
.upload(filename, avatar, {
cacheControl: '3600',
upsert: false,
});
if (data && data.Key) {
const { publicURL } = supabase.storage
.from('avatars')
.getPublicUrl(filename);
updates.avatar_url = publicURL;
2021-09-11 22:15:15 +03:00
currentProfile.avatar_url = publicURL;
2021-08-26 13:35:56 +03:00
}
avatar = undefined;
}
2021-08-18 19:47:02 +03:00
let { error } = await supabase
.from('profile')
.update(updates, {
returning: 'minimal', // Don't return the value after inserting
})
.match({ id: profileId });
2021-08-26 13:35:56 +03:00
if (isOwner) {
profile.update((_profile) => ({
..._profile,
...updates,
}));
}
2021-08-18 19:47:02 +03:00
if (error) throw error;
} catch (error) {
alert(error.message);
loading = false;
} finally {
loading = false;
initialValueOfUserName = currentProfile.username;
initialValueOfFullName = currentProfile.fullname;
}
}
2021-09-11 22:15:15 +03:00
$: isDirty =
avatar ||
initialValueOfUserName !== currentProfile.username ||
initialValueOfFullName !== currentProfile.fullname;
2021-08-18 19:47:02 +03:00
$: isOwner = $profile.id === profileId;
2021-08-26 13:35:56 +03:00
$: getProfile(profileId);
2021-09-11 22:15:15 +03:00
$: if (avatar) {
updateProfile();
}
2021-08-18 19:47:02 +03:00
</script>
<svelte:head>
<title>Profile of {currentProfile.username}</title>
</svelte:head>
2021-09-17 06:33:49 +03:00
<section
class="root w-11/12 mt-3 m-auto flex items-start justify-between flex-col lg:flex-row"
>
2021-08-18 19:47:02 +03:00
{#if currentProfile.id}
2021-09-17 06:33:49 +03:00
<div class="w-full lg:w-1/4 flex items-center flex-col">
2021-08-26 13:35:56 +03:00
<!-- <img
2021-08-18 19:47:02 +03:00
alt={currentProfile.username}
src={currentProfile.avatar_url}
height="260"
width="260"
class="rounded-full bg-light-blue-100 mb-4"
2021-08-26 13:35:56 +03:00
/> -->
<UploadImage
bind:avatar
src={currentProfile.avatar_url}
widthHeight="w-60 h-60"
2021-08-18 19:47:02 +03:00
/>
{#if isOwner}
<TextField
label="Full name"
bind:value={currentProfile.fullname}
className="w-60 mb-4"
inputClassName="rounded-md"
/>
<TextField
label="Username"
bind:value={currentProfile.username}
className="w-60 mb-4"
inputClassName="rounded-md"
/>
2021-08-28 22:44:06 +03:00
<TextField
label="Email"
bind:value={currentProfile.email}
className="w-60 mb-4"
inputClassName="rounded-md"
isDisabled={true}
/>
2021-08-18 19:47:02 +03:00
2021-09-11 22:15:15 +03:00
{#if isDirty}
2021-08-18 19:47:02 +03:00
<PrimaryButton
label={loading ? 'Updating...' : 'Update profile'}
onClick={updateProfile}
isDisabled={loading}
/>
{/if}
{:else}
<h3 class="text-xl">{currentProfile.username}</h3>
{/if}
</div>
<div class="flex-grow" />
<div class="flex-grow flex flex-wrap mx-3 w-3/5">
<div class="mb-3 w-full">
<h3 class="font-bold text-md m-0">Organizations</h3>
<p class="text-gray-500 text-sm">
This are the organizations you work at
</p>
2021-08-24 18:20:07 +03:00
<div class="hidden my-3 py-2 px-4 border shadow-sm rounded-md">
2021-08-18 19:47:02 +03:00
<h4 class="m-0 font-normal">
<a class="text-blue-700 font-bold" href="/courses"> CitiBim </a>
</h4>
<p class="text-gray-500">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Non aliquam
tenetur magnam. Alias cumque quaerat expedita dolores placeat
laboriosam culpa, suscipit veritatis adipisci eius magni obcaecati.
Sequi doloremque blanditiis earum.
</p>
<div class="mt-2 flex items-center">
<span class="flex mr-2 items-center">
<UserAvatar16 class="mr-2" /> 5
</span>
<span class="flex mr-2 items-center">
<Book16 class="mr-2" /> 5
</span>
</div>
</div>
2021-08-24 18:20:07 +03:00
<div class="hidden my-2 py-2 px-4 border shadow-sm rounded-md">
2021-08-18 19:47:02 +03:00
<h4 class="m-0 font-normal">
<a class="text-blue-700 font-bold" href="/courses">
Climate Change Group
</a>
</h4>
<p class="text-gray-500">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo quasi
autem quo possimus illum consequuntur repudiandae nobis error, porro
</p>
<div class="mt-2 flex items-center">
<span class="flex mr-2 items-center">
<UserAvatar16 class="mr-2" /> 30
</span>
<span class="flex mr-2 items-center">
<Book16 class="mr-2" /> 3
</span>
</div>
</div>
</div>
</div>
<!-- {:else if loading}
<Box>
<Chasing size="60" color="#ff3e00" unit="px" duration="1s" />
</Box>
{:else}
<Box>
<div>
<Search32 />
</div>
<h3 class="text-xxl">Profile not found</h3>
</Box> -->
{/if}
</section>
<style>
.root {
max-width: 1204px;
}
</style>