Files
classroomio/src/lib/components/Org/Settings/Profile.svelte
T

104 lines
3.6 KiB
Svelte
Raw Normal View History

2023-05-02 23:55:26 +00:00
<script>
import { goto } from '$app/navigation';
2023-05-02 23:55:26 +00:00
import { Grid, Row, Column } from 'carbon-components-svelte';
import TextField from '$lib/components/Form/TextField.svelte';
import SectionTitle from '../SectionTitle.svelte';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
import UploadImage from '$lib/components/UploadImage/index.svelte';
import { supabase } from '$lib/utils/functions/supabase';
import { profile } from '$lib/utils/store/user';
import { snackbarStore } from '$lib/components/Snackbar/store';
import { SNACKBAR_SEVERITY } from '$lib/components/Snackbar/constants';
2023-05-02 23:55:26 +00:00
let avatar;
let loading = false;
async function logout() {
const { error } = await supabase.auth.signOut();
console.log('error logout', error);
goto('/login');
}
async function handleUpdate() {
try {
loading = true;
const updates = {
fullname: $profile.fullname,
2023-08-05 04:44:32 +01:00
username: $profile.username
2023-05-02 23:55:26 +00:00
};
if (avatar) {
const filename = `user/${$profile.username + Date.now()}.webp`;
2023-08-05 04:44:32 +01:00
const { data } = await supabase.storage.from('avatars').upload(filename, avatar, {
cacheControl: '3600',
upsert: false
});
if (data) {
const { data: response } = await supabase.storage.from('avatars').getPublicUrl(filename);
2023-05-02 23:55:26 +00:00
updates.avatar_url = response.publicUrl;
$profile.avatar_url = response.publicUrl;
2023-05-02 23:55:26 +00:00
}
avatar = undefined;
}
2023-08-05 04:44:32 +01:00
let { error } = await supabase.from('profile').update(updates).match({ id: $profile.id });
2023-05-02 23:55:26 +00:00
profile.update((_profile) => ({
..._profile,
2023-08-05 04:44:32 +01:00
...updates
2023-05-02 23:55:26 +00:00
}));
$snackbarStore.open = true;
$snackbarStore.message = 'Update successful';
$snackbarStore.severity = SNACKBAR_SEVERITY.SUCCESS;
if (error) throw error;
} catch (error) {
let message = error.message;
if (message.includes('profile_username_key')) {
message = 'username already exists';
}
$snackbarStore.open = true;
$snackbarStore.message = `Update failed: ${message}`;
$snackbarStore.severity = SNACKBAR_SEVERITY.ERROR;
loading = false;
} finally {
loading = false;
}
}
</script>
<Grid class="border-c rounded border-gray-200 w-full mt-5">
<Row class="flex flex-col lg:flex-row items-center lg:items-start py-7 border-bottom-c ">
<Column sm={4} md={8} lg={4} class="mt-2 md:mt-0">
<SectionTitle>Profile Picture</SectionTitle>
</Column>
<Column sm={2} md={4} lg={8} class="mt-2 lg:mt-0">
<UploadImage bind:avatar src={$profile.avatar_url} widthHeight="w-16 h-16 lg:w-24 lg:h-24" />
2023-05-02 23:55:26 +00:00
</Column>
</Row>
<Row class="flex flex-col lg:flex-row py-7 border-bottom-c">
<Column sm={4} md={4} lg={4}>
<SectionTitle>Personal Information</SectionTitle>
</Column>
<Column sm={8} md={8} lg={8} class="mt-2 lg:mt-0">
<TextField label="Full Name" bind:value={$profile.fullname} className="w-full lg:w-60 mb-4" />
<TextField label="Username" bind:value={$profile.username} className="w-full lg:w-60 mb-4" />
<TextField label="Email" bind:value={$profile.email} className="w-full lg:w-60 mb-4" />
2023-05-02 23:55:26 +00:00
</Column>
</Row>
<Row class="m-5 w-full flex items-center gap-2 lg:justify-center">
2023-05-02 23:55:26 +00:00
<PrimaryButton
label="Update Profile"
2023-08-05 04:44:32 +01:00
className="mr-5"
2023-05-02 23:55:26 +00:00
isLoading={loading}
isDisabled={loading}
onClick={handleUpdate}
/>
2023-08-20 19:08:48 +00:00
<PrimaryButton variant={VARIANTS.OUTLINED} label="Log out" onClick={logout} />
2023-05-02 23:55:26 +00:00
</Row>
</Grid>