Skip to content

Commit 6064da6

Browse files
committed
refactor: modals form_history creation
1 parent cbc24e1 commit 6064da6

File tree

12 files changed

+141
-67
lines changed

12 files changed

+141
-67
lines changed

frontend/src/lib/components/modals/create_community/index.svelte

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
2-
import AuthModal from './auth/index.svelte';
3-
import CreateCommunityModal from './create_community/index.svelte';
2+
import ModalAuth from './modal_auth/index.svelte';
3+
import ModalCreateCommunity from './modal_create_community/index.svelte';
44
</script>
55

6-
<AuthModal />
7-
<CreateCommunityModal />
6+
<ModalAuth />
7+
<ModalCreateCommunity />
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<script lang="ts">
2+
import { createModalsStore } from '$lib/stores/modals.svelte';
3+
import type { Nullable } from '$lib/types/shared';
4+
import type { FormsState, FormSubmitData, Forms } from '../types';
5+
import { create_form_history } from '../utils/history.svelte';
6+
import forms from './forms';
7+
8+
type AuthForms = Forms<typeof forms>;
9+
type AuthFormsState = FormsState<typeof forms>;
10+
11+
const form_history = create_form_history<typeof forms>('join');
12+
let current_form = $derived(forms[form_history.history.at(-1) ?? 'join']);
13+
14+
const initial_forms_state = Object.fromEntries(
15+
Object.keys(forms).map((key) => [key, {}])
16+
) as AuthFormsState;
17+
18+
let forms_state = $state<AuthFormsState>(initial_forms_state);
19+
20+
function update_forms_state(form: AuthForms, data: FormSubmitData) {
21+
forms_state[form] = { ...forms_state[form], ...data };
22+
}
23+
24+
function goto_form(form: AuthForms) {
25+
form_history.go_to_form(form);
26+
}
27+
28+
function handle_go_back() {
29+
form_history.go_back();
30+
}
31+
32+
let dialog_element = $state<Nullable<HTMLDialogElement>>(null);
33+
34+
const modalsStore = createModalsStore();
35+
36+
$effect(() => {
37+
if (modalsStore.state.get('auth')) {
38+
dialog_element?.showModal();
39+
} else {
40+
dialog_element?.close();
41+
}
42+
});
43+
</script>
44+
45+
<dialog
46+
class="modal modal-bottom sm:modal-middle"
47+
bind:this={dialog_element}
48+
onclose={() => modalsStore.close('auth')}
49+
>
50+
<div class="modal-box !w-[25rem]">
51+
{#await current_form then Form}
52+
<Form.default {forms_state} {update_forms_state} {goto_form} />
53+
{/await}
54+
{#if form_history.history.length > 1}
55+
<div
56+
class="tooltip tooltip-right absolute left-2.5 top-2.5 flex before:capitalize"
57+
data-tip={form_history.history.at(-2)?.replace('_', ' ')}
58+
>
59+
<button
60+
class="btn btn-square btn-circle btn-ghost btn-sm"
61+
aria-label="Close modal"
62+
onclick={handle_go_back}
63+
>
64+
<coreicons-shape-arrow class="size-5" variant="left"></coreicons-shape-arrow>
65+
</button>
66+
</div>
67+
{/if}
68+
<button
69+
class="btn btn-square btn-circle btn-ghost btn-sm absolute right-2.5 top-2.5"
70+
aria-label="Close modal"
71+
onclick={() => dialog_element?.close()}
72+
>
73+
<coreicons-shape-x class="size-5" variant="no-border"></coreicons-shape-x>
74+
</button>
75+
</div>
76+
<form method="dialog" class="modal-backdrop">
77+
<button>close</button>
78+
</form>
79+
</dialog>

frontend/src/lib/components/modals/create_community/forms/introduction.svelte renamed to frontend/src/lib/components/modals/modal_create_community/forms/introduction.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<script lang="ts">
2+
import type { FormProps } from '../../types';
3+
import forms from '../forms';
4+
5+
let {}: FormProps<typeof forms> = $props();
6+
</script>
7+
18
<div>
29
<div>
310
<h3>Introduce Your Community</h3>

frontend/src/lib/components/modals/auth/index.svelte renamed to frontend/src/lib/components/modals/modal_create_community/index.svelte

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
import type { FormsState, FormSubmitData, Forms } from '../types';
55
import forms from './forms';
66
7-
type AuthForms = Forms<typeof forms>;
8-
type AuthFormsState = FormsState<typeof forms>;
7+
type CForms = Forms<typeof forms>;
8+
type CFormsState = FormsState<typeof forms>;
99
10-
let _form = $state<AuthForms>('join');
11-
let prev_form_history = $state<AuthForms[]>(['join']);
10+
let _form = $state<CForms>('introduction');
11+
let prev_form_history = $state<CForms[]>(['introduction']);
1212
1313
let current_form = $derived(forms[_form]);
1414
1515
const initial_forms_state = Object.fromEntries(
1616
Object.keys(forms).map((key) => [key, {}])
17-
) as AuthFormsState;
17+
) as CFormsState;
1818
19-
let forms_state = $state<AuthFormsState>(initial_forms_state);
19+
let forms_state = $state<CFormsState>(initial_forms_state);
2020
21-
function update_forms_state(form: AuthForms, data: FormSubmitData) {
21+
function update_forms_state(form: CForms, data: FormSubmitData) {
2222
forms_state[form] = { ...forms_state[form], ...data };
2323
}
2424
25-
function goto_form(form: AuthForms) {
25+
function goto_form(form: CForms) {
2626
// if navigating to a form thats already in the history stack,
2727
// truncate the stack upto the most recent occurance of that form
2828
// (avoiding duplicate entiries)
@@ -53,7 +53,7 @@
5353
const modalsStore = createModalsStore();
5454
5555
$effect(() => {
56-
if (modalsStore.state.get('auth')) {
56+
if (modalsStore.state.get('create_community')) {
5757
dialog_element?.showModal();
5858
} else {
5959
dialog_element?.close();
@@ -64,26 +64,12 @@
6464
<dialog
6565
class="modal modal-bottom sm:modal-middle"
6666
bind:this={dialog_element}
67-
onclose={() => modalsStore.close('auth')}
67+
onclose={() => modalsStore.close('create_community')}
6868
>
69-
<div class="modal-box !w-[25rem]">
69+
<div class="modal-box">
7070
{#await current_form then Form}
7171
<Form.default {forms_state} {update_forms_state} {goto_form} />
7272
{/await}
73-
{#if prev_form_history.length > 1}
74-
<div
75-
class="tooltip tooltip-right absolute left-2.5 top-2.5 flex before:capitalize"
76-
data-tip={prev_form_history.at(-2)?.replace('_', ' ')}
77-
>
78-
<button
79-
class="btn btn-square btn-circle btn-ghost btn-sm"
80-
aria-label="Close modal"
81-
onclick={handle_go_back}
82-
>
83-
<coreicons-shape-arrow class="size-5" variant="left"></coreicons-shape-arrow>
84-
</button>
85-
</div>
86-
{/if}
8773
<button
8874
class="btn btn-square btn-circle btn-ghost btn-sm absolute right-2.5 top-2.5"
8975
aria-label="Close modal"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { FormConfig } from '../types';
2+
3+
export function create_form_history<T extends FormConfig>(initial_form: keyof T) {
4+
let history = $state([initial_form]);
5+
6+
function go_to_form(form: keyof T) {
7+
// if navigating to a form thats already in the history stack,
8+
// truncate the stack upto the most recent occurance of that form
9+
// (avoiding duplicate entiries)
10+
const form_index = history.findIndex((entry) => entry === form);
11+
if (form_index > -1) {
12+
// if exists
13+
history = history.slice(0, form_index + 1);
14+
} else {
15+
// otherwise, push current form to prev_form_history
16+
history.push(form);
17+
}
18+
return history.at(-1);
19+
}
20+
21+
function go_back() {
22+
if (history.length > 1) {
23+
// remove current form from history stack
24+
history.pop();
25+
}
26+
return history.at(-1);
27+
}
28+
29+
return {
30+
get history() {
31+
return history;
32+
},
33+
go_to_form,
34+
go_back
35+
};
36+
}

frontend/src/styles/smiz.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@
7878
}
7979

8080
[data-smiz-modal-overlay='hidden'] {
81-
background-color: oklch(var(--b3) / 0);
81+
/* background-color: oklch(var(--b3) / 0); */
82+
background-color: #0000;
8283
}
8384

8485
[data-smiz-modal-overlay='visible'] {
85-
background-color: oklch(var(--b3) / 0.55);
86+
/* background-color: oklch(var(--b3) / 0.55); */
87+
background-color: #0006;
8688
}
8789

8890
[data-smiz-modal-content] {

0 commit comments

Comments
 (0)