2023-12-11 21:33:20 +03:00
|
|
|
<script lang="ts">
|
2023-09-01 15:28:47 +01:00
|
|
|
import { Grid, Row, Column, Select, SelectItem } from 'carbon-components-svelte';
|
2023-05-02 23:55:26 +00:00
|
|
|
import { Moon } from 'svelte-loading-spinners';
|
2023-08-02 19:35:27 +01:00
|
|
|
import TextField from '$lib/components/Form/TextField.svelte';
|
|
|
|
|
import TextChip from '$lib/components/Chip/Text.svelte';
|
|
|
|
|
import ComingSoon from '$lib/components/ComingSoon/index.svelte';
|
|
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
|
|
|
|
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
|
|
|
|
|
import { validateEmailInString } from '$lib/utils/functions/validator';
|
|
|
|
|
import { ROLE_LABEL, ROLE } from '$lib/utils/constants/roles';
|
|
|
|
|
import { currentOrg, orgTeam } from '$lib/utils/store/org';
|
2023-09-27 05:35:06 +01:00
|
|
|
import { snackbar } from '$lib/components/Snackbar/store';
|
2023-08-02 19:35:27 +01:00
|
|
|
import { getOrgTeam } from '$lib/utils/services/org';
|
|
|
|
|
import { profile } from '$lib/utils/store/user';
|
|
|
|
|
import { supabase } from '$lib/utils/functions/supabase';
|
2023-05-05 20:19:45 +00:00
|
|
|
import SectionTitle from '../SectionTitle.svelte';
|
2023-10-07 06:08:08 +01:00
|
|
|
import {
|
|
|
|
|
triggerSendEmail,
|
|
|
|
|
NOTIFICATION_NAME
|
|
|
|
|
} from '$lib/utils/services/notification/notification';
|
2023-12-11 21:33:20 +03:00
|
|
|
import type { OrgTeamMember } from '$lib/utils/types/org';
|
2023-05-02 23:55:26 +00:00
|
|
|
|
|
|
|
|
let emailsStr = '';
|
|
|
|
|
let errorMessage = '';
|
2023-09-17 03:08:43 +01:00
|
|
|
let role = ROLE.TUTOR;
|
2023-05-02 23:55:26 +00:00
|
|
|
let isFetching = false;
|
|
|
|
|
let isLoading = false;
|
|
|
|
|
let isRemoving = false;
|
|
|
|
|
|
|
|
|
|
async function onSendInvite() {
|
2023-09-01 15:28:47 +01:00
|
|
|
const { hasError, error: _error, emails } = validateEmailInString(emailsStr);
|
2023-05-02 23:55:26 +00:00
|
|
|
|
|
|
|
|
if (hasError) {
|
|
|
|
|
errorMessage = _error;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isLoading = true;
|
|
|
|
|
let apiError = '';
|
2023-12-11 21:33:20 +03:00
|
|
|
emails.forEach(async (email: string, index: number) => {
|
2023-05-02 23:55:26 +00:00
|
|
|
if (apiError) return;
|
2023-12-11 08:17:41 +05:30
|
|
|
|
2023-12-11 21:33:20 +03:00
|
|
|
const doesEmailExist = $orgTeam.some(
|
|
|
|
|
(teamMember) => teamMember.email.toLowerCase() === email.toLowerCase()
|
|
|
|
|
);
|
2023-12-11 08:17:41 +05:30
|
|
|
|
|
|
|
|
if (doesEmailExist) {
|
2023-12-11 21:33:20 +03:00
|
|
|
snackbar.error('User Already Exists');
|
2023-12-11 08:17:41 +05:30
|
|
|
isLoading = false;
|
|
|
|
|
return;
|
2023-12-11 21:33:20 +03:00
|
|
|
}
|
|
|
|
|
const { data, error } = await supabase
|
2023-08-02 19:35:27 +01:00
|
|
|
.from('organizationmember')
|
|
|
|
|
.insert({
|
|
|
|
|
organization_id: $currentOrg.id,
|
|
|
|
|
email,
|
|
|
|
|
role_id: role,
|
2023-09-01 15:28:47 +01:00
|
|
|
verified: false
|
2023-08-02 19:35:27 +01:00
|
|
|
})
|
|
|
|
|
.select();
|
2023-12-11 21:33:20 +03:00
|
|
|
console.log('data', data);
|
2023-05-02 23:55:26 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
2023-12-11 21:33:20 +03:00
|
|
|
apiError = `${error}`;
|
2023-09-01 15:28:47 +01:00
|
|
|
|
2023-05-02 23:55:26 +00:00
|
|
|
console.error('onSendInvite:', error);
|
2023-09-27 05:35:06 +01:00
|
|
|
snackbar.error(`Failed to send invite, please try again`);
|
2023-05-02 23:55:26 +00:00
|
|
|
isLoading = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const [newMember] = data || [];
|
|
|
|
|
if (newMember) {
|
|
|
|
|
orgTeam.update((team) => [
|
|
|
|
|
{
|
|
|
|
|
id: newMember.id,
|
|
|
|
|
email: newMember?.email,
|
2023-12-11 21:33:20 +03:00
|
|
|
fullname: newMember?.fullname || '',
|
2023-05-02 23:55:26 +00:00
|
|
|
verified: newMember?.verified,
|
|
|
|
|
role: ROLE_LABEL[newMember?.role_id] || '',
|
2023-09-01 15:28:47 +01:00
|
|
|
isAdmin: newMember?.role_id === ROLE.ADMIN
|
2023-05-02 23:55:26 +00:00
|
|
|
},
|
2023-09-01 15:28:47 +01:00
|
|
|
...team
|
2023-05-02 23:55:26 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-07 06:08:08 +01:00
|
|
|
triggerSendEmail(NOTIFICATION_NAME.INVITE_TEACHER, {
|
|
|
|
|
email: newMember.email,
|
|
|
|
|
org: {
|
|
|
|
|
id: $currentOrg.id,
|
|
|
|
|
name: $currentOrg.name,
|
|
|
|
|
siteName: $currentOrg.siteName
|
|
|
|
|
}
|
2023-09-01 15:28:47 +01:00
|
|
|
});
|
|
|
|
|
|
2023-05-02 23:55:26 +00:00
|
|
|
const isLast = index === emails.length - 1;
|
|
|
|
|
if (isLast) {
|
2023-12-10 10:35:06 +00:00
|
|
|
snackbar.success('Invite Sent!');
|
2023-05-02 23:55:26 +00:00
|
|
|
|
|
|
|
|
emailsStr = '';
|
|
|
|
|
isLoading = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 21:33:20 +03:00
|
|
|
async function onRemove(id: number) {
|
2023-09-01 15:28:47 +01:00
|
|
|
console.log('onRemove called');
|
2023-05-02 23:55:26 +00:00
|
|
|
isRemoving = true;
|
2023-09-01 15:28:47 +01:00
|
|
|
const { data, error } = await supabase.from('organizationmember').delete().match({ id });
|
2023-05-02 23:55:26 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error('onRemove:', error);
|
2023-09-27 05:35:06 +01:00
|
|
|
snackbar.error('Failed to remove user, please try again');
|
2023-09-01 15:28:47 +01:00
|
|
|
} else {
|
|
|
|
|
orgTeam.update((team) => [...team.filter((member) => member.id !== id)]);
|
2023-05-02 23:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isRemoving = false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 21:33:20 +03:00
|
|
|
const fetchTeam = async (id: string) => {
|
2023-05-02 23:55:26 +00:00
|
|
|
if (!id) return;
|
|
|
|
|
|
|
|
|
|
isFetching = true;
|
|
|
|
|
await getOrgTeam(id);
|
|
|
|
|
isFetching = false;
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-11 21:33:20 +03:00
|
|
|
const isTeamMemberAdmin = (members: OrgTeamMember[], profileId: string | undefined) => {
|
2023-09-01 15:28:47 +01:00
|
|
|
return members.some((member) => member.profileId === profileId && member.isAdmin);
|
2023-05-02 23:55:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$: fetchTeam($currentOrg.id);
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<Grid class="border rounded border-gray-200 w-full mt-5">
|
2023-05-11 15:15:18 +00:00
|
|
|
<Row class="py-7 border-bottom-c">
|
2023-09-01 15:28:47 +01:00
|
|
|
<Column sm={2} md={2} lg={4} class="text-lg"><SectionTitle>Add</SectionTitle></Column>
|
2023-05-02 23:55:26 +00:00
|
|
|
<Column sm={2} md={6} lg={8}>
|
2023-06-17 19:11:29 +00:00
|
|
|
<p class="text-md text-gray-500 dark:text-white mb-5">
|
2023-09-01 15:28:47 +01:00
|
|
|
Add your team mates or collaborators to your organization. Start working together
|
2023-05-02 23:55:26 +00:00
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<div class="">
|
|
|
|
|
<TextField
|
|
|
|
|
label="Invite"
|
|
|
|
|
placeholder="email comma separated"
|
|
|
|
|
bind:value={emailsStr}
|
|
|
|
|
className="mb-3"
|
|
|
|
|
{errorMessage}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Select labelText="Role" bind:selected={role} class="mb-5 w-40">
|
2023-09-17 03:08:43 +01:00
|
|
|
<!-- <SelectItem value={ROLE.ADMIN} text={ROLE_LABEL[ROLE.ADMIN]} /> -->
|
2023-05-02 23:55:26 +00:00
|
|
|
<SelectItem value={ROLE.TUTOR} text={ROLE_LABEL[ROLE.TUTOR]} />
|
|
|
|
|
</Select>
|
|
|
|
|
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
label="Send Invite"
|
|
|
|
|
onClick={onSendInvite}
|
|
|
|
|
{isLoading}
|
|
|
|
|
isDisabled={isLoading}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</Column>
|
|
|
|
|
</Row>
|
|
|
|
|
|
2023-05-11 15:15:18 +00:00
|
|
|
<Row class="py-7 border-bottom-c">
|
2023-09-01 15:28:47 +01:00
|
|
|
<Column sm={2} md={2} lg={4} class="text-lg"><SectionTitle>Members</SectionTitle></Column>
|
2023-05-02 23:55:26 +00:00
|
|
|
<Column sm={2} md={6} lg={8}>
|
|
|
|
|
{#if isFetching}
|
|
|
|
|
<Moon />
|
|
|
|
|
{:else}
|
|
|
|
|
{#each $orgTeam as teamMember}
|
|
|
|
|
<div class="flex justify-between items-center mb-5">
|
|
|
|
|
<div class="flex">
|
2023-06-17 19:11:29 +00:00
|
|
|
<p class="text-sm text-gray-500 dark:text-white mr-3">
|
2023-05-02 23:55:26 +00:00
|
|
|
{teamMember.email}
|
|
|
|
|
</p>
|
2023-09-01 15:28:47 +01:00
|
|
|
<TextChip value={teamMember.role} className="text-xs mr-3" size="sm" />
|
2023-05-02 23:55:26 +00:00
|
|
|
{#if !teamMember.verified}
|
|
|
|
|
<TextChip
|
|
|
|
|
value="Invite Sent"
|
|
|
|
|
className="text-xs bg-yellow-200 text-yellow-700"
|
|
|
|
|
size="sm"
|
|
|
|
|
/>
|
|
|
|
|
{:else if teamMember.profileId === $profile.id}
|
|
|
|
|
<ComingSoon label="You" />
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
|
|
|
|
{#if teamMember.profileId !== $profile.id && isTeamMemberAdmin($orgTeam, $profile.id)}
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
label="Remove"
|
|
|
|
|
variant={VARIANTS.TEXT_DANGER}
|
|
|
|
|
onClick={() => onRemove(teamMember.id)}
|
|
|
|
|
isLoading={isRemoving}
|
|
|
|
|
isDisabled={isRemoving}
|
|
|
|
|
/>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
|
|
|
|
{/each}
|
|
|
|
|
{/if}
|
|
|
|
|
</Column>
|
|
|
|
|
</Row>
|
|
|
|
|
</Grid>
|