2023-05-02 23:55:26 +00:00
|
|
|
<script>
|
|
|
|
|
import { Grid, Row, Column } from 'carbon-components-svelte';
|
2023-08-02 19:35:27 +01:00
|
|
|
import TextField from '$lib/components/Form/TextField.svelte';
|
2023-05-05 20:19:45 +00:00
|
|
|
import SectionTitle from '../SectionTitle.svelte';
|
2023-08-02 19:35:27 +01:00
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
|
|
|
|
import UploadImage from '$lib/components/UploadImage/index.svelte';
|
|
|
|
|
import { supabase } from '$lib/utils/functions/supabase';
|
|
|
|
|
import { profile } from '$lib/utils/store/user';
|
2023-09-27 05:35:06 +01:00
|
|
|
import { snackbar } from '$lib/components/Snackbar/store';
|
2023-10-30 06:04:53 +00:00
|
|
|
import LogoutButton from '$lib/components/Buttons/Logout/index.svelte';
|
2023-11-16 07:34:50 +00:00
|
|
|
import generateUUID from '$lib/utils/functions/generateUUID';
|
2023-05-02 23:55:26 +00:00
|
|
|
|
2023-10-30 06:04:53 +00:00
|
|
|
let avatar = '';
|
2023-05-02 23:55:26 +00:00
|
|
|
let loading = false;
|
|
|
|
|
|
|
|
|
|
async function handleUpdate() {
|
|
|
|
|
try {
|
|
|
|
|
loading = true;
|
|
|
|
|
|
|
|
|
|
const updates = {
|
|
|
|
|
fullname: $profile.fullname,
|
2023-10-21 07:44:57 +00:00
|
|
|
username: $profile.username,
|
|
|
|
|
email: $profile.email
|
2023-05-02 23:55:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (avatar) {
|
2023-11-16 07:34:50 +00:00
|
|
|
const filename = `user/${generateUUID()}.webp`;
|
2023-05-02 23:55:26 +00:00
|
|
|
|
2023-08-05 04:44:32 +01:00
|
|
|
const { data } = await supabase.storage.from('avatars').upload(filename, avatar, {
|
|
|
|
|
cacheControl: '3600',
|
|
|
|
|
upsert: false
|
|
|
|
|
});
|
2023-08-21 15:44:15 +00:00
|
|
|
if (data) {
|
|
|
|
|
const { data: response } = await supabase.storage.from('avatars').getPublicUrl(filename);
|
2023-05-02 23:55:26 +00:00
|
|
|
|
2023-08-21 15:44:15 +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
|
|
|
}));
|
2023-09-27 05:35:06 +01:00
|
|
|
snackbar.success('Update successful');
|
2023-05-02 23:55:26 +00:00
|
|
|
|
|
|
|
|
if (error) throw error;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
let message = error.message;
|
|
|
|
|
if (message.includes('profile_username_key')) {
|
|
|
|
|
message = 'username already exists';
|
|
|
|
|
}
|
2023-09-27 05:35:06 +01:00
|
|
|
snackbar.success(`Update failed: ${message}`);
|
2023-05-02 23:55:26 +00:00
|
|
|
loading = false;
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2023-05-11 15:15:18 +00:00
|
|
|
<Grid class="border-c rounded border-gray-200 w-full mt-5">
|
2023-09-08 09:17:05 +00:00
|
|
|
<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">
|
2023-05-05 20:19:45 +00:00
|
|
|
<SectionTitle>Profile Picture</SectionTitle>
|
|
|
|
|
</Column>
|
2023-09-08 09:17:05 +00:00
|
|
|
<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>
|
2023-09-08 09:17:05 +00:00
|
|
|
<Row class="flex flex-col lg:flex-row py-7 border-bottom-c">
|
|
|
|
|
<Column sm={4} md={4} lg={4}>
|
2023-05-05 20:19:45 +00:00
|
|
|
<SectionTitle>Personal Information</SectionTitle>
|
|
|
|
|
</Column>
|
2023-09-08 09:17:05 +00:00
|
|
|
<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>
|
2023-09-08 09:17:05 +00:00
|
|
|
<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-10-30 06:04:53 +00:00
|
|
|
<LogoutButton />
|
2023-05-02 23:55:26 +00:00
|
|
|
</Row>
|
|
|
|
|
</Grid>
|