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

136 lines
4.4 KiB
Svelte
Raw Normal View History

2023-05-02 23:55:26 +00:00
<script>
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';
2024-04-07 06:08:34 +01:00
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';
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';
2024-04-19 05:45:48 +01:00
import { t } from '$lib/utils/functions/translations';
import LanguagePicker from './LanguagePicker.svelte';
import { handleLocaleChange } from '$lib/utils/functions/translations';
import { updateProfileEmailValidation } from '$lib/utils/functions/validator';
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;
2024-04-19 05:45:48 +01:00
let hasLangChanged = false;
let locale = '';
2023-05-02 23:55:26 +00:00
let errors = {
email: ''
}
2023-05-02 23:55:26 +00:00
async function handleUpdate() {
errors = updateProfileEmailValidation($profile.email)
if (Object.values(errors).length) {
loading = false;
return;
}
2023-05-02 23:55:26 +00:00
try {
2024-04-19 05:45:48 +01:00
console.log({ hasLangChanged });
2023-05-02 23:55:26 +00:00
loading = true;
const updates = {
fullname: $profile.fullname,
2023-10-21 07:44:57 +00:00
username: $profile.username,
2024-04-19 05:45:48 +01:00
email: $profile.email,
locale
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
});
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
}));
2024-04-19 05:45:48 +01:00
snackbar.success('snackbar.course_settings.success.update_successful');
if (hasLangChanged) {
handleLocaleChange(locale);
}
2023-05-02 23:55:26 +00:00
if (error) throw error;
} catch (error) {
let message = error.message;
if (message.includes('profile_username_key')) {
2024-04-19 05:45:48 +01:00
message = $t('snackbar.lms.error.username_exists');
2023-05-02 23:55:26 +00:00
}
2024-04-19 05:45:48 +01:00
snackbar.error(`${$t('snackbar.lms.error.update')} ${message}`);
2023-05-02 23:55:26 +00:00
loading = false;
} finally {
loading = false;
}
}
2024-04-19 05:45:48 +01:00
$: locale = !locale ? $profile.locale : locale;
2023-05-02 23:55:26 +00:00
</script>
2024-04-07 06:08:34 +01:00
<Grid class="border-c rounded border-gray-200 dark:border-neutral-600 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">
2024-04-19 05:45:48 +01:00
<SectionTitle>{$t('settings.profile.profile_picture.heading')}</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}>
2024-04-19 05:45:48 +01:00
<SectionTitle>{$t('settings.profile.personal_information.heading')}</SectionTitle>
</Column>
<Column sm={8} md={8} lg={8} class="mt-2 lg:mt-0">
2024-04-19 05:45:48 +01:00
<TextField
label={$t('settings.profile.personal_information.full_name')}
bind:value={$profile.fullname}
className="w-full lg:w-60 mb-4"
/>
<TextField
label={$t('settings.profile.personal_information.username')}
bind:value={$profile.username}
className="w-full lg:w-60 mb-4"
/>
<TextField
label={$t('settings.profile.personal_information.email')}
bind:value={$profile.email}
className="w-full lg:w-60 mb-4"
errorMessage={errors.email}
2024-04-19 05:45:48 +01:00
/>
<LanguagePicker bind:hasLangChanged bind:value={locale} className="w-full lg:w-60 mb-4" />
2023-05-02 23:55:26 +00:00
</Column>
</Row>
2024-04-19 05:45:48 +01:00
<Row class="m-5 w-full flex items-center gap-2 lg:justify-center">
2023-05-02 23:55:26 +00:00
<PrimaryButton
2024-04-19 05:45:48 +01:00
label={$t('settings.profile.update_profile')}
2024-04-07 06:08:34 +01:00
variant={VARIANTS.CONTAINED_DARK}
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>