diff --git a/src/routes/admin/panel/clubs/+page.svelte b/src/routes/admin/panel/clubs/+page.svelte index 85203715..f8964bed 100644 --- a/src/routes/admin/panel/clubs/+page.svelte +++ b/src/routes/admin/panel/clubs/+page.svelte @@ -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; } @@ -51,9 +54,34 @@ {#snippet renderItems({ items, openCreateForm, updateAll })}
- -
+ { + 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'); + } + }} + /> + Name diff --git a/src/routes/admin/panel/clubs/+page.ts b/src/routes/admin/panel/clubs/+page.ts index db0b659a..01d0ffa2 100644 --- a/src/routes/admin/panel/clubs/+page.ts +++ b/src/routes/admin/panel/clubs/+page.ts @@ -6,6 +6,6 @@ export const load = (async (event) => { const clubs = (await ( await event.fetch('/api/v2/page_data/clubs/edit') ).json()) as GetAllResults; - + clubs.results.sort((a, b) => a.name.localeCompare(b.name)); return clubs; }) satisfies PageLoad; diff --git a/src/routes/admin/panel/clubs/AutoImportClubsButton.svelte b/src/routes/admin/panel/clubs/AutoImportClubsButton.svelte index 710788d9..58fc0b08 100644 --- a/src/routes/admin/panel/clubs/AutoImportClubsButton.svelte +++ b/src/routes/admin/panel/clubs/AutoImportClubsButton.svelte @@ -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: @@ -15,15 +14,13 @@ https://lhs.cx/old-clubs */ interface Props { - onSubmit: (clubs: WithoutID[]) => void; + onSubmit: (clubs: WithoutID[], 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(); @@ -83,8 +80,7 @@ https://lhs.cx/old-clubs }); }, complete() { - notificationContext.show('Imported ' + records.length + ' clubs', 'success'); - submit(records); + submit(records, overwriteClubs); modalOpen = false; }, }); @@ -143,6 +139,9 @@ https://lhs.cx/old-clubs /> + + Overwrite existing clubs +
- Note: This will overwrite everything already in the table. Import diff --git a/src/routes/api/v2/page_data/clubs/+server.ts b/src/routes/api/v2/page_data/clubs/+server.ts index 27f3b693..4520980d 100644 --- a/src/routes/api/v2/page_data/clubs/+server.ts +++ b/src/routes/api/v2/page_data/clubs/+server.ts @@ -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()),