2021-12-11 11:01:36 +00:00
|
|
|
<script lang="ts">
|
2021-12-12 09:31:18 +00:00
|
|
|
import { Container, Col, Row, TabContent, TabPane } from 'sveltestrap';
|
2021-12-11 11:01:36 +00:00
|
|
|
import { navigate, useLocation } from "svelte-navigator";
|
|
|
|
import { currentUser, loggedIn } from '../stores';
|
|
|
|
|
2021-12-11 17:45:08 +00:00
|
|
|
import System from '../lib/system/Main.svelte';
|
2021-12-11 11:01:36 +00:00
|
|
|
import PKAPI from '../api';
|
2021-12-12 06:45:42 +00:00
|
|
|
import Sys from '../api/system';
|
2021-12-18 19:10:02 +00:00
|
|
|
import MemberList from '../lib/member/List.svelte';
|
|
|
|
import GroupList from '../lib/group/List.svelte';
|
2021-12-11 11:01:36 +00:00
|
|
|
|
2021-12-11 17:41:21 +00:00
|
|
|
let isPublic = false;
|
|
|
|
|
2021-12-18 19:10:02 +00:00
|
|
|
let group = true;
|
|
|
|
let member = false;
|
|
|
|
|
2021-12-11 14:57:47 +00:00
|
|
|
// get the state from the navigator so that we know which tab to start on
|
2021-12-11 11:01:36 +00:00
|
|
|
let location = useLocation();
|
2021-12-15 12:56:36 +00:00
|
|
|
let params = $location.search && new URLSearchParams($location.search);
|
|
|
|
let tabPane: string;
|
|
|
|
if (params) {
|
|
|
|
tabPane = params.get("tab");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tabPane) {
|
2021-12-11 11:01:36 +00:00
|
|
|
tabPane = "system";
|
|
|
|
}
|
|
|
|
|
2021-12-11 14:57:47 +00:00
|
|
|
// subscribe to the cached user in the store
|
2021-12-11 11:01:36 +00:00
|
|
|
let current;
|
|
|
|
currentUser.subscribe(value => {
|
|
|
|
current = value;
|
|
|
|
});
|
2021-12-11 14:57:47 +00:00
|
|
|
|
|
|
|
// if there is no cached user, get the user from localstorage
|
2021-12-12 06:45:42 +00:00
|
|
|
let user = new Sys(current ? current : JSON.parse(localStorage.getItem("pk-user")));
|
2021-12-11 14:57:47 +00:00
|
|
|
// since the user in localstorage can be outdated, fetch the user from the api again
|
2021-12-11 11:01:36 +00:00
|
|
|
if (!current) {
|
|
|
|
login(localStorage.getItem("pk-token"));
|
|
|
|
}
|
|
|
|
|
2021-12-11 14:57:47 +00:00
|
|
|
// if there's no user, and there's no token, assume the login failed and send us back to the homepage.
|
2021-12-11 11:01:36 +00:00
|
|
|
if (!localStorage.getItem("pk-token") && !user) {
|
|
|
|
navigate("/");
|
|
|
|
}
|
|
|
|
|
2021-12-11 14:56:47 +00:00
|
|
|
let settings = JSON.parse(localStorage.getItem("pk-settings"));
|
|
|
|
|
2021-12-11 14:57:47 +00:00
|
|
|
// just the login function
|
2021-12-11 11:01:36 +00:00
|
|
|
async function login(token: string) {
|
|
|
|
const api = new PKAPI();
|
|
|
|
try {
|
|
|
|
if (!token) {
|
|
|
|
throw new Error("Token cannot be empty.")
|
|
|
|
}
|
|
|
|
const res: Sys = await api.getSystem({token: token});
|
|
|
|
localStorage.setItem("pk-token", token);
|
|
|
|
localStorage.setItem("pk-user", JSON.stringify(res));
|
|
|
|
loggedIn.update(() => true);
|
|
|
|
currentUser.update(() => res);
|
|
|
|
user = res;
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
localStorage.removeItem("pk-token");
|
|
|
|
localStorage.removeItem("pk-user");
|
|
|
|
currentUser.update(() => null);
|
|
|
|
navigate("/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 19:10:02 +00:00
|
|
|
// some values that get passed from the member to the group components and vice versa
|
|
|
|
let members = [];
|
|
|
|
let groups = [];
|
|
|
|
|
2021-12-11 11:01:36 +00:00
|
|
|
</script>
|
|
|
|
|
2021-12-11 14:56:47 +00:00
|
|
|
<!-- display the banner if there's a banner set, and if the current settings allow for it-->
|
2021-12-12 13:33:35 +00:00
|
|
|
{#if user && user.banner && ((settings && settings.appearance.banner_top) || !settings)}
|
2021-12-11 11:01:36 +00:00
|
|
|
<div class="banner" style="background-image: url({user.banner})" />
|
|
|
|
{/if}
|
|
|
|
<Container>
|
|
|
|
<Row>
|
2021-12-11 14:58:03 +00:00
|
|
|
<Col class="mx-auto" xs={12} lg={10}>
|
2021-12-11 11:01:36 +00:00
|
|
|
<TabContent class="mt-3">
|
|
|
|
<TabPane tabId="system" tab="System" active={tabPane === "system"}>
|
2021-12-15 12:56:36 +00:00
|
|
|
<System bind:user={user} bind:isPublic />
|
2021-12-11 11:01:36 +00:00
|
|
|
</TabPane>
|
|
|
|
<TabPane tabId="members" tab="Members" active={tabPane === "members"}>
|
2021-12-18 19:10:02 +00:00
|
|
|
<MemberList bind:list={members} bind:isPublic />
|
|
|
|
</TabPane>
|
|
|
|
<TabPane tabId="groups" tab="Groups" active={tabPane === "groups"}>
|
|
|
|
<GroupList bind:list={groups} bind:isPublic />
|
|
|
|
</TabPane>
|
2021-12-11 11:01:36 +00:00
|
|
|
</TabContent>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</Container>
|
|
|
|
|
|
|
|
<svelte:head>
|
|
|
|
<title>pk-webs | dash</title>
|
|
|
|
</svelte:head>
|