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

348 lines
11 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { goto } from '$app/navigation';
2024-10-07 02:41:25 +01:00
import debounce from 'lodash/debounce';
2024-10-06 01:20:32 +01:00
import ColorPicker from 'svelte-awesome-color-picker';
2023-05-02 23:55:26 +00:00
import { Grid, Row, Column } from 'carbon-components-svelte';
2024-04-07 06:08:34 +01:00
import FlashFilled from 'carbon-icons-svelte/lib/FlashFilled.svelte';
2024-10-06 01:20:32 +01:00
import { isFreePlan } from '$lib/utils/store/org';
import { t } from '$lib/utils/functions/translations';
import { supabase } from '$lib/utils/functions/supabase';
2023-09-27 05:35:06 +01:00
import { snackbar } from '$lib/components/Snackbar/store';
import { currentOrg, currentOrgPath } from '$lib/utils/store/org';
import { updateOrgNameValidation } from '$lib/utils/functions/validator';
2024-10-06 01:20:32 +01:00
import { setTheme, setCustomTheme, injectCustomTheme } from '$lib/utils/functions/theme';
import SectionTitle from '../SectionTitle.svelte';
import TextField from '$lib/components/Form/TextField.svelte';
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
import UploadImage from '$lib/components/UploadImage/index.svelte';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
2023-05-02 23:55:26 +00:00
let avatar;
type Error = {
orgName: string;
};
let errors: Error = {
orgName: ''
};
2023-05-02 23:55:26 +00:00
let loading = false;
2024-10-06 01:20:32 +01:00
let hex = '';
2023-08-20 19:08:48 +00:00
const themes = {
rose: 'theme-rose',
green: 'theme-green',
orange: 'theme-orange',
violet: 'theme-violet',
default: ''
};
2024-10-07 02:41:25 +01:00
const saveTheme = debounce(async (theme) => {
const { error, data } = await supabase
.from('organization')
.update({ theme })
.match({ id: $currentOrg.id });
console.log('Debounced update theme', data);
if (error) {
snackbar.error('Failed to update theme: ' + error.message);
}
}, 700);
2023-08-20 19:08:48 +00:00
function handleChangeTheme(t = '') {
return async () => {
document.body.className = document.body.className
.split(' ')
.filter((c) => !c.includes('theme'))
.join(' ')
.concat(!!t ? ' ' : '', t);
$currentOrg.theme = t;
2024-10-06 01:20:32 +01:00
hex = '';
2024-08-19 19:26:41 +01:00
setTheme(t);
2024-10-07 02:41:25 +01:00
saveTheme(t);
2023-08-20 19:08:48 +00:00
};
}
2023-05-02 23:55:26 +00:00
2024-10-06 01:20:32 +01:00
async function handleCustomTheme() {
if (!hex) return;
injectCustomTheme(hex);
setCustomTheme('theme-custom');
$currentOrg.theme = hex;
2024-10-07 02:41:25 +01:00
saveTheme(hex);
2024-10-06 01:20:32 +01:00
}
2023-05-02 23:55:26 +00:00
async function handleUpdate() {
errors = updateOrgNameValidation($currentOrg.name) as Error;
if (Object.values(errors).length) {
loading = false;
return;
}
2023-05-02 23:55:26 +00:00
try {
loading = true;
2024-10-06 01:20:32 +01:00
const updates: Record<string, string> = {
2024-08-19 19:26:41 +01:00
name: $currentOrg.name
2023-05-02 23:55:26 +00:00
};
if (avatar) {
const filename = `user/${$currentOrg.name + Date.now()}.webp`;
2023-08-20 19:08:48 +00:00
const { data } = await supabase.storage.from('avatars').upload(filename, avatar, {
cacheControl: '3600',
upsert: false
});
2023-05-02 23:55:26 +00:00
2023-08-27 04:25:59 +01:00
if (data) {
const { data: response } = supabase.storage.from('avatars').getPublicUrl(filename);
2023-05-02 23:55:26 +00:00
2023-08-27 04:25:59 +01:00
updates.avatar_url = response.publicUrl;
$currentOrg.avatar_url = response.publicUrl;
2023-05-02 23:55:26 +00:00
}
avatar = undefined;
}
let { error } = await supabase
.from('organization')
.update(updates)
2023-05-02 23:55:26 +00:00
.match({ id: $currentOrg.id });
currentOrg.update((_currentOrg) => ({
..._currentOrg,
2023-08-20 19:08:48 +00:00
...updates
2023-05-02 23:55:26 +00:00
}));
2023-09-27 05:35:06 +01:00
2024-04-19 05:45:48 +01:00
snackbar.success('snackbar.course_settings.success.update_successful');
2023-05-02 23:55:26 +00:00
if (error) throw error;
} catch (error) {
2024-10-06 01:20:32 +01:00
let message = error as string;
2023-05-02 23:55:26 +00:00
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
}
2023-09-27 05:35:06 +01: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-10-06 01:20:32 +01:00
function gotoSettings(pathname) {
2023-05-20 23:53:28 +00:00
goto(`${$currentOrgPath}/settings${pathname}`);
2023-05-02 23:55:26 +00:00
}
2024-10-06 01:20:32 +01:00
function setHex(theme?: string) {
if (!theme || hex || theme.includes('theme-')) return;
2024-10-06 01:20:32 +01:00
hex = theme;
}
$: setHex($currentOrg.theme);
$: isCustomTheme = hex && !hex.includes('theme-');
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 lg:flex-row flex-col py-7 border-bottom-c">
2024-10-06 01:20:32 +01:00
<Column sm={4} md={4} lg={4}>
<SectionTitle>
{$t('settings.organization.organization_profile.heading')}
</SectionTitle>
</Column>
<Column sm={8} md={8} lg={8} class="mt-2 lg:mt-0 flex flex-col items-center lg:items-start">
<TextField
2024-04-19 05:45:48 +01:00
label={$t('settings.organization.organization_profile.organization_name')}
bind:value={$currentOrg.name}
className="w-full lg:w-60 mb-5"
errorMessage={errors.orgName}
/>
2023-05-02 23:55:26 +00:00
<UploadImage
bind:avatar
src={$currentOrg.avatar_url}
shape="rounded-md"
widthHeight="w-24 h-24"
/>
<PrimaryButton
2024-04-19 05:45:48 +01:00
label={$t('settings.organization.organization_profile.update_organization')}
className="px-6 py-3 lg:mr-5 mt-5"
2023-05-02 23:55:26 +00:00
isLoading={loading}
isDisabled={loading}
onClick={handleUpdate}
/>
</Column>
</Row>
2024-10-06 01:20:32 +01:00
<Row class="flex lg:flex-row flex-col py-7 border-bottom-c relative">
2024-04-19 05:45:48 +01:00
<Column sm={4} md={4} lg={4}
><SectionTitle>{$t('settings.organization.organization_profile.theme.heading')}</SectionTitle
></Column
>
<Column sm={8} md={8} lg={8}>
2024-04-19 05:45:48 +01:00
<h4 class="dark:text-white lg:mt-0">
{$t('settings.organization.organization_profile.theme.sub_heading')}
</h4>
2023-08-20 19:08:48 +00:00
2024-10-06 01:20:32 +01:00
<div class="flex items-center gap-5">
2023-08-20 19:08:48 +00:00
<button
class="rounded-full border-2 {$currentOrg.theme === themes.default &&
2024-10-06 01:20:32 +01:00
'border-[#1d4ee2]'} flex items-center justify-center h-fit"
2023-08-20 19:08:48 +00:00
on:click={handleChangeTheme(themes.default)}
>
2024-10-06 01:20:32 +01:00
<div class="w-6 h-6 md:w-6 md:h-6 bg-[#1d4ee2] rounded-full m-1" />
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.rose &&
2024-10-06 01:20:32 +01:00
'border-[#be1241]'} flex items-center justify-center h-fit"
2023-08-20 19:08:48 +00:00
on:click={handleChangeTheme(themes.rose)}
>
2024-10-06 01:20:32 +01:00
<div class="w-6 h-6 md:w-6 md:h-6 bg-[#be1241] rounded-full m-1" />
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.green &&
2024-10-06 01:20:32 +01:00
'border-[#0c891b]'} flex items-center justify-center h-fit"
2023-08-20 19:08:48 +00:00
on:click={handleChangeTheme(themes.green)}
>
2024-10-06 01:20:32 +01:00
<div class="w-6 h-6 md:w-6 md:h-6 bg-[#0c891b] rounded-full m-1" />
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.orange &&
2024-10-06 01:20:32 +01:00
'border-[#cc4902]'} flex items-center justify-center h-fit"
2023-08-20 19:08:48 +00:00
on:click={handleChangeTheme(themes.orange)}
>
2024-10-06 01:20:32 +01:00
<div class="w-6 h-6 md:w-6 md:h-6 bg-[#cc4902] rounded-full m-1" />
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.violet &&
2024-10-06 01:20:32 +01:00
'border-[#cf00ce]'} flex items-center justify-center h-fit"
2023-08-20 19:08:48 +00:00
on:click={handleChangeTheme(themes.violet)}
>
2024-10-06 01:20:32 +01:00
<div class="w-6 h-6 md:w-6 md:h-6 bg-[#cf00ce] rounded-full m-1" />
2023-08-20 19:08:48 +00:00
</button>
2024-10-06 01:20:32 +01:00
<div
class="w-fit h-auto border-2 {isCustomTheme
? 'border-primary-700'
: 'dark:border-neutral-700'} rounded-full relative group"
2024-10-06 01:20:32 +01:00
>
<!-- plus icon positioned over the color picker -->
<div
class="absolute inset-0 flex items-center justify-center pointer-events-none transition-opacity duration-200"
>
<svg
class="w-6 h-6 text-{isCustomTheme
? 'white'
: 'black'} dark:text-white z-10 opacity-100"
2024-10-06 01:20:32 +01:00
fill="none"
stroke="currentColor"
stroke-width="2"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"></path>
</svg>
</div>
<ColorPicker position="responsive" label="" bind:hex on:input={handleCustomTheme} />
2024-10-06 01:20:32 +01:00
</div>
2023-08-20 19:08:48 +00:00
</div>
2023-05-02 23:55:26 +00:00
</Column>
2023-08-20 19:08:48 +00:00
</Row>
2024-10-06 01:20:32 +01:00
<Row class="flex lg:flex-row flex-col py-7 border-bottom-c">
2024-10-06 01:20:32 +01:00
<Column sm={4} md={4} lg={4}>
<SectionTitle>
{$t('settings.organization.organization_profile.customize_lms.heading')}
</SectionTitle>
</Column>
<Column sm={8} md={8} lg={8}>
<h4 class="dark:text-white lg:mt-0">
{$t('settings.organization.organization_profile.customize_lms.sub_heading')}
</h4>
<p class="text-sm text-gray-500 dark:text-white">
{$t('settings.organization.organization_profile.customize_lms.body')}
</p>
<PrimaryButton
className="my-7 py-5 px-10 flex items-center gap-2 justify-center"
variant={VARIANTS.OUTLINED}
2024-10-06 01:20:32 +01:00
onClick={() => gotoSettings('/customize-lms')}
>
{$t('settings.organization.organization_profile.customize_lms.button')}
</PrimaryButton>
</Column>
</Row>
<Row class="flex lg:flex-row flex-col py-7 border-bottom-c">
2024-04-19 05:45:48 +01:00
<Column sm={4} md={4} lg={4}
><SectionTitle
>{$t('settings.organization.organization_profile.custom_domain.heading')}</SectionTitle
></Column
>
<Column sm={8} md={8} lg={8}>
2024-04-19 05:45:48 +01:00
<h4 class="dark:text-white lg:mt-0">
{$t('settings.organization.organization_profile.custom_domain.sub_heading')}
</h4>
2023-06-17 19:11:29 +00:00
<p class="text-sm text-gray-500 dark:text-white">
2024-04-19 05:45:48 +01:00
{$t('settings.organization.organization_profile.custom_domain.body')}
2023-05-02 23:55:26 +00:00
</p>
<PrimaryButton
2024-09-04 06:32:54 +01:00
className="my-7 py-5 px-10 flex items-center gap-2 justify-center"
2023-05-02 23:55:26 +00:00
variant={VARIANTS.OUTLINED}
2024-10-06 01:20:32 +01:00
onClick={() => gotoSettings('/domains')}
2024-09-04 06:32:54 +01:00
>
{#if $isFreePlan}
<FlashFilled size={16} class="text-blue-700" />
{/if}
{$t('settings.organization.organization_profile.custom_domain.button')}
</PrimaryButton>
2023-05-02 23:55:26 +00:00
</Column>
</Row>
<Row class="flex lg:flex-row flex-col py-7 border-bottom-c">
2024-04-19 05:45:48 +01:00
<Column sm={4} md={4} lg={4}
><SectionTitle>{$t('settings.organization.organization_profile.team.heading')}</SectionTitle
></Column
>
<Column sm={8} md={8} lg={8}>
2024-04-19 05:45:48 +01:00
<h4 class="dark:text-white lg:mt-0">
{$t('settings.organization.organization_profile.team.sub_heading')}
</h4>
2023-06-17 19:11:29 +00:00
<p class="text-sm text-gray-500 dark:text-white">
2024-04-19 05:45:48 +01:00
{$t('settings.organization.organization_profile.team.body')}
2023-05-02 23:55:26 +00:00
</p>
<PrimaryButton
2024-04-07 06:08:34 +01:00
className="my-7 py-5 px-10 flex items-center gap-2 justify-center"
2023-05-02 23:55:26 +00:00
variant={VARIANTS.OUTLINED}
2024-10-06 01:20:32 +01:00
onClick={() => gotoSettings('/teams')}
2024-04-07 06:08:34 +01:00
>
{#if $isFreePlan}
<FlashFilled size={16} class="text-blue-700" />
{/if}
2024-04-24 23:01:20 +01:00
{$t('settings.organization.organization_profile.team.button')}
2024-04-07 06:08:34 +01:00
</PrimaryButton>
2023-05-02 23:55:26 +00:00
</Column>
</Row>
</Grid>
<style>
:global(.dark) {
--cp-text-color: #fff;
--cp-border-color: white;
--cp-text-color: white;
--cp-input-color: #555;
--cp-button-hover-color: #777;
}
:global(.dark .alpha) {
background: #333 !important;
}
</style>