diff --git a/client/src/definitions/datasets.ts b/client/src/definitions/datasets.ts index 85e03b81..23f0c8aa 100644 --- a/client/src/definitions/datasets.ts +++ b/client/src/definitions/datasets.ts @@ -47,8 +47,8 @@ export type DatasetFormInitial = Omit; export type DatasetFormData = Omit< Dataset, - "id" | "catalogRecord" | "headlines" -> & { organizationSiret: string }; + "id" | "catalogRecord" | "headlines" | "formats" +> & { organizationSiret: string; formats: Partial[] }; export type DatasetCreateData = Omit & { tagIds: string[]; diff --git a/client/src/lib/components/DatasetForm/DatasetForm.svelte b/client/src/lib/components/DatasetForm/DatasetForm.svelte index 90da6354..62740abe 100644 --- a/client/src/lib/components/DatasetForm/DatasetForm.svelte +++ b/client/src/lib/components/DatasetForm/DatasetForm.svelte @@ -156,16 +156,6 @@ const url = values.url ? values.url : null; const license = values.license ? values.license : null; - const formats = values.formats - .filter((item) => { - if (!item.id || !item.name) { - return; - } - - return item; - }) - .filter((item) => item) as DataFormat[]; - let extraFieldValues: ExtraFieldValue[] = []; values.extraFieldValues.forEach((value, index) => { @@ -179,7 +169,6 @@ const data: DatasetFormData = { ...values, - formats, producerEmail, contactEmails, lastUpdatedAt, @@ -300,6 +289,7 @@ error={typeof $errors.formats === "string" ? $errors.formats : ""} on:addItem={handleAddDataFormat} on:change={handleDataFormatChanges} + bind:selectedFormatOptions={initialValues.formats} /> [] = []; + export let selectedFormatOptions: Partial[] = []; const handleSelectFormat = (e: CustomEvent>) => { const selectedOption = transoformSelectOptionToDataFormat(e.detail); diff --git a/client/src/routes/(app)/contribuer/+page.svelte b/client/src/routes/(app)/contribuer/+page.svelte index 6c01c15c..f4489bcd 100644 --- a/client/src/routes/(app)/contribuer/+page.svelte +++ b/client/src/routes/(app)/contribuer/+page.svelte @@ -14,6 +14,8 @@ getDataFormats, postDataFormat, } from "src/lib/repositories/dataformat"; + import type { DataFormat } from "src/definitions/dataformat"; + import Alert from "src/lib/components/Alert/Alert.svelte"; let modalControlId = "confirm-stop-contributing-modal"; @@ -21,6 +23,8 @@ let formHasBeenTouched = false; + let freshDataFormat: DataFormat[]; + export let data: PageData; $: ({ catalog, tags, licenses, filtersInfo, formats } = data); @@ -29,11 +33,31 @@ try { loading = true; const tagIds = event.detail.tags.map((item) => item.id); - const formatIds = event.detail.formats.map((item) => item.id); + + const mergedDataFormatsIds = event.detail.formats.reduce((prev, next) => { + if (!next.name) { + return prev; + } + + if (!next.id) { + const foundItemId = freshDataFormat.find( + (item) => item.name === next.name + )?.id; + + if (!foundItemId) { + return prev; + } + + return [...prev, foundItemId]; + } + + return [...prev, next.id]; + }, [] as number[]); + const dataset = await createDataset({ fetch, apiToken: $apiToken, - data: { ...event.detail, tagIds, formatIds }, + data: { ...event.detail, tagIds, formatIds: mergedDataFormatsIds }, }); if (Maybe.Some(dataset)) { @@ -52,12 +76,14 @@ } }; const handleCreateDataFormat = async (e: CustomEvent) => { - await postDataFormat({ + const dataformat = await postDataFormat({ fetch, apiToken: $apiToken, value: e.detail, }); + freshDataFormat = [...freshDataFormat, dataformat]; + formats = await getDataFormats({ fetch, apiToken: $apiToken }); }; diff --git a/client/src/routes/(app)/fiches/[id]/edit/+page.svelte b/client/src/routes/(app)/fiches/[id]/edit/+page.svelte index 06aae21e..6c3987c4 100644 --- a/client/src/routes/(app)/fiches/[id]/edit/+page.svelte +++ b/client/src/routes/(app)/fiches/[id]/edit/+page.svelte @@ -13,6 +13,7 @@ getDataFormats, postDataFormat, } from "src/lib/repositories/dataformat"; + import type { DataFormat } from "src/definitions/dataformat"; export let data: PageData; @@ -24,13 +25,17 @@ let formHasbeenTouched = false; + let freshDataFormat: DataFormat[] = []; + const handleCreateDataFormat = async (e: CustomEvent) => { - await postDataFormat({ + const dataFormat = await postDataFormat({ fetch, apiToken: $apiToken, value: e.detail, }); + freshDataFormat = [...freshDataFormat, dataFormat]; + formats = await getDataFormats({ fetch, apiToken: $apiToken }); }; @@ -40,7 +45,25 @@ } const tagIds = event.detail.tags.map((item) => item.id); - const fromatIds = event.detail.formats.map((item) => item.id); + const mergedDataFormatsIds = event.detail.formats.reduce((prev, next) => { + if (!next.name) { + return prev; + } + + if (!next.id) { + const foundItemId = freshDataFormat.find( + (item) => item.name === next.name + )?.id; + + if (!foundItemId) { + return prev; + } + + return [...prev, foundItemId]; + } + + return [...prev, next.id]; + }, [] as number[]); try { loading = true; @@ -49,7 +72,7 @@ fetch, apiToken: $apiToken, id: dataset.id, - data: { ...event.detail, tagIds, formatIds: fromatIds }, + data: { ...event.detail, tagIds, formatIds: mergedDataFormatsIds }, }); if (Maybe.Some(updatedDataset)) {