Skip to content

Commit 85118e7

Browse files
committed
chore: proxy globally usable
1 parent cd5741b commit 85118e7

File tree

16 files changed

+265
-193
lines changed

16 files changed

+265
-193
lines changed

svelte_frontend/src/app.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare global {
88
// interface Error {}
99
interface Locals {
1010
current_user: import('$lib/interfaces/auth').User;
11-
site_data: import('$lib/interfaces/siteData').SiteData;
11+
site_data: import('$lib/interfaces/site-data').SiteData;
1212
}
1313

1414
// interface PageData {}
@@ -46,9 +46,11 @@ declare global {
4646
type MessageFlux = {
4747
message_type?: MessageType
4848
alias?: string;
49-
message?: string;
49+
message: string;
5050
} & ActionPathRequired;
5151

52+
type BASE_METHOD = 'GET' | 'POST' | 'PUT' | 'DELETE';
53+
5254
type RedirectStatus = 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
5355

5456
type FlashRedirect = (

svelte_frontend/src/hooks.server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import {assign_cookies} from "@friendofsvelte/django-kit/server/utils";
21
import type {Handle, RequestEvent} from "@sveltejs/kit";
32
import {sequence} from "@sveltejs/kit/hooks";
43
import type {User} from "$lib/interfaces/auth";
5-
import type {SiteData} from "$lib/interfaces/siteData";
6-
import {assign_headers, get_headers} from "$lib/server/request";
4+
import type {SiteData} from "$lib/interfaces/site-data";
5+
import {assign_headers, get_headers, assign_cookies} from "$lib/server/request";
76
import {SECRET_BASE_API} from "$env/static/private";
87

98
export const handleFetch = async ({request, fetch, event}) => {

svelte_frontend/src/items/Flash.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import {fade} from "svelte/transition";
33
import {flip} from "svelte/animate";
44
import {quintOut} from "svelte/easing";
5-
import {dismiss_toast, notifier} from "@friendofsvelte/django-kit/notifier";
65
import BxsCheckCircle from "$icons/BxsCheckCircle.svelte";
76
import BxsError from "$icons/BxsError.svelte";
87
import BxsErrorCircle from "$icons/BxsErrorCircle.svelte";
@@ -15,6 +14,7 @@
1514
flashMessageVariants,
1615
flashActionVariants
1716
} from '$lib/helpers/variants/flash-variants';
17+
import {dismiss_toast, notifier} from "$lib/stores/notifier.svelte";
1818
1919
const icons_ = {
2020
success: BxsCheckCircle,
@@ -42,7 +42,7 @@
4242
</h3>
4343
<button
4444
class={flashExitButtonVariants({ type: toast.message_type })}
45-
on:click={() => dismiss_toast(toast.id)}
45+
onclick={() => dismiss_toast(toast.id)}
4646
>
4747
<BxsXCircle display class="h-full w-full"/>
4848
</button>
Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,65 @@
1+
<script lang="ts" module>
2+
import type {FormActionResult} from '$lib/interfaces';
3+
import type {Snippet} from 'svelte';
4+
import type {ActionResult} from '@sveltejs/kit';
5+
import type {HTMLFormAttributes} from "svelte/elements";
6+
7+
export interface FormProps {
8+
action?: string;
9+
loading?: boolean;
10+
method?: HTMLFormAttributes['method'];
11+
class?: string;
12+
action_function?: () => void;
13+
children: Snippet;
14+
ivl?: boolean,
15+
reset?: boolean,
16+
after?: (result: FormActionResult) => void;
17+
enctype?: HTMLFormAttributes['enctype'];
18+
}
19+
20+
export interface FormActionArgs {
21+
result: ActionResult;
22+
update: (options: { invalidateAll: boolean; reset?: boolean }) => Promise<void>;
23+
}
24+
</script>
25+
126
<script lang="ts">
2-
import {enhance} from "$app/forms";
3-
import type {Snippet} from 'svelte'
4-
5-
interface Props {
6-
action?: string;
7-
loading?: boolean;
8-
method?: string;
9-
class: string;
10-
action_function?: (e: Event) => void;
11-
children: Snippet;
12-
inval?: boolean,
13-
after?: () => void;
14-
}
15-
16-
let {
17-
action = '',
18-
method = 'post',
19-
class: className = '',
20-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
21-
loading = $bindable(),
22-
inval: invalidateAll = true,
23-
children,
24-
after = () => {
25-
}
26-
}: Props = $props();
27-
let action_function = () => {
28-
loading = true;
29-
return ({update}: { update: ({invalidateAll}: { invalidateAll: boolean }) => Promise<void> }) => {
30-
update({invalidateAll}).finally(async () => {
31-
loading = false;
32-
after();
33-
});
34-
};
35-
}
27+
import {enhance} from '$app/forms';
3628
29+
let {
30+
action = '',
31+
method = 'post',
32+
class: className = '',
33+
loading = $bindable(false),
34+
ivl: invalidateAll = true,
35+
children,
36+
after,
37+
reset = true,
38+
enctype,
39+
action_function = actionFunction
40+
}: FormProps = $props();
41+
42+
43+
async function actionFunction() {
44+
loading = true;
45+
46+
return async ({result, update}: FormActionArgs) => {
47+
await update({invalidateAll, reset}).finally(async () => {
48+
loading = false;
49+
if (after && typeof after === 'function') {
50+
after(result as unknown as FormActionResult);
51+
}
52+
});
53+
};
54+
}
3755
</script>
3856

39-
<form {action} {method} use:enhance={action_function} class={className} class:loading>
40-
{@render children()}
41-
</form>
57+
<form
58+
{action}
59+
use:enhance={action_function}
60+
{method}
61+
class={className}
62+
class:loading
63+
{enctype}>
64+
{@render children()}
65+
</form>

svelte_frontend/src/items/PutFlash.svelte

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import {page} from '$app/stores';
33
import {parse} from 'cookie';
44
import {afterNavigate} from '$app/navigation';
5-
import type {FlashMessage, Message, MessageFlux,} from "$lib/types.js";
6-
import {add_toast, dismiss_toast_after} from "$lib/notifier.svelte.js";
5+
import {add_toast, dismiss_toast_after} from "$lib/stores/notifier.svelte";
76
import {untrack} from "svelte";
87
98
function flux_message_to_proper(msg_source: MessageFlux) {
@@ -23,8 +22,8 @@
2322
2423
$effect(() => {
2524
trigger_message($page.form);
26-
trigger_message($page.data);
27-
trigger_message($page.error);
25+
trigger_message($page.data as unknown as MessageFlux);
26+
trigger_message($page.error as unknown as MessageFlux);
2827
});
2928
3029
function assign_flash_message() {

svelte_frontend/src/items/TodoItem.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ViewDate from "$items/ViewDate.svelte";
66
import {tv, type VariantProps} from "tailwind-variants";
77
import BiCircleFill from "$icons/BiCircleFill.svelte";
8+
import Record from "$items/Record.svelte";
89
910
// Props destructuring with type safety
1011
interface Props {
@@ -177,7 +178,7 @@
177178
<button
178179
type="button"
179180
class={actionButton({ intent: "primary", padding: "left" })}
180-
on:click={openPopup}
181+
onclick={openPopup}
181182
>
182183
edit
183184
</button>
@@ -189,15 +190,12 @@
189190
{#if is_edit_popup_enabled}
190191
<div class={modal()}>
191192
<Form
192-
action="?/todos/update&_sfx=/{todo.id}/"
193+
action="?/call&s=/todos/update/{todo.id}/&m=post"
193194
method="post"
194-
after={closePopup}
195195
class={modalContainer()}
196196
>
197197
<h3 class="text-lg font-bold">Edit todo: {todo.title}</h3>
198198

199-
<input type="hidden" name="todo_id" value={todo.id}/>
200-
201199
<div class={modalInput()}>
202200
<label for="title">Title</label>
203201
<input
@@ -230,7 +228,7 @@
230228
</div>
231229

232230
<button type="submit" class={actionButton({ intent: "primary" })}>save</button>
233-
<button type="button" class={actionButton({ intent: "danger" })} on:click={closePopup}>cancel</button>
231+
<button type="button" class={actionButton({ intent: "danger" })} onclick={closePopup}>cancel</button>
234232
</Form>
235233
</div>
236234
{/if}
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
import type {ActionResult} from "@sveltejs/kit";
2+
13
export interface GenericResponse {
2-
id: number;
3-
created_at: string;
4-
updated_at: string;
4+
id: number;
5+
created_at: string;
6+
updated_at: string;
57
}
68

79
export interface OffsetPaginated<T> {
8-
offset: number;
9-
limit: number;
10-
search: string;
11-
order_by: string;
12-
items: T[];
13-
total_pages: number;
14-
has_next: boolean;
15-
has_previous: boolean;
10+
offset: number;
11+
limit: number;
12+
search: string;
13+
order_by: string;
14+
items: T[];
15+
total_pages: number;
16+
has_next: boolean;
17+
has_previous: boolean;
1618
}
19+
20+
export type FormActionResult<
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
Success extends GenericObject = Record<string, any>,
23+
Failure extends GenericObject = Partial<ErrorResponseData>,
24+
> = Exclude<ActionResult<Success, Failure>, { type: 'redirect' }>;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
export enum Prox {
1+
export enum ReMod {
22
NAME = ".proxy",
3-
PARAMS = "q-params-001",
43
URL_NAME = "url_name",
54
}

0 commit comments

Comments
 (0)