Add built-in screens (login, profile, menu, posts), per-user and site themes
This commit is contained in:
1 parent
4bf0a8bbb0
commit
351667abaf
46 files changed
+3720
-12
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import { reactive } from 'vue';
|
||||
import { ApiError, api, boot } from './api';
|
||||
import { t } from './i18n';
|
||||
import Dashboard from './pages/Dashboard.vue';
|
||||
import Fallback from './pages/Fallback.vue';
|
||||
import Login from './pages/Login.vue';
|
||||
import PostForm from './pages/PostForm.vue';
|
||||
import PostsIndex from './pages/PostsIndex.vue';
|
||||
import Profile from './pages/Profile.vue';
|
||||
|
||||
/** The screens that exist. Every other manager address shows `Fallback` until its module gets a screen. */
|
||||
const routes: { pattern: RegExp; component: any; guest?: boolean; data?: boolean }[] = [
|
||||
{ pattern: /^\/?$/, component: Dashboard, data: true },
|
||||
{ pattern: /^\/login$/, component: Login, guest: true },
|
||||
{ pattern: /^\/profile$/, component: Profile, data: true },
|
||||
{ pattern: /^\/posts$/, component: PostsIndex, data: true },
|
||||
{ pattern: /^\/posts\/create$/, component: PostForm, data: true },
|
||||
{ pattern: /^\/posts\/\d+\/edit$/, component: PostForm, data: true },
|
||||
];
|
||||
|
||||
export const state = reactive({
|
||||
path: '',
|
||||
component: null as any,
|
||||
props: {} as Record<string, any>,
|
||||
loading: true,
|
||||
forbidden: false,
|
||||
toast: null as { text: string; error: boolean } | null,
|
||||
});
|
||||
|
||||
let timer: number | undefined;
|
||||
|
||||
export function flash(text: string, error = false): void {
|
||||
state.toast = { text, error };
|
||||
window.clearTimeout(timer);
|
||||
timer = window.setTimeout(() => (state.toast = null), 4000);
|
||||
}
|
||||
|
||||
/** The path inside the manager: without the base, without the query. */
|
||||
function inside(url: string): string {
|
||||
return url.split('?')[0].slice(boot.base.length) || '/';
|
||||
}
|
||||
|
||||
export async function load(url: string): Promise<void> {
|
||||
const route = routes.find((r) => r.pattern.test(inside(url)));
|
||||
|
||||
if (!boot.user && !route?.guest) {
|
||||
window.location.assign(boot.base + '/login');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (boot.user && route?.guest) {
|
||||
return navigate(boot.base, true);
|
||||
}
|
||||
|
||||
state.loading = true;
|
||||
state.forbidden = false;
|
||||
state.path = url;
|
||||
state.component = route?.component ?? Fallback;
|
||||
state.props = {};
|
||||
|
||||
try {
|
||||
state.props = route?.data ? await api.get(url) : {};
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError && error.status === 401) {
|
||||
window.location.assign(boot.base + '/login');
|
||||
} else if (error instanceof ApiError && error.status === 403) {
|
||||
state.forbidden = true;
|
||||
} else {
|
||||
flash(t('Something went wrong. Try again.'), true);
|
||||
}
|
||||
} finally {
|
||||
state.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
export function navigate(url: string, replace = false): Promise<void> {
|
||||
window.history[replace ? 'replaceState' : 'pushState']({}, '', url);
|
||||
window.scrollTo(0, 0);
|
||||
|
||||
return load(url);
|
||||
}
|
||||
|
||||
export function reload(): Promise<void> {
|
||||
return load(state.path);
|
||||
}
|
||||
|
||||
export function start(): void {
|
||||
window.addEventListener('popstate', () => load(window.location.pathname + window.location.search));
|
||||
void load(window.location.pathname + window.location.search);
|
||||
}
|
||||
Reference in new issue
Block a user