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

354 lines
11 KiB
Svelte
Raw Normal View History

<script lang="ts">
2025-11-01 15:49:29 +01:00
import { untrack } from 'svelte';
import { goto } from '$app/navigation';
2025-11-01 15:49:29 +01:00
import ZapIcon from '@lucide/svelte/icons/zap';
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';
import { t } from '$lib/utils/functions/translations';
import { supabase } from '$lib/utils/functions/supabase';
import { snackbar } from '$lib/components/Snackbar/store';
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
import { updateOrgNameValidation } from '$lib/utils/functions/validator';
import { currentOrg, currentOrgPath, isFreePlan } from '$lib/utils/store/org';
import { injectCustomTheme, setCustomTheme, setTheme } from '$lib/utils/functions/theme';
2024-10-06 01:20:32 +01:00
import { Row, Grid, Column } from './Layout';
import SectionTitle from '../SectionTitle.svelte';
2024-10-06 01:20:32 +01:00
import TextField from '$lib/components/Form/TextField.svelte';
import UploadImage from '$lib/components/UploadImage/index.svelte';
2024-10-06 01:20:32 +01:00
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import UnsavedChanges from '$lib/components/UnsavedChanges/index.svelte';
2023-05-02 23:55:26 +00:00
2025-11-01 15:49:29 +01:00
let avatar = $state<string | undefined>();
let hasUnsavedChanges = $state(false);
type Error = {
orgName: string;
};
2025-11-01 15:49:29 +01:00
let errors: Error = $state({
orgName: ''
2025-11-01 15:49:29 +01:00
});
let loading = $state(false);
let hex = $state('');
$effect(() => {
const theme = $currentOrg.theme;
// if not a custom theme, don't update hex
if (!theme || theme.includes('theme-')) return;
// only update hex with value of `theme` when theme changes not when HEX changes
untrack(() => {
hex = theme;
});
});
2024-10-06 01:20:32 +01:00
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) => {
2025-11-01 15:49:29 +01:00
const { error, data } = await supabase.from('organization').update({ theme }).match({ id: $currentOrg.id });
2024-10-07 02:41:25 +01:00
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(' ')
2025-11-01 15:49:29 +01:00
.concat(t ? ' ' : '', t);
2023-08-20 19:08:48 +00:00
$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
updates.avatarUrl = response.publicUrl;
$currentOrg.avatarUrl = response.publicUrl;
2023-05-02 23:55:26 +00:00
}
avatar = undefined;
}
2025-11-01 15:49:29 +01:00
let { error } = await supabase.from('organization').update(updates).match({ id: $currentOrg.id });
2023-05-02 23:55:26 +00:00
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');
hasUnsavedChanges = false;
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
2025-11-01 15:49:29 +01:00
let isCustomTheme = $derived(hex && !hex.includes('theme-'));
2023-05-02 23:55:26 +00:00
</script>
<UnsavedChanges bind:hasUnsavedChanges />
2025-11-01 15:49:29 +01:00
<Grid class="mt-5 w-full">
<Row class="border-bottom-c flex flex-col py-7 lg:flex-row">
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 flex flex-col items-center lg:mt-0 lg:items-start">
<TextField
2024-04-19 05:45:48 +01:00
label={$t('settings.organization.organization_profile.organization_name')}
bind:value={$currentOrg.name}
onChange={() => (hasUnsavedChanges = true)}
className="w-full lg:w-60 mb-5"
errorMessage={errors.orgName}
/>
2023-05-02 23:55:26 +00:00
<UploadImage
bind:avatar
src={$currentOrg.avatarUrl}
2023-05-02 23:55:26 +00:00
shape="rounded-md"
widthHeight="w-24 h-24"
2025-11-01 15:49:29 +01:00
change={() => (hasUnsavedChanges = true)}
2023-05-02 23:55:26 +00:00
/>
2025-11-01 15:49:29 +01:00
2023-05-02 23:55:26 +00:00
<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>
<Row class="border-bottom-c relative flex flex-col py-7 lg:flex-row">
2024-04-19 05:45:48 +01:00
<Column sm={4} md={4} lg={4}
2025-11-01 15:49:29 +01:00
><SectionTitle>{$t('settings.organization.organization_profile.theme.heading')}</SectionTitle></Column
2024-04-19 05:45:48 +01:00
>
<Column sm={8} md={8} lg={8}>
2025-11-01 15:49:29 +01:00
<h4 class="lg:mt-0 dark:text-white">
2024-04-19 05:45:48 +01:00
{$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 &&
'border-[#1d4ee2]'} flex h-fit items-center justify-center"
2025-11-01 15:49:29 +01:00
onclick={handleChangeTheme(themes.default)}
aria-label="Default blue theme"
2023-08-20 19:08:48 +00:00
>
2025-11-01 15:49:29 +01:00
<div class="m-1 h-6 w-6 rounded-full bg-[#1d4ee2] md:h-6 md:w-6"></div>
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.rose &&
'border-[#be1241]'} flex h-fit items-center justify-center"
2025-11-01 15:49:29 +01:00
onclick={handleChangeTheme(themes.rose)}
aria-label="Rose theme"
2023-08-20 19:08:48 +00:00
>
2025-11-01 15:49:29 +01:00
<div class="m-1 h-6 w-6 rounded-full bg-[#be1241] md:h-6 md:w-6"></div>
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.green &&
'border-[#0c891b]'} flex h-fit items-center justify-center"
2025-11-01 15:49:29 +01:00
onclick={handleChangeTheme(themes.green)}
aria-label="Green theme"
2023-08-20 19:08:48 +00:00
>
2025-11-01 15:49:29 +01:00
<div class="m-1 h-6 w-6 rounded-full bg-[#0c891b] md:h-6 md:w-6"></div>
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.orange &&
'border-[#cc4902]'} flex h-fit items-center justify-center"
2025-11-01 15:49:29 +01:00
onclick={handleChangeTheme(themes.orange)}
aria-label="Orange theme"
2023-08-20 19:08:48 +00:00
>
2025-11-01 15:49:29 +01:00
<div class="m-1 h-6 w-6 rounded-full bg-[#cc4902] md:h-6 md:w-6"></div>
2023-08-20 19:08:48 +00:00
</button>
<button
class="rounded-full border-2 {$currentOrg.theme === themes.violet &&
'border-[#cf00ce]'} flex h-fit items-center justify-center"
2025-11-01 15:49:29 +01:00
onclick={handleChangeTheme(themes.violet)}
aria-label="Violet theme"
2023-08-20 19:08:48 +00:00
>
2025-11-01 15:49:29 +01:00
<div class="m-1 h-6 w-6 rounded-full bg-[#cf00ce] md:h-6 md:w-6"></div>
2023-08-20 19:08:48 +00:00
</button>
2024-10-06 01:20:32 +01:00
<div
class="h-auto w-fit border-2 {isCustomTheme
? 'border-primary-700'
: 'dark:border-neutral-700'} group relative rounded-full"
2024-10-06 01:20:32 +01:00
>
<!-- plus icon positioned over the color picker -->
<div
class="pointer-events-none absolute inset-0 flex items-center justify-center transition-opacity duration-200"
2024-10-06 01:20:32 +01:00
>
<svg
2025-11-01 15:49:29 +01:00
class="h-6 w-6 text-{isCustomTheme ? 'white' : 'black'} z-10 opacity-100 dark:text-white"
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="border-bottom-c flex flex-col py-7 lg:flex-row">
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}>
2025-11-01 15:49:29 +01:00
<h4 class="lg:mt-0 dark:text-white">
{$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="border-bottom-c flex flex-col py-7 lg:flex-row">
2024-04-19 05:45:48 +01:00
<Column sm={4} md={4} lg={4}
2025-11-01 15:49:29 +01:00
><SectionTitle>{$t('settings.organization.organization_profile.custom_domain.heading')}</SectionTitle></Column
2024-04-19 05:45:48 +01:00
>
<Column sm={8} md={8} lg={8}>
2025-11-01 15:49:29 +01:00
<h4 class="lg:mt-0 dark:text-white">
2024-04-19 05:45:48 +01:00
{$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}
2025-11-01 15:49:29 +01:00
<ZapIcon size={16} class="filled" />
2024-09-04 06:32:54 +01:00
{/if}
{$t('settings.organization.organization_profile.custom_domain.button')}
</PrimaryButton>
2023-05-02 23:55:26 +00:00
</Column>
</Row>
<Row class="border-bottom-c flex flex-col py-7 lg:flex-row">
2024-04-19 05:45:48 +01:00
<Column sm={4} md={4} lg={4}
2025-11-01 15:49:29 +01:00
><SectionTitle>{$t('settings.organization.organization_profile.team.heading')}</SectionTitle></Column
2024-04-19 05:45:48 +01:00
>
<Column sm={8} md={8} lg={8}>
2025-11-01 15:49:29 +01:00
<h4 class="lg:mt-0 dark:text-white">
2024-04-19 05:45:48 +01:00
{$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}
2025-11-01 15:49:29 +01:00
<ZapIcon size={16} class="filled" />
2024-04-07 06:08:34 +01:00
{/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>