Routing: add hasRoute and hasAnyRoute and make optional components views not render route if they don't exist
This commit is contained in:
parent
c0245e3e3d
commit
e97cbb5d7f
@ -3,16 +3,18 @@
|
|||||||
import Message from "../../components/Message.svelte";
|
import Message from "../../components/Message.svelte";
|
||||||
import Form from "../../utils/Form.svelte";
|
import Form from "../../utils/Form.svelte";
|
||||||
import Field from "../../utils/Field.svelte";
|
import Field from "../../utils/Field.svelte";
|
||||||
|
import {hasRoute, route} from "../../../../common/Routing";
|
||||||
|
|
||||||
let newName = '';
|
let newName = '';
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
{#if hasRoute('change-name')}
|
||||||
<section class="panel">
|
<section class="panel">
|
||||||
<h2><i data-feather="key"></i> Change name</h2>
|
<h2><i data-feather="key"></i> Change name</h2>
|
||||||
|
|
||||||
|
|
||||||
{#if $locals.can_change_name}
|
{#if $locals.can_change_name}
|
||||||
<Form action={$locals.route('change-name')}
|
<Form action={route('change-name')}
|
||||||
submitIcon="save" submitText="Change my name {newName.length > 0 ? 'to ' + newName : ''}"
|
submitIcon="save" submitText="Change my name {newName.length > 0 ? 'to ' + newName : ''}"
|
||||||
confirm="Are you sure you want to change your name to {newName}?">
|
confirm="Are you sure you want to change your name to {newName}?">
|
||||||
<Field type="text" name="name" icon="user" placeholder="New name" required bind:value={newName}/>
|
<Field type="text" name="name" icon="user" placeholder="New name" required bind:value={newName}/>
|
||||||
@ -29,3 +31,4 @@
|
|||||||
<Message type="info" content="You will be able to change your name in {$locals.can_change_name_in}" sticky discreet/>
|
<Message type="info" content="You will be able to change your name in {$locals.can_change_name_in}" sticky discreet/>
|
||||||
{/if}
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
|
{/if}
|
@ -2,21 +2,23 @@
|
|||||||
import {locals} from "../../../ts/stores";
|
import {locals} from "../../../ts/stores";
|
||||||
import Form from "../../utils/Form.svelte";
|
import Form from "../../utils/Form.svelte";
|
||||||
import Field from "../../utils/Field.svelte";
|
import Field from "../../utils/Field.svelte";
|
||||||
|
import {hasRoute, route} from "../../../../common/Routing";
|
||||||
|
|
||||||
let removePasswordMode = false;
|
let removePasswordMode = false;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
{#if hasRoute('remove-password', 'change-password')}
|
||||||
<section class="panel">
|
<section class="panel">
|
||||||
<h2><i data-feather="key"></i> {$locals.has_password ? 'Change' : 'Set'} password</h2>
|
<h2><i data-feather="key"></i> {$locals.has_password ? 'Change' : 'Set'} password</h2>
|
||||||
|
|
||||||
{#if removePasswordMode}
|
{#if removePasswordMode}
|
||||||
<Form action={$locals.route('remove-password')}
|
<Form action={route('remove-password')}
|
||||||
submitIcon="trash" submitText="Remove password" submitClass="danger"
|
submitIcon="trash" submitText="Remove password" submitClass="danger"
|
||||||
confirm="Are you sure you want to remove your password?">
|
confirm="Are you sure you want to remove your password?">
|
||||||
<button type="button" on:click={() => removePasswordMode = false}>Go back</button>
|
<button type="button" on:click={() => removePasswordMode = false}>Go back</button>
|
||||||
</Form>
|
</Form>
|
||||||
{:else}
|
{:else}
|
||||||
<Form action={$locals.route('change-password')}
|
<Form action={route('change-password')}
|
||||||
submitIcon="save" submitText="Set password">
|
submitIcon="save" submitText="Set password">
|
||||||
{#if $locals.has_password}
|
{#if $locals.has_password}
|
||||||
<Field type="password" name="current_password" icon="key" placeholder="Current password"/>
|
<Field type="password" name="current_password" icon="key" placeholder="Current password"/>
|
||||||
@ -28,3 +30,4 @@
|
|||||||
</Form>
|
</Form>
|
||||||
{/if}
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
|
{/if}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
import PasswordPanel from "./PasswordPanel.svelte";
|
import PasswordPanel from "./PasswordPanel.svelte";
|
||||||
import Form from "../../utils/Form.svelte";
|
import Form from "../../utils/Form.svelte";
|
||||||
import Field from "../../utils/Field.svelte";
|
import Field from "../../utils/Field.svelte";
|
||||||
|
import {route} from "../../../../common/Routing";
|
||||||
|
|
||||||
const mainEmail = $locals.main_email?.email;
|
const mainEmail = $locals.main_email?.email;
|
||||||
const personalInfoFields = $locals.user_personal_info_fields || [];
|
const personalInfoFields = $locals.user_personal_info_fields || [];
|
||||||
@ -68,13 +69,13 @@
|
|||||||
<td>Secondary</td>
|
<td>Secondary</td>
|
||||||
<td>{email.email}</td>
|
<td>{email.email}</td>
|
||||||
<td class="actions">
|
<td class="actions">
|
||||||
<Form action={$locals.route('set-main-email')} button
|
<Form action={route('set-main-email')} button
|
||||||
submitIcon="refresh-ccw" submitText="Set as main address" submitClass="warning"
|
submitIcon="refresh-ccw" submitText="Set as main address" submitClass="warning"
|
||||||
confirm="Are you sure you want to set {email.email} as your main address?">
|
confirm="Are you sure you want to set {email.email} as your main address?">
|
||||||
<Field type="hidden" name="id" value={email.id}/>
|
<Field type="hidden" name="id" value={email.id}/>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
<Form action={$locals.route('remove-email')} button
|
<Form action={route('remove-email')} button
|
||||||
submitIcon="trash" submitText="Remove" submitClass="danger"
|
submitIcon="trash" submitText="Remove" submitClass="danger"
|
||||||
confirm="Are you sure you want to delete {email.email}?">
|
confirm="Are you sure you want to delete {email.email}?">
|
||||||
<Field type="hidden" name="id" value={email.id}/>
|
<Field type="hidden" name="id" value={email.id}/>
|
||||||
@ -87,7 +88,7 @@
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Form action={$locals.route('add-email')} class="sub-panel"
|
<Form action={route('add-email')} class="sub-panel"
|
||||||
submitIcon="plus" submitText="Add email address">
|
submitIcon="plus" submitText="Add email address">
|
||||||
<h3>Add an email address:</h3>
|
<h3>Add an email address:</h3>
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
import Form from "../utils/Form.svelte";
|
import Form from "../utils/Form.svelte";
|
||||||
import Field from "../utils/Field.svelte";
|
import Field from "../utils/Field.svelte";
|
||||||
import Icon from "../utils/Icon.svelte";
|
import Icon from "../utils/Icon.svelte";
|
||||||
|
import {hasRoute, route} from "../../../common/Routing";
|
||||||
|
|
||||||
let registerUsingMagicLink = $locals.previousFormData()?.['auth_method'] !== 'password';
|
let registerUsingMagicLink = $locals.previousFormData()?.['auth_method'] !== 'password';
|
||||||
let loginUsingMagicLink = true;
|
let loginUsingMagicLink = true;
|
||||||
@ -22,10 +23,11 @@
|
|||||||
h1="Authentication and registration">
|
h1="Authentication and registration">
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
{#if hasRoute('login')}
|
||||||
<section class="panel">
|
<section class="panel">
|
||||||
<h2><i data-feather="log-in"></i> Log in</h2>
|
<h2><i data-feather="log-in"></i> Log in</h2>
|
||||||
|
|
||||||
<Form action={$locals.route('login') + queryStr} submitText="Authenticate" submitIcon="log-in">
|
<Form action={route('login') + queryStr} submitText="Authenticate" submitIcon="log-in">
|
||||||
<Field type="text" name="identifier" value={$locals.query?.identifier} icon="at-sign"
|
<Field type="text" name="identifier" value={$locals.query?.identifier} icon="at-sign"
|
||||||
hint={loginUsingMagicLink ? 'You will receive a magic link in your mailbox. Click on the link from any device to authenticate here.' : ''}
|
hint={loginUsingMagicLink ? 'You will receive a magic link in your mailbox. Click on the link from any device to authenticate here.' : ''}
|
||||||
placeholder="Your email address or username" required/>
|
placeholder="Your email address or username" required/>
|
||||||
@ -39,11 +41,13 @@
|
|||||||
<Field type="checkbox" name="persist_session" icon="clock" placeholder="Stay logged in on this computer."/>
|
<Field type="checkbox" name="persist_session" icon="clock" placeholder="Stay logged in on this computer."/>
|
||||||
</Form>
|
</Form>
|
||||||
</section>
|
</section>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if hasRoute('register')}
|
||||||
<section class="panel">
|
<section class="panel">
|
||||||
<h2><i data-feather="user-plus"></i> Register</h2>
|
<h2><i data-feather="user-plus"></i> Register</h2>
|
||||||
|
|
||||||
<Form action={$locals.route('register') + queryStr} submitText="Register" submitIcon="check">
|
<Form action={route('register') + queryStr} submitText="Register" submitIcon="check">
|
||||||
<Field type="hidden" name="auth_method" value={registerUsingMagicLink ? 'magic_link': 'password'}/>
|
<Field type="hidden" name="auth_method" value={registerUsingMagicLink ? 'magic_link': 'password'}/>
|
||||||
|
|
||||||
{#if $locals.has_username}
|
{#if $locals.has_username}
|
||||||
@ -68,5 +72,6 @@
|
|||||||
</Field>
|
</Field>
|
||||||
</Form>
|
</Form>
|
||||||
</section>
|
</section>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</BaseLayout>
|
</BaseLayout>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
import Form from "../utils/Form.svelte";
|
import Form from "../utils/Form.svelte";
|
||||||
import Field from "../utils/Field.svelte";
|
import Field from "../utils/Field.svelte";
|
||||||
import Breadcrumb from "../components/Breadcrumb.svelte";
|
import Breadcrumb from "../components/Breadcrumb.svelte";
|
||||||
import {route} from "../../../common/Routing";
|
import {hasRoute, route} from "../../../common/Routing";
|
||||||
|
|
||||||
const accounts = $locals.accounts || [];
|
const accounts = $locals.accounts || [];
|
||||||
</script>
|
</script>
|
||||||
@ -49,16 +49,20 @@
|
|||||||
<td><time datetime={user.created_at_iso}>{user.created_at_human} ago</time></td>
|
<td><time datetime={user.created_at_iso}>{user.created_at_human} ago</time></td>
|
||||||
<td>
|
<td>
|
||||||
<div class="max-content">
|
<div class="max-content">
|
||||||
<Form action={$locals.route('approve-account')}
|
{#if hasRoute('approve-account')}
|
||||||
|
<Form action={route('approve-account')}
|
||||||
submitIcon="check" submitText="Approve" submitClass="success">
|
submitIcon="check" submitText="Approve" submitClass="success">
|
||||||
<Field type="hidden" name="user_id" value={user.id}/>
|
<Field type="hidden" name="user_id" value={user.id}/>
|
||||||
</Form>
|
</Form>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<Form action={$locals.route('reject-account')}
|
{#if hasRoute('reject-account')}
|
||||||
|
<Form action={route('reject-account')}
|
||||||
submitIcon="trash" submitText="Reject" submitClass="danger"
|
submitIcon="trash" submitText="Reject" submitClass="danger"
|
||||||
confirm="This will irrevocably delete the {user.mainEmailStr || user.name || user.id} account.">
|
confirm="This will irrevocably delete the {user.mainEmailStr || user.name || user.id} account.">
|
||||||
<Field type="hidden" name="user_id" value={user.id}/>
|
<Field type="hidden" name="user_id" value={user.id}/>
|
||||||
</Form>
|
</Form>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {locals} from "../ts/stores";
|
import {locals} from "../ts/stores";
|
||||||
import {route} from "../../common/Routing";
|
import {route, hasRoute, hasAnyRoute} from "../../common/Routing";
|
||||||
import BaseLayout from "./layouts/BaseLayout.svelte";
|
import BaseLayout from "./layouts/BaseLayout.svelte";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -9,11 +9,17 @@
|
|||||||
<h1>swaf - Svelte Web Application Framework</h1>
|
<h1>swaf - Svelte Web Application Framework</h1>
|
||||||
<p>Welcome to {$locals.app.name}!</p>
|
<p>Welcome to {$locals.app.name}!</p>
|
||||||
|
|
||||||
|
{#if hasAnyRoute('tests', 'design')}
|
||||||
<nav>
|
<nav>
|
||||||
<ul>
|
<ul>
|
||||||
|
{#if hasRoute('tests')}
|
||||||
<li><a href={route('tests')}>Frontend tests</a></li>
|
<li><a href={route('tests')}>Frontend tests</a></li>
|
||||||
|
{/if}
|
||||||
|
{#if hasRoute('design')}
|
||||||
<li><a href={route('design')}>Design test</a></li>
|
<li><a href={route('design')}>Design test</a></li>
|
||||||
|
{/if}
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</BaseLayout>
|
</BaseLayout>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {locals} from "../../ts/stores.js";
|
import {locals} from "../../ts/stores.js";
|
||||||
import {route} from "../../../common/Routing.js";
|
import {hasRoute, route} from "../../../common/Routing.js";
|
||||||
import FlashMessages from "../components/FlashMessages.svelte";
|
import FlashMessages from "../components/FlashMessages.svelte";
|
||||||
import NavMenuItem from "../components/NavMenuItem.svelte";
|
import NavMenuItem from "../components/NavMenuItem.svelte";
|
||||||
import NavMenu from "../components/NavMenu.svelte";
|
import NavMenu from "../components/NavMenu.svelte";
|
||||||
@ -94,6 +94,7 @@
|
|||||||
<header>
|
<header>
|
||||||
<a href="/" class="logo"><img src="/img/logo.svg" alt="{$locals.app.name} logo"> {$locals.app.name}</a>
|
<a href="/" class="logo"><img src="/img/logo.svg" alt="{$locals.app.name} logo"> {$locals.app.name}</a>
|
||||||
<NavMenu>
|
<NavMenu>
|
||||||
|
{#if hasRoute('auth')}
|
||||||
{#if $locals.user}
|
{#if $locals.user}
|
||||||
{#if $locals.user.is_admin}
|
{#if $locals.user.is_admin}
|
||||||
<NavMenuItem href={route('backend')} icon="settings" text="Backend"/>
|
<NavMenuItem href={route('backend')} icon="settings" text="Backend"/>
|
||||||
@ -104,6 +105,7 @@
|
|||||||
{:else}
|
{:else}
|
||||||
<NavMenuItem href={route('auth')} icon="log-in" text="Log in / Register"/>
|
<NavMenuItem href={route('auth')} icon="log-in" text="Log in / Register"/>
|
||||||
{/if}
|
{/if}
|
||||||
|
{/if}
|
||||||
</NavMenu>
|
</NavMenu>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
|
@ -66,3 +66,19 @@ export function route(
|
|||||||
export function getRouteParamRegExp(key: string, flags?: string): RegExp {
|
export function getRouteParamRegExp(key: string, flags?: string): RegExp {
|
||||||
return new RegExp(`:${key}(\\(.+?\\))?\\??`, flags);
|
return new RegExp(`:${key}(\\(.+?\\))?\\??`, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function hasRoute(...routesToMatch: string[]): boolean {
|
||||||
|
for (const route of routesToMatch) {
|
||||||
|
if (!routes[route]) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function hasAnyRoute(...routesToMatch: string[]): boolean {
|
||||||
|
for (const route of routesToMatch) {
|
||||||
|
if (routes[route]) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user