Files
classroomio/src/routes/_layout.svelte
T

251 lines
6.5 KiB
Svelte
Raw Normal View History

2021-07-31 23:06:14 +03:00
<script context="module">
export function preload(page, { config }) {
2022-12-03 22:45:01 +01:00
return { config };
2021-07-31 23:06:14 +03:00
}
</script>
2020-12-24 20:32:05 +02:00
<script>
2021-07-31 23:06:14 +03:00
import { onMount } from 'svelte';
2021-09-11 19:27:11 +03:00
import { derived } from 'svelte/store';
2021-07-31 23:06:14 +03:00
import { stores, goto } from '@sapper/app';
import _ from 'lodash';
2021-10-22 14:55:33 +03:00
import * as Sentry from '@sentry/browser';
import { Moon } from 'svelte-loading-spinners';
2021-07-26 02:31:00 +03:00
import Tailwindcss from '../components/Tailwindcss.svelte';
import LandingNavigation from '../components/Navigation/index.svelte';
import OrgNavigation from '../components/Navigation/app.svelte';
2021-08-28 22:44:06 +03:00
import Snackbar from '../components/Snackbar/index.svelte';
2021-09-11 19:27:11 +03:00
import Backdrop from '../components/Backdrop/index.svelte';
import OrgSideBar from '../components/Org/SideBar.svelte';
// import SideBar from "../components/SideBar/index.svelte";
2021-08-18 19:47:02 +03:00
// import Footer from '../components/Footer/index.svelte';
2022-11-26 07:48:49 +01:00
import Apps from '../components/Apps/index.svelte';
2021-07-31 23:06:14 +03:00
import { handleAuthChange } from '../utils/functions/api';
import { user, profile } from '../utils/store/user';
import { getSupabase } from '../utils/functions/supabase';
2021-09-17 06:33:49 +03:00
import { isMobile } from '../utils/store/useMobile';
2022-12-03 22:45:01 +01:00
import { ROUTES_TO_HIDE_NAV, ROUTE } from '../utils/constants/routes';
import { getOrganizations } from '../utils/services/org';
2020-12-24 20:32:05 +02:00
2021-04-03 16:43:02 +03:00
export let segment;
2021-07-31 23:06:14 +03:00
export let config;
2021-09-11 19:27:11 +03:00
let { page, preloading } = stores();
2021-07-31 23:06:14 +03:00
let supabase = getSupabase(config);
let path = $page.path.replace('/', '');
2021-09-11 19:27:11 +03:00
const delayedPreloading = derived(preloading, (currentPreloading, set) => {
setTimeout(() => set(currentPreloading), 250);
});
2021-07-31 23:06:14 +03:00
async function getProfile() {
// Get user profile
const authUser = supabase.auth.user();
2022-11-30 09:18:41 +01:00
console.log('Get user profile', authUser);
2021-07-31 23:06:14 +03:00
// Check if user has profile
let {
data: profileData,
error,
status,
} = await supabase
.from('profile')
.select(`*`)
2021-08-18 19:47:02 +03:00
.eq('id', authUser.id)
2021-07-31 23:06:14 +03:00
.single();
if (error && !profileData && status === 406) {
// User wasn't found, create profile
2021-08-18 19:47:02 +03:00
console.log(`User wasn't found, create profile`);
2021-07-31 23:06:14 +03:00
2021-08-18 19:47:02 +03:00
const [regexUsernameMatch] = [...authUser.email.matchAll(/(.*)@/g)];
const { data, error } = await supabase.from('profile').insert({
id: authUser.id,
2022-12-03 22:45:01 +01:00
username: regexUsernameMatch[1] + '1669991321770',
2021-08-18 19:47:02 +03:00
fullname: regexUsernameMatch[1],
2021-08-24 10:45:43 +03:00
email: authUser.email,
2021-08-18 19:47:02 +03:00
});
2021-07-31 23:06:14 +03:00
// Profile created, go to profile page
if (!error && data) {
2021-08-18 19:47:02 +03:00
$user.fetchingUser = false;
$user.isLoggedIn = true;
$user.currentSession = authUser;
profile.set(data[0]);
2021-10-22 14:55:33 +03:00
// Set user in sentry
Sentry.setUser($profile);
2022-12-03 22:45:01 +01:00
goto(ROUTE.ONBOARDING);
2021-07-31 23:06:14 +03:00
}
2022-11-30 09:18:41 +01:00
$user.fetchingUser = false;
2021-07-31 23:06:14 +03:00
} else if (profileData) {
// Profile exists, go to profile page
2021-08-18 19:47:02 +03:00
$user.fetchingUser = false;
$user.isLoggedIn = true;
$user.currentSession = authUser;
2021-07-31 23:06:14 +03:00
profile.set(profileData);
2021-08-18 19:47:02 +03:00
2021-10-22 14:55:33 +03:00
// Set user in sentry
Sentry.setUser($profile);
const orgRes = await getOrganizations($profile.id);
2021-08-18 19:47:02 +03:00
// If user coming to login page, then
if (_.isEmpty(orgRes.orgs)) {
2022-12-03 22:45:01 +01:00
goto(ROUTE.ONBOARDING);
2023-04-22 20:00:37 +00:00
} else if (
['login', 'signup', 'onboarding'].includes(path) ||
path === '' // for home page
) {
// By default redirect to first organization
goto(`/org/${orgRes.currentOrg.siteName}`);
2021-08-18 19:47:02 +03:00
}
2021-07-31 23:06:14 +03:00
}
}
2021-08-26 13:35:56 +03:00
function isCoursePage(path) {
return /course[s]*\/[a-z 0-9 -]/.test(path);
2021-08-26 13:35:56 +03:00
}
function isOrgPage(path) {
return /org\/[a-z 0-9 -]/.test(path);
}
2021-09-17 06:33:49 +03:00
function handleResize() {
isMobile.update(() => window.innerWidth <= 760);
}
2021-07-31 23:06:14 +03:00
onMount(() => {
2022-11-26 07:58:37 +01:00
console.log('Welcome to ClassroomIO, we are grateful you chose us.');
// Disable GA on localhost
if (config && !config.isProd) {
console.log('disable GA');
window['ga-disable-G-C7WBN0S06R'] = true;
}
2021-09-17 06:33:49 +03:00
handleResize();
2021-07-31 23:06:14 +03:00
const { data: authListener } = supabase.auth.onAuthStateChange(
(event, session) => {
if (config.isProd) {
2021-08-21 10:18:10 +03:00
handleAuthChange(event, session);
}
2021-10-22 14:55:33 +03:00
console.log(`event`, event);
2022-12-03 22:45:01 +01:00
if (event == 'PASSWORD_RECOVERY') {
console.log('PASSWORD RESET');
}
if (path.includes('reset')) {
console.log('Dont change auth when on reset page');
return;
}
2021-07-31 23:06:14 +03:00
if (event === 'SIGNED_IN') {
2021-08-18 19:47:02 +03:00
$user.fetchingUser = true;
2021-07-31 23:06:14 +03:00
getProfile();
} else {
2021-08-18 19:47:02 +03:00
console.log('not logged in, go to login');
2022-12-03 22:45:01 +01:00
return goto('/login');
2021-07-31 23:06:14 +03:00
}
}
);
return () => {
authListener.unsubscribe();
};
});
2022-12-03 22:45:01 +01:00
// if (typeof window !== 'undefined' && typeof document !== 'undefined') {
// page.subscribe(() => {
// const from = window.location.pathname;
// const redirect = (href) => {
// goto(href);
// };
// if (!$user.isLoggedIn && !PUBLIC_ROUTES.includes(from)) {
// redirect('/login');
// }
// });
// }
2021-07-31 23:06:14 +03:00
$: path = $page.path.replace('/', '');
2020-12-24 20:32:05 +02:00
</script>
2021-09-17 06:33:49 +03:00
<svelte:window on:resize={handleResize} />
<svelte:head>
<link href="/css/carbon.css" rel="stylesheet" />
</svelte:head>
2021-05-07 03:43:37 +03:00
<Tailwindcss />
<!-- <Nav {segment} /> -->
2021-08-28 22:44:06 +03:00
<Snackbar />
2021-05-07 03:43:37 +03:00
2021-07-27 22:53:51 +03:00
<main>
2021-09-11 19:27:11 +03:00
{#if $preloading && $delayedPreloading}
<Backdrop>
<Moon size="60" color="#1d4ed8" unit="px" duration="1s" />
2021-09-11 19:27:11 +03:00
</Backdrop>
{/if}
2022-12-03 22:45:01 +01:00
{#if !ROUTES_TO_HIDE_NAV.includes($page.path) && !isCoursePage(path)}
{#if $page.path.includes('org') || $page.path.includes('profile')}
<OrgNavigation />
{:else}
<LandingNavigation {segment} />
{/if}
2021-07-26 02:31:00 +03:00
{/if}
2021-07-31 23:06:14 +03:00
2021-07-27 22:59:41 +03:00
<div class="flex justify-between">
{#if isOrgPage($page.path)}
<div class="org-root w-full flex items-center justify-between">
<OrgSideBar />
<div class="org-slot bg-white">
<slot />
</div>
</div>
{:else}
<slot />
{/if}
2021-07-31 23:06:14 +03:00
{#if isCoursePage(path)}
2021-07-27 22:59:41 +03:00
<Apps />
2022-11-26 07:48:49 +01:00
{/if}
2021-07-27 22:59:41 +03:00
</div>
2021-05-07 03:43:37 +03:00
</main>
2021-07-31 23:06:14 +03:00
{#if !['about', ''].includes(path)}
2021-08-18 19:47:02 +03:00
<!-- <Footer {segment} /> -->
2021-07-31 23:06:14 +03:00
{/if}
2021-05-07 03:43:37 +03:00
2020-12-24 20:32:05 +02:00
<style>
2020-12-30 22:29:01 +02:00
main {
2021-08-29 20:35:58 +03:00
background-color: #fafbfc;
2020-12-30 22:29:01 +02:00
box-sizing: border-box;
}
.org-root {
height: calc(100vh - 44px);
}
.org-slot {
width: calc(100vw - 250px);
height: calc(100vh - 44px);
overflow-y: auto;
}
2021-04-03 16:43:02 +03:00
:global(a:hover) {
text-decoration: underline;
}
2021-05-06 08:22:25 +03:00
:global(:root) {
2021-07-28 22:29:23 +03:00
--main-primary-color: rgba(29, 78, 216, 1);
2021-05-06 08:22:25 +03:00
--border-color: #eaecef;
2022-11-28 11:23:46 +01:00
--app-background-color: #fafbfc;
}
:global(.app-background-color) {
background-color: var(--app-background-color);
2021-05-06 08:22:25 +03:00
}
2020-12-24 20:32:05 +02:00
</style>