Files
classroomio/apps/dashboard/src/routes/onboarding/+page.svelte
T

411 lines
12 KiB
Svelte
Raw Normal View History

2022-12-03 22:45:01 +01:00
<script lang="ts">
2024-09-29 04:49:28 +01:00
import { env } from '$env/dynamic/public';
import { goto } from '$app/navigation';
import * as Select from '@cio/ui/base/select';
import TextField from '$lib/components/Form/TextField.svelte';
import UserProfileIcon from '$lib/components/Icons/UserProfileIcon.svelte';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
import { profile } from '$lib/utils/store/user';
import { onboardingValidation } from '$lib/utils/functions/validator';
import { supabase } from '$lib/utils/functions/supabase';
import { blockedSubdomain } from '$lib/utils/constants/app';
import { getOrganizations } from '$lib/utils/services/org';
2023-09-11 11:47:30 +01:00
import { generateSitename } from '$lib/utils/functions/org';
2025-11-01 15:49:29 +01:00
import { triggerSendEmail, NOTIFICATION_NAME } from '$lib/utils/services/notification/notification';
2024-04-20 04:39:16 +01:00
import { handleLocaleChange, t } from '$lib/utils/functions/translations';
import { LOCALE } from '$lib/utils/types';
2025-11-01 15:49:29 +01:00
import { untrack } from 'svelte';
2022-12-03 22:45:01 +01:00
interface OnboardingField {
fullname?: string;
orgName?: string;
siteName?: string;
2022-12-03 22:45:01 +01:00
goal?: string;
source?: string;
2024-05-09 09:15:08 +01:00
locale?: LOCALE;
metadata?: {};
2022-12-03 22:45:01 +01:00
}
interface Goal {
label: string;
value: string;
}
const maxSteps = 2;
2025-11-01 15:49:29 +01:00
let fields: OnboardingField = $state({
2022-12-03 22:45:01 +01:00
fullname: '',
orgName: '',
2024-04-19 05:45:48 +01:00
siteName: '',
2024-05-09 09:15:08 +01:00
locale: LOCALE.EN
2025-11-01 15:49:29 +01:00
});
let errors: OnboardingField = $state({});
let step = $state(1);
let loading = $state(false);
let isSiteNameTouched = $state(false);
2024-04-19 05:45:48 +01:00
let dropdownItems = [
{ id: 'de' as LOCALE, text: 'German' },
{ id: 'en' as LOCALE, text: 'English' },
{ id: 'es' as LOCALE, text: 'Spanish' },
{ id: 'fr' as LOCALE, text: 'French' },
{ id: 'hi' as LOCALE, text: 'Hindi' },
{ id: 'pl' as LOCALE, text: 'Polish' },
{ id: 'pt' as LOCALE, text: 'Portuguese' },
{ id: 'ru' as LOCALE, text: 'Russian' },
{ id: 'vi' as LOCALE, text: 'Vietnamese' },
{ id: 'da' as LOCALE, text: 'Danish' }
2024-04-19 05:45:48 +01:00
];
2022-12-03 22:45:01 +01:00
2025-11-01 15:49:29 +01:00
const progress = $derived(Math.round((step / maxSteps) * 100));
2022-12-03 22:45:01 +01:00
const educatorGoals: Goal[] = [
{
2024-04-19 05:45:48 +01:00
label: $t('onboarding.sell'),
2023-08-20 19:08:48 +00:00
value: 'sell-online'
2022-12-03 22:45:01 +01:00
},
{
2024-04-19 05:45:48 +01:00
label: $t('onboarding.teach'),
2023-08-20 19:08:48 +00:00
value: 'teach-online'
2022-12-03 22:45:01 +01:00
},
{
2024-10-07 01:36:07 +01:00
label: $t('onboarding.employees'),
value: 'employees'
2022-12-03 22:45:01 +01:00
},
{
2024-10-07 01:36:07 +01:00
label: $t('onboarding.customers'),
value: 'customers'
},
{
label: $t('onboarding.expanding'),
value: 'expanding-platform'
2023-08-20 19:08:48 +00:00
}
2022-12-03 22:45:01 +01:00
];
const sources = [
{
2024-04-19 05:45:48 +01:00
label: $t('onboarding.articles'),
2023-08-20 19:08:48 +00:00
value: 'articles'
2022-12-03 22:45:01 +01:00
},
{
2024-04-19 05:45:48 +01:00
label: $t('onboarding.search'),
2023-08-20 19:08:48 +00:00
value: 'search-engine'
2022-12-03 22:45:01 +01:00
},
{
2024-04-19 05:45:48 +01:00
label: $t('onboarding.social'),
2023-08-20 19:08:48 +00:00
value: 'social-media'
2022-12-03 22:45:01 +01:00
},
{
2024-04-19 05:45:48 +01:00
label: $t('onboarding.friends'),
2023-08-20 19:08:48 +00:00
value: 'friends-family'
}
2022-12-03 22:45:01 +01:00
];
async function setMetaData() {
2024-09-29 04:49:28 +01:00
if (!env.PUBLIC_IP_REGISTRY_KEY) return;
2023-11-23 22:18:31 +00:00
2024-09-29 04:49:28 +01:00
const response = await fetch(`https://api.ipregistry.co/?key=${env.PUBLIC_IP_REGISTRY_KEY}`);
const payload = await response.json();
fields.metadata = payload;
}
2022-12-03 22:45:01 +01:00
const handleSubmit = async () => {
loading = true;
2022-12-03 22:45:01 +01:00
// Run validation
console.log(fields);
errors = onboardingValidation(fields, step);
console.log('errors', errors);
2023-11-23 22:18:31 +00:00
if (Object.keys(errors).length || !$profile.id) {
loading = false;
2022-12-03 22:45:01 +01:00
return;
}
if (step === 1) {
// Validate if domain is among our seculeded subdomains
if (blockedSubdomain.includes(fields.siteName || '')) {
errors.siteName = 'Sitename already exists.';
loading = false;
return;
}
const { data: org, error } = await supabase
.from('organization')
.insert({
name: fields.orgName,
2023-08-20 19:08:48 +00:00
siteName: fields.siteName
})
.select();
console.log('Create organisation', org);
if (error) {
console.log('Error: create organisation', error);
errors.siteName = 'Sitename already exists.';
loading = false;
return;
}
if (Array.isArray(org) && org.length) {
const orgData = org[0];
const { data, error } = await supabase
.from('organizationmember')
.insert({
organization_id: orgData.id,
profile_id: $profile.id,
2024-07-08 06:45:26 +01:00
role_id: 1,
verified: true
})
.select();
console.log('Create organisation member', data);
if (error) {
console.log('Error: create organisation member', error);
2024-04-19 05:45:48 +01:00
errors.siteName = $t('add_org.error_organization');
// Delete organization so it can be recreated.
2023-08-20 19:08:48 +00:00
await supabase.from('organization').delete().match({ siteName: fields.siteName });
loading = false;
return;
}
await getOrganizations($profile.id);
}
// client
}
2022-12-03 22:45:01 +01:00
if (step === maxSteps) {
// Submit filled
// Set extra metadata based off location
await setMetaData();
2024-04-07 22:01:36 +01:00
await supabase
2022-12-03 22:45:01 +01:00
.from('profile')
.update({
...fields,
orgName: undefined,
2023-08-20 19:08:48 +00:00
siteName: undefined
})
2022-12-03 22:45:01 +01:00
.match({ id: $profile.id });
2022-12-03 22:45:01 +01:00
loading = false;
if (fields.fullname) {
$profile.fullname = fields.fullname;
}
2024-04-20 04:39:16 +01:00
if (fields.locale) {
$profile.locale = fields.locale;
if (fields.locale !== LOCALE.EN) {
handleLocaleChange(fields.locale);
}
}
2024-03-22 06:30:24 +01:00
triggerSendEmail(NOTIFICATION_NAME.VERIFY_EMAIL, {
to: $profile.email,
profileId: $profile.id,
2024-05-09 09:15:08 +01:00
fullname: $profile.fullname,
2024-03-22 06:30:24 +01:00
orgSiteName: fields.siteName
});
const welcomePopup = `${$profile.is_email_verified}`;
return goto(`/org/${fields.siteName}?welcomePopup=${welcomePopup}`);
2022-12-03 22:45:01 +01:00
}
// Move to next step
step = step + 1;
loading = false;
2022-12-03 22:45:01 +01:00
};
2025-11-01 15:49:29 +01:00
function updateSiteName(sname?: string) {
if (!sname) return;
untrack(() => {
fields.siteName = generateSitename(sname);
});
}
$effect(() => {
updateSiteName(fields.siteName);
});
function setOrgSiteName(orgName: string | undefined, isTouched: boolean) {
if (!orgName || isTouched) return;
untrack(() => {
fields.siteName = orgName
?.toLowerCase()
?.replace(/\s+/g, '-')
?.replace(/[^a-zA-Z0-9-]/g, '');
});
}
$effect(() => {
setOrgSiteName(fields.orgName, isSiteNameTouched);
});
2022-12-03 22:45:01 +01:00
</script>
{#if $profile.id}
2025-08-08 04:53:38 +01:00
<div class="flex min-h-screen w-full justify-center">
<div class="flex w-9/12 max-w-md flex-col items-center justify-center">
<!-- Header With Logo -->
<div class="flex flex-col items-center">
2025-08-08 04:53:38 +01:00
<div class="mb-4 flex w-full items-center justify-center">
2023-08-20 19:08:48 +00:00
<img src="/logo-192.png" alt="ClassroomIO logo" height="50" width="50" data-atf="1" />
2025-08-08 04:53:38 +01:00
<h4 class="text-xl dark:text-white">ClassroomIO</h4>
2022-12-03 22:45:01 +01:00
</div>
<!-- Loggedin Email -->
<div
2025-08-08 04:53:38 +01:00
class="mb-6 flex w-64 items-center justify-center rounded-2xl border border-gray-300 bg-gray-100 py-6 dark:bg-neutral-800"
>
2025-11-01 15:49:29 +01:00
<UserProfileIcon size={16} />
2025-08-08 04:53:38 +01:00
<p class="ml-2 text-sm dark:text-white">{$profile.email}</p>
</div>
</div>
2025-08-08 04:53:38 +01:00
<div class="form-container w-full overflow-y-auto px-2">
{#if step === 1}
<!-- Name/Organization Question -->
2025-08-08 04:53:38 +01:00
<div id="role-question" class="mb-6 flex flex-col items-start">
<!-- Full name -->
<TextField
2024-04-19 05:45:48 +01:00
label={$t('onboarding.fullname')}
bind:value={fields.fullname}
2023-07-18 07:40:12 +00:00
name="fullname"
type="text"
placeholder="e.g Joke Silva"
className="mb-5 w-full"
labelClassName="text-lg font-normal"
errorMessage={errors.fullname}
2024-03-02 00:33:28 +01:00
isRequired
/>
<!-- Org name -->
<TextField
2024-04-19 05:45:48 +01:00
label={$t('onboarding.name')}
bind:value={fields.orgName}
2023-07-18 07:40:12 +00:00
name="orgname"
type="text"
placeholder="e.g Education For All"
className="mb-5 w-full"
labelClassName="text-lg font-normal"
errorMessage={errors.orgName}
2024-03-02 00:33:28 +01:00
isRequired
/>
<!-- Org Site Name -->
<TextField
2024-04-19 05:45:48 +01:00
label={$t('onboarding.organisation_sitename')}
helperMessage={`https://${fields.siteName || ''}.classroomio.com`}
bind:value={fields.siteName}
2023-07-18 07:40:12 +00:00
name="sitename"
type="text"
placeholder="e.g edforall"
className="mb-5 w-full"
labelClassName="text-lg font-normal"
errorMessage={errors.siteName}
2023-11-23 21:43:42 +00:00
onChange={() => {
isSiteNameTouched = true;
}}
2024-03-02 00:33:28 +01:00
isRequired
/>
</div>
{:else}
<!-- Goal/Source Question -->
2025-08-08 04:53:38 +01:00
<div id="goal-question" class="mb-6 flex flex-col items-center">
<div class="w-10/12">
<!-- Goal Question -->
2025-08-08 04:53:38 +01:00
<div class="mb-10 flex w-full flex-col items-start justify-between">
<label for="text-field" class="m-0 mb-3 text-lg font-normal dark:text-white">
2024-04-19 05:45:48 +01:00
{$t('onboarding.what_brings')}
2022-12-03 22:45:01 +01:00
</label>
<!-- Loop through Goals -->
{#each educatorGoals as goal}
2025-08-08 04:53:38 +01:00
<label class="mb-1 inline-flex w-full items-center font-light dark:text-white">
2025-11-01 15:49:29 +01:00
<input type="radio" bind:group={fields.goal} name="goal" value={goal.value} class="mr-2" />
{goal.label}
</label>
{/each}
<!-- Goal: Error message -->
{#if errors.goal}
<p class="text-sm text-red-500">
{errors.goal}
</p>
{/if}
</div>
2022-12-03 22:45:01 +01:00
<!-- Source Question -->
2025-08-08 04:53:38 +01:00
<div class="flex w-full flex-col items-start justify-between">
<label for="text-field" class="m-0 mb-3 text-lg font-normal dark:text-white">
2024-04-19 05:45:48 +01:00
{$t('onboarding.how')}
2022-12-03 22:45:01 +01:00
</label>
<!-- Loop through Goals -->
{#each sources as source}
2025-08-08 04:53:38 +01:00
<label class="mb-1 inline-flex w-full items-center font-light dark:text-white">
2025-11-01 15:49:29 +01:00
<input type="radio" bind:group={fields.source} name="source" value={source.value} class="mr-2" />
{source.label}
</label>
{/each}
<!-- Goal: Error message -->
{#if errors.source}
<p class="text-sm text-red-500">
{errors.source}
</p>
{/if}
</div>
2024-04-19 05:45:48 +01:00
<!-- Language Picker -->
<div class="mt-10">
<span class="dark:text-white">{$t('content.toggle_label')}: </span>
<Select.Root type="single" bind:value={fields.locale}>
<Select.Trigger class="w-full">
<p>{fields.locale}</p>
</Select.Trigger>
<Select.Content>
{#each dropdownItems as item}
<Select.Item value={item.id}>{item.text}</Select.Item>
{/each}
</Select.Content>
</Select.Root>
2024-04-19 05:45:48 +01:00
</div>
2022-12-03 22:45:01 +01:00
</div>
</div>
{/if}
</div>
<!-- Footer -->
2025-08-08 04:53:38 +01:00
<div class="mt-8 flex w-full items-center justify-between">
<div class="relative h-2 w-24 bg-gray-300">
2025-11-01 15:49:29 +01:00
<span class="progress bg-primary-700 absolute left-0 top-0 h-full" style="width: {progress}%;"></span>
</div>
<div class="flex">
{#if step > 1}
<PrimaryButton
2024-04-19 05:45:48 +01:00
label={$t('onboarding.back')}
variant={VARIANTS.NONE}
2023-08-20 19:08:48 +00:00
className="py-3 px-6 mr-2 text-primary-700"
onClick={() => (step = step - 1)}
/>
{/if}
<PrimaryButton
2024-04-19 05:45:48 +01:00
label={$t('onboarding.continue')}
variant={VARIANTS.CONTAINED}
type="button"
className="px-5 py-3"
isLoading={loading}
onClick={handleSubmit}
/>
</div>
2022-12-03 22:45:01 +01:00
</div>
</div>
</div>
{/if}
2022-12-03 22:45:01 +01:00
<style>
.form-container {
max-height: 66%;
}
</style>