Files
classroomio/src/routes/_layout.svelte
T

119 lines
2.6 KiB
Svelte
Raw Normal View History

2021-07-31 23:06:14 +03:00
<script context="module">
export function preload(page, { config }) {
return { config };
}
</script>
2020-12-24 20:32:05 +02:00
<script>
2021-07-31 23:06:14 +03:00
import { onMount } from 'svelte';
import { stores, goto } from '@sapper/app';
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';
// import SideBar from "../components/SideBar/index.svelte";
2021-07-26 02:31:00 +03:00
import Footer from '../components/Footer/index.svelte';
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';
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-07-31 23:06:14 +03:00
let supabase = getSupabase(config);
let { page } = stores();
2021-07-31 23:06:14 +03:00
let path = $page.path.replace('/', '');
async function getProfile() {
// Get user profile
const authUser = supabase.auth.user();
// Check if user has profile
let {
data: profileData,
error,
status,
} = await supabase
.from('profile')
.select(`*`)
.eq('user_id', authUser.id)
.single();
if (error && !profileData && status === 406) {
// User wasn't found, create profile
const { data, error } = await supabase
.from('profile')
.insert([{ user_id: authUser.id }]);
// Profile created, go to profile page
if (!error && data) {
user.set(authUser);
profile.set(data);
return goto('/');
}
} else if (profileData) {
// Profile exists, go to profile page
user.set(authUser);
profile.set(profileData);
return goto('/');
}
}
onMount(() => {
const { data: authListener } = supabase.auth.onAuthStateChange(
(event, session) => {
handleAuthChange(event, session);
if (event === 'SIGNED_IN') {
getProfile();
} else {
return goto('/');
}
}
);
return () => {
authListener.unsubscribe();
};
});
$: path = $page.path.replace('/', '');
2020-12-24 20:32:05 +02:00
</script>
2021-05-07 03:43:37 +03:00
<Tailwindcss />
<!-- <Nav {segment} /> -->
2021-07-27 22:53:51 +03:00
<main>
2021-07-31 23:06:14 +03:00
{#if !['login', 'signup', ''].includes(path)}
2021-07-27 22:53:51 +03:00
<Navigation {segment} />
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
{#if path.includes('courses')}
2021-07-27 22:59:41 +03:00
<Apps />
{/if}
</div>
2021-05-07 03:43:37 +03:00
</main>
2021-07-31 23:06:14 +03:00
{#if !['about', ''].includes(path)}
<Footer {segment} />
{/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 {
background-color: white;
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>