Files
classroomio/src/routes/_layout.svelte
T

187 lines
4.8 KiB
Svelte
Raw Normal View History

2021-07-31 23:06:14 +03:00
<script context="module">
export function preload(page, { config }) {
2021-08-18 20:23:56 +03:00
return { config, isMvpUser: !!page.query.mvp };
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';
2021-09-11 19:27:11 +03:00
import { Firework } from 'svelte-loading-spinners';
2021-07-26 02:31:00 +03:00
import Tailwindcss from '../components/Tailwindcss.svelte';
2021-07-27 22:53:51 +03:00
import Navigation from '../components/Navigation/index.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 SideBar from "../components/SideBar/index.svelte";
2021-08-18 19:47:02 +03:00
// import Footer from '../components/Footer/index.svelte';
2021-08-21 12:35:01 +03: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';
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-08-18 20:23:56 +03:00
export let isMvpUser;
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-08-18 20:23:56 +03:00
let allowUser = false;
2021-07-31 23:06:14 +03:00
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() {
2021-08-18 19:47:02 +03:00
console.log('Get user profile');
2021-07-31 23:06:14 +03:00
// Get user profile
const authUser = supabase.auth.user();
// Check if user has profile
let {
data: profileData,
error,
status,
} = await supabase
.from('profile')
2021-08-18 19:47:02 +03:00
.select(`*, org:organizationmember(*)`)
.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,
username: regexUsernameMatch[1],
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]);
// If user coming to login page, then
if (path.includes('login') || path.includes('signup')) {
2021-08-28 22:44:06 +03:00
goto('/profile/' + $profile.id);
2021-08-18 19:47:02 +03:00
}
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;
console.log(`profileData`, profileData);
2021-07-31 23:06:14 +03:00
profile.set(profileData);
2021-08-18 19:47:02 +03:00
// If user coming to login page, then
if (path.includes('login') || path.includes('signup')) {
2021-08-28 22:44:06 +03:00
goto('/profile/' + $profile.id);
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 /courses\/[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(() => {
2021-09-17 06:33:49 +03:00
handleResize();
2021-08-18 20:23:56 +03:00
const _isMvpUser = localStorage.getItem('mvp');
2021-08-18 20:43:20 +03:00
console.log('mounting layout', isMvpUser, path, _isMvpUser);
if (isMvpUser || window.location.search.includes('mvp')) {
2021-08-18 20:23:56 +03:00
localStorage.setItem('mvp', 'true');
allowUser = true;
} else if (!_isMvpUser) {
2021-09-17 06:33:49 +03:00
// if (!!path) {
// goto('/courses');
// }
2021-08-18 20:23:56 +03:00
return;
}
2021-07-31 23:06:14 +03:00
const { data: authListener } = supabase.auth.onAuthStateChange(
(event, session) => {
2021-08-21 10:18:10 +03:00
if (config.isDev) {
handleAuthChange(event, session);
}
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');
return goto('/courses');
2021-07-31 23:06:14 +03:00
}
}
);
return () => {
authListener.unsubscribe();
};
});
$: 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} />
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>
<Firework size="60" color="#fff" unit="px" duration="1s" />
</Backdrop>
{/if}
2021-08-26 13:35:56 +03:00
{#if !['login', 'signup'].includes(path) && !isCoursePage(path)}
2021-08-18 20:23:56 +03:00
<Navigation {segment} disableLogin={!allowUser} />
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">
<slot />
2021-07-31 23:06:14 +03:00
2021-08-18 19:47:02 +03:00
<!-- {#if path.includes('courses')}
2021-07-27 22:59:41 +03:00
<Apps />
2021-08-18 19:47:02 +03: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;
}
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;
}
2020-12-24 20:32:05 +02:00
</style>