Skip to content

Commit 5a660c8

Browse files
committed
feat: implement submitting domains
1 parent 0820fd0 commit 5a660c8

File tree

11 files changed

+113
-81
lines changed

11 files changed

+113
-81
lines changed

src/app.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ declare global {
44
namespace App {
55
// interface Error {}
66
interface Locals {
7-
status:
7+
loginStatus:
88
| 'assumeLoggedIn'
99
| 'assumeLoggedOut'
1010
| 'accountError'
1111
| 'accountCreated'
1212
| 'accountDeleted';
1313
accountMessage: string;
14+
15+
actionResult: string;
1416
}
1517
// interface PageData {}
1618
// interface PageState {}

src/lib/components/custom/MobileMenu.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
open = false;
1919
});
2020
21-
let { status } = $props();
21+
let { loginStatus: status } = $props();
2222
</script>
2323

2424
<Sheet.Root bind:open>
@@ -49,7 +49,7 @@
4949
{/if}
5050
</span>
5151
<div class="min-[400px]:justify-self-end">
52-
<SignInButton {status} />
52+
<SignInButton loginStatus={status} />
5353
</div>
5454
</div>
5555
<div class="grid gap-4 min-[400px]:grid-cols-2">

src/lib/components/custom/SignInButton.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
import RiUserLine from '~icons/ri/user-line';
55
6-
let { status } = $props();
6+
let { loginStatus: status } = $props();
77
</script>
88

99
{#if status !== 'assumeLoggedIn'}

src/routes/(nosearch)/+layout.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
</a>
2222
<div class="hidden md:contents">
2323
<Button variant="link" href="https://opencollective.com/mwmbl" class="px-2">Donate</Button>
24-
<SignInButton status={data.status} />
24+
<SignInButton loginStatus={data.loginStatus} />
2525
</div>
2626
<div class="contents md:hidden">
27-
<MobileMenu status={data.status} />
27+
<MobileMenu loginStatus={data.loginStatus} />
2828
</div>
2929
</header>
3030

src/routes/(nosearch)/account/+page.server.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export const actions: Actions = {
3535
maxAge: 60 * 60 * 24 * 30 // 30 days
3636
});
3737

38-
locals.status = 'assumeLoggedIn';
38+
locals.loginStatus = 'assumeLoggedIn';
3939
} else {
40-
locals.status = 'accountError';
40+
locals.loginStatus = 'accountError';
4141
locals.accountMessage = 'Error logging in.';
4242
}
4343
},
@@ -53,17 +53,17 @@ export const actions: Actions = {
5353
});
5454
const json = await res.json();
5555
if (res.ok) {
56-
locals.status = 'accountCreated';
56+
locals.loginStatus = 'accountCreated';
5757
locals.accountMessage = json.message;
5858
} else {
59-
locals.status = 'accountError';
59+
locals.loginStatus = 'accountError';
6060
locals.accountMessage = json.message;
6161
}
6262
},
6363
logout: async ({ cookies, locals }) => {
6464
cookies.delete('refreshToken', { path: '/' });
6565
cookies.delete('accessToken', { path: '/' });
66-
locals.status = 'assumeLoggedOut';
66+
locals.loginStatus = 'assumeLoggedOut';
6767
locals.accountMessage = 'Logged out.';
6868
},
6969
deleteUser: async ({ cookies, locals }) => {
@@ -80,10 +80,10 @@ export const actions: Actions = {
8080
cookies.delete('refreshToken', { path: '/' });
8181
cookies.delete('accessToken', { path: '/' });
8282

83-
locals.status = 'accountDeleted';
83+
locals.loginStatus = 'accountDeleted';
8484
locals.accountMessage = 'Your account has been deleted.';
8585
} else {
86-
locals.status = 'accountError';
86+
locals.loginStatus = 'accountError';
8787
locals.accountMessage = res.statusText;
8888
}
8989
}

src/routes/(nosearch)/account/+page.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@
1313

1414
<main class="flex w-full max-w-2xl flex-col gap-2 self-center px-6">
1515
<h2 class="-mx-2 text-3xl">
16-
{#if data.status !== 'assumeLoggedIn'}
16+
{#if data.loginStatus !== 'assumeLoggedIn'}
1717
Sign in
1818
{:else}
1919
Logged in as {data.username}
2020
{/if}
2121
</h2>
2222
<hr class="my-2" />
23-
{#if data.status === 'accountError'}
23+
{#if data.loginStatus === 'accountError'}
2424
<Card.Root class="p-4 outline outline-red-100 dark:outline-red-900">
2525
{data.accountMessage.replaceAll('%20', ' ')}
2626
</Card.Root>
27-
{:else if data.status === 'accountCreated'}
27+
{:else if data.loginStatus === 'accountCreated'}
2828
<Card.Root class="p-4 outline outline-green-100 dark:outline-green-900">
2929
{data.accountMessage.replaceAll('%20', ' ')}
3030
</Card.Root>
31-
{:else if data.status === 'accountDeleted'}
31+
{:else if data.loginStatus === 'accountDeleted'}
3232
<Card.Root class="p-4 outline outline-green-100 dark:outline-green-900">
3333
{data.accountMessage.replaceAll('%20', ' ')}
3434
</Card.Root>
3535
{/if}
36-
{#if data.status !== 'assumeLoggedIn'}
36+
{#if data.loginStatus !== 'assumeLoggedIn'}
3737
<Tabs.Root value="Log in" class="Log in">
3838
<Tabs.List class="w-full rounded-2xl">
3939
<Tabs.Trigger value="Log in" class="w-full rounded-xl">Log in</Tabs.Trigger>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { Actions } from '@sveltejs/kit';
2+
3+
const pageSize = 500;
4+
5+
export type SubmissionsResult = {
6+
items: Array<{
7+
id: number;
8+
name: string;
9+
submitted_by: number;
10+
submitted_on: string;
11+
status: string;
12+
rejection_reason: string;
13+
rejection_detail: string;
14+
}>;
15+
count: number;
16+
};
17+
18+
export async function load({ fetch, url, locals }) {
19+
const page = Number(url.searchParams.get('page'));
20+
const offset = page * pageSize;
21+
const response = await fetch(
22+
`https://api.mwmbl.org/api/v1/platform/domain-submissions?limit=${pageSize}&offset=${offset}`
23+
);
24+
const submissions: SubmissionsResult = await response.json();
25+
26+
return {
27+
submissions,
28+
page,
29+
maxPage: Math.floor(submissions.count / pageSize),
30+
status: locals.actionResult
31+
};
32+
}
33+
export const actions: Actions = {
34+
submitDomain: async ({ request, cookies, locals }) => {
35+
const data = await request.formData();
36+
const res = await fetch(
37+
'https://api.mwmbl.org/api/v1/platform/domain-submissions/?domain=' + data.get('domain'),
38+
{
39+
method: 'POST',
40+
headers: {
41+
Authorization: 'Bearer ' + cookies.get('accessToken')
42+
}
43+
}
44+
);
45+
if (res.ok) {
46+
locals.actionResult = 'domainSubmitted';
47+
} else {
48+
locals.actionResult = 'domainSubmissionError';
49+
}
50+
}
51+
};

src/routes/(nosearch)/domain-submissions/+page.svelte

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import RiArrowDropRightLine from '~icons/ri/arrow-drop-right-line';
77
import RiLinksLine from '~icons/ri/links-line';
88
import RiLoader2Line from '~icons/ri/loader-2-line';
9-
import type { SubmissionsResult } from './+page.js';
9+
import type { SubmissionsResult } from './+page.server.js';
10+
import SignInButton from '@/components/custom/SignInButton.svelte';
11+
import * as Card from '@/components/ui/card';
1012
1113
let { data } = $props();
1214
@@ -35,9 +37,6 @@
3537
clearTimeout(debounceTimer);
3638
debounceTimer = setTimeout(callback, ms);
3739
}
38-
39-
// TODO implement domain submission (server actions)
40-
function submitDomain() {}
4140
</script>
4241

4342
{#snippet submissions(submissions: SubmissionsResult)}
@@ -74,6 +73,15 @@
7473
<main class="flex w-full max-w-4xl flex-col gap-2 self-center px-6">
7574
<h2 class="-mx-2 text-3xl">Domain submissions</h2>
7675
<hr class="my-2" />
76+
{#if data.status === 'domainSubmissionError'}
77+
<Card.Root class="p-4 outline outline-red-100 dark:outline-red-900">
78+
Error submitting domain. Please try again and file an issue if it does not work.
79+
</Card.Root>
80+
{:else if data.status === 'domainSubmitted'}
81+
<Card.Root class="p-4 outline outline-green-100 dark:outline-green-900">
82+
Domain submitted successfully!
83+
</Card.Root>
84+
{/if}
7785
<div class="w-full rounded-2xl bg-white p-4">
7886
<h3 class="text-2xl">Submit domain</h3>
7987
<hr class="my-2" />
@@ -91,31 +99,34 @@
9199
{#await submissionsForInput}
92100
<RiLoader2Line class="h-8 w-8 animate-spin" />
93101
{:then submissionsForInput}
94-
{#if submissionsForInput.count > 0}
95-
<div class="mb-4">Found {submissionsForInput.count} pre-existing submission(s)</div>
96-
{@render submissions(submissionsForInput)}
97-
{#if domainInput.length > 0}
98-
<Button
99-
title="unimplemented"
100-
variant="secondary"
101-
class="mt-6 flex w-full flex-row items-center gap-2 dark:bg-muted"
102-
>
103-
<RiLinksLine class="min-h-5 min-w-5 text-black dark:text-white" />
104-
Resubmit Domain
105-
</Button>
106-
{/if}
107-
{:else}
108-
Found no pre-existing submission(s)
109-
{#if domainInput.length > 0}
110-
<Button
111-
title="unimplemented"
112-
variant="secondary"
113-
class="mt-6 flex w-full flex-row items-center gap-2 dark:bg-muted"
114-
>
115-
<RiLinksLine class="min-h-5 min-w-5 text-black dark:text-white" />
116-
Submit Domain
117-
</Button>
102+
{#if domainInput.length > 0}
103+
{#if submissionsForInput.count > 0}
104+
<div class="mb-4">Found {submissionsForInput.count} pre-existing submission(s)</div>
105+
{@render submissions(submissionsForInput)}
106+
{:else}
107+
Found no pre-existing submission(s)
118108
{/if}
109+
<form method="POST" action="?/submitDomain">
110+
{#if data.loginStatus == 'assumeLoggedIn'}
111+
<Button
112+
variant="secondary"
113+
class="mt-6 flex w-full flex-row items-center gap-2 dark:bg-muted"
114+
type="submit"
115+
>
116+
<RiLinksLine class="min-h-5 min-w-5 text-black dark:text-white" />
117+
{#if submissionsForInput.count > 0}
118+
Resubmit Domain
119+
{:else}
120+
Submit Domain
121+
{/if}
122+
</Button>
123+
{:else}
124+
<div class="mt-6 flex w-full flex-row items-center gap-2 dark:bg-muted">
125+
Authenticate to submit
126+
<SignInButton loginStatus={data.loginStatus} />
127+
</div>
128+
{/if}
129+
</form>
119130
{/if}
120131
{/await}
121132
</div>

src/routes/(nosearch)/domain-submissions/+page.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/routes/+layout.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export async function load({ locals }) {
22
return {
3-
status: locals.status
3+
loginStatus: locals.loginStatus
44
};
55
}

0 commit comments

Comments
 (0)