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
34 changes: 31 additions & 3 deletions src/routes/admin/panel/clubs/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
} from 'flowbite-svelte-icons';
import ExternalLink from '$components/ExternalLink.svelte';
import EditableClubForm from './EditableClubForm.svelte';

import type { IClub } from '$api/page_data/clubs/types';
import type { WithoutID } from '$lib/types/basicTypes';
import { getNotificationContext } from '$components/NotificationProvider.svelte';
const notificationContext = getNotificationContext();
interface Props {
data: PageData;
}
Expand Down Expand Up @@ -51,9 +54,34 @@
{#snippet renderItems({ items, openCreateForm, updateAll })}
<div class="mb-8 flex justify-center gap-8">
<Button color="alternative" on:click={openCreateForm}>Add Club</Button>
<AutoImportClubsButton onSubmit={updateAll} />
</div>
<AutoImportClubsButton
onSubmit={(importedClubs, overwrite) => {
if (overwrite) {
updateAll(importedClubs);
notificationContext.show(
'Imported ' + importedClubs.length + ' clubs (Overwrote existing)',
'success'
);
} else {
const existing = items.map((i) => i.item);
const newClubs = [];
for (let checkedItem = 0; checkedItem < importedClubs.length; checkedItem++) {
if (
!existing.some(
(ext) =>
ext.name.toLowerCase() === importedClubs[checkedItem]?.name.toLowerCase()
)
) {
newClubs.push(importedClubs[checkedItem]);
}
}
updateAll([...existing, ...newClubs]);

notificationContext.show('Added ' + newClubs.length + ' clubs', 'success');
}
}}
/>
</div>
<Table striped shadow class="min-w-[1024px] table-fixed">
<TableHead>
<TableHeadCell>Name</TableHeadCell>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/admin/panel/clubs/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const load = (async (event) => {
const clubs = (await (
await event.fetch('/api/v2/page_data/clubs/edit')
).json()) as GetAllResults<IClub>;

clubs.results.sort((a, b) => a.name.localeCompare(b.name));
return clubs;
}) satisfies PageLoad;
16 changes: 7 additions & 9 deletions src/routes/admin/panel/clubs/AutoImportClubsButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import ValidatedInput from '$components/form/ValidatedInput.svelte';
import ValidatedTextarea from '$components/form/ValidatedTextarea.svelte';
import ExternalLink from '$components/ExternalLink.svelte';
import { GradientButton, Helper, Modal, Tooltip } from 'flowbite-svelte';
import { GradientButton, Helper, Modal, Tooltip, Checkbox } from 'flowbite-svelte';
import { InfoCircleOutline } from 'flowbite-svelte-icons';
import { string } from 'yup';
import Papa from 'papaparse';
import { getNotificationContext } from '$components/NotificationProvider.svelte';
import type { IClub } from '$api/page_data/clubs/types';
import type { WithoutID } from '$lib/types/basicTypes';
/* SAMPLE DATA:
Expand All @@ -15,15 +14,13 @@ https://lhs.cx/old-clubs
*/

interface Props {
onSubmit: (clubs: WithoutID<IClub>[]) => void;
onSubmit: (clubs: WithoutID<IClub>[], overwrite: boolean) => void;
}

let { onSubmit: submit }: Props = $props();

const notificationContext = getNotificationContext();

let modalOpen = $state(false);

let overwriteClubs = $state(false);
let startInput: ValidatedInput | undefined = $state();
let endInput: ValidatedInput | undefined = $state();
let csvInput: ValidatedTextarea | undefined = $state();
Expand Down Expand Up @@ -83,8 +80,7 @@ https://lhs.cx/old-clubs
});
},
complete() {
notificationContext.show('Imported ' + records.length + ' clubs', 'success');
submit(records);
submit(records, overwriteClubs);
modalOpen = false;
},
});
Expand Down Expand Up @@ -143,6 +139,9 @@ https://lhs.cx/old-clubs
/>
</div>
</div>
<Checkbox bind:checked={overwriteClubs} id="overwrite">
<span class="text-amber-500 dark:text-amber-600">Overwrite existing clubs</span>
</Checkbox>
<div class="w-full">
<ValidatedTextarea
bind:this={csvInput}
Expand All @@ -160,7 +159,6 @@ https://lhs.cx/old-clubs
}}
/>
</div>
<Helper>Note: This will overwrite everything already in the table.</Helper>
<GradientButton type="submit" color="purpleToPink">Import</GradientButton>
</form>
</Modal>
3 changes: 2 additions & 1 deletion src/routes/api/v2/page_data/clubs/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { RequestHandler } from './$types';
import { Club } from './model';

export const GET: RequestHandler = async ({}) => {
const docs = await Club.find().sort({ name: 'asc' }).exec();
const docs = await Club.find().exec();
docs.sort((a, b) => a.name.localeCompare(b.name));

return json({
clubs: docs.map((doc) => doc.toObject()),
Expand Down
Loading