Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/lib/api/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type EventCreateRequestData = {
name: string;
choices: string[];
electoral_system: string;
allow_registration?: boolean;
allow_voting?: boolean;
};

export type EventStatus = 'RE' | 'VO' | 'CL';
Expand All @@ -12,16 +14,23 @@ export type EventResponseData = {
id: number;
name: string;
choices: string[];
status: EventStatus;
allow_registration: boolean;
allow_voting: boolean;
electoral_system: string;
share_token: string;
show_results: boolean;
closed: null | string;
};

export type EventCreateResponseData = EventResponseData & {
host_token: string;
};

export type EventUpdateBody = {
allow_registration?: boolean;
allow_voting?: boolean;
};

export type BallotResponseData = {
id: number;
voter_name: string;
Expand All @@ -47,7 +56,9 @@ export class EventsAPI extends BaseAPI {
};

closeEvent = async (eventID: number, host_token: string) => {
return this.post(`/${eventID}/close`, null, { 'X-API-Key': host_token });
return this.post(`/${eventID}/close`, null, { 'X-API-Key': host_token }) as Promise<{
closed: string;
}>;
};

openEvent = async (eventID: number, host_token: string) => {
Expand All @@ -72,14 +83,10 @@ export class EventsAPI extends BaseAPI {
}) as Promise<BallotCreateResponseData>;
};

updateStatus = async (eventID: number, token: string, status: EventStatus) => {
return this.patch(
`/${eventID}/update-status`,
{ status },
{
'X-API-Key': token
}
);
update = async (eventID: number, token: string, body: EventUpdateBody) => {
return this.patch(`/${eventID}/update`, body, {
'X-API-Key': token
}) as Promise<EventResponseData>;
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/EventForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
const response = await api.createEvent({
name: eventName,
choices: dishes,
electoral_system: votingSystem
electoral_system: votingSystem,
allow_registration: true
});

storage.saveEvent(response.id, response.name, response.host_token);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/SavedBallotList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
eventName: response.name,
eventID: localBallots[keyAsNumber].eventID,
id: keyAsNumber,
state: response.status == 'CL' ? 'closed' : 'open'
state: response.closed ? 'closed' : 'open'
});
} catch (e) {
const status = (e as APIError).status;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/SavedEventList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
events.push({
name: localEvents[keyAsNumber].name,
id: keyAsNumber,
state: event.status === 'CL' ? 'closed' : 'open'
state: event.closed ? 'closed' : 'open'
});
} catch (e) {
const status = (e as APIError).status;
Expand Down
8 changes: 4 additions & 4 deletions src/routes/event/[id=eventID]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
</script>

<div class="flex min-h-dvh flex-col justify-between gap-8 p-4">
{#if event?.status == 'RE'}
{#if event?.closed}
<Results bind:event {ballots} />
{:else if event?.allow_registration == true && event?.allow_voting == false}
<Registration bind:event {ballots} />
{:else if event?.status == 'VO'}
{:else if event?.allow_voting == true}
<Voting bind:event {ballots} />
{:else if event?.status == 'CL'}
<Results bind:event {ballots} />
{:else}
Loading...
{/if}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
</script>

<div class="min-h-dvh p-4">
{#if ballot && event?.status == 'RE'}
{#if ballot && event?.allow_registration == true && event?.allow_voting == false}
<Heading tag="h1" class="mb-8">{event.name}</Heading>
<P>
Thanks for registering for <b>{event.name}</b>. Sit tight and wait for the event to start.
</P>
{:else if ballot && event?.status == 'VO'}
{:else if ballot && event?.allow_registration == false && event?.allow_voting == true}
<Voting {event} bind:ballot />
{:else if ballot && event?.status == 'CL'}
{:else if ballot && event?.closed}
<Results {event} {ballot} />
{:else}
Loading...
Expand Down
8 changes: 5 additions & 3 deletions src/routes/event/[id=eventID]/components/registration.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Button, Heading, P } from 'flowbite-svelte';
import { ClipboardCheckOutline, ClipboardCleanOutline } from 'flowbite-svelte-icons';

const {
let {
event = $bindable(),
ballots
}: {
Expand Down Expand Up @@ -39,8 +39,10 @@

const beginVote = async () => {
const api = new EventsAPI();
await api.updateStatus(event.id, storage.getEvent(event.id).token, 'VO');
event.status = 'VO';
event = await api.update(event.id, storage.getEvent(event.id).token, {
allow_registration: false,
allow_voting: true
});
};
</script>

Expand Down
6 changes: 3 additions & 3 deletions src/routes/event/[id=eventID]/components/voting.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { getStorageContext } from '$lib/storage/storage';
import { Button, Heading, P } from 'flowbite-svelte';

const {
let {
event = $bindable(),
ballots
}: { event: EventResponseData; ballots: BallotResponseData[] | null } = $props();
Expand All @@ -23,8 +23,8 @@

const closeVoting = async () => {
const api = new EventsAPI();
await api.updateStatus(event.id, storage.getEvent(event.id).token, 'CL');
event.status = 'CL';
const response = await api.closeEvent(event.id, storage.getEvent(event.id).token);
event.closed = response.closed;
};
</script>

Expand Down
Loading