Skip to content

Commit

Permalink
fix: create new data formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Volubyl committed Feb 2, 2023
1 parent 74efb68 commit 46172a5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
4 changes: 2 additions & 2 deletions client/src/definitions/datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export type DatasetFormInitial = Omit<Dataset, "id">;

export type DatasetFormData = Omit<
Dataset,
"id" | "catalogRecord" | "headlines"
> & { organizationSiret: string };
"id" | "catalogRecord" | "headlines" | "formats"
> & { organizationSiret: string; formats: Partial<DataFormat>[] };

export type DatasetCreateData = Omit<DatasetFormData, "tags" | "formats"> & {
tagIds: string[];
Expand Down
12 changes: 1 addition & 11 deletions client/src/lib/components/DatasetForm/DatasetForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -179,7 +169,6 @@
const data: DatasetFormData = {
...values,
formats,
producerEmail,
contactEmails,
lastUpdatedAt,
Expand Down Expand Up @@ -300,6 +289,7 @@
error={typeof $errors.formats === "string" ? $errors.formats : ""}
on:addItem={handleAddDataFormat}
on:change={handleDataFormatChanges}
bind:selectedFormatOptions={initialValues.formats}
/>
<InputField
name="technicalSource"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
export let formatOptions: DataFormat[];
export let error: string;
let selectedFormatOptions: Partial<DataFormat>[] = [];
export let selectedFormatOptions: Partial<DataFormat>[] = [];
const handleSelectFormat = (e: CustomEvent<SelectOption<number>>) => {
const selectedOption = transoformSelectOptionToDataFormat(e.detail);
Expand Down
32 changes: 29 additions & 3 deletions client/src/routes/(app)/contribuer/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
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";
let loading = false;
let formHasBeenTouched = false;
let freshDataFormat: DataFormat[];
export let data: PageData;
$: ({ catalog, tags, licenses, filtersInfo, formats } = data);
Expand All @@ -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)) {
Expand All @@ -52,12 +76,14 @@
}
};
const handleCreateDataFormat = async (e: CustomEvent<string>) => {
await postDataFormat({
const dataformat = await postDataFormat({
fetch,
apiToken: $apiToken,
value: e.detail,
});
freshDataFormat = [...freshDataFormat, dataformat];
formats = await getDataFormats({ fetch, apiToken: $apiToken });
};
</script>
Expand Down
29 changes: 26 additions & 3 deletions client/src/routes/(app)/fiches/[id]/edit/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
getDataFormats,
postDataFormat,
} from "src/lib/repositories/dataformat";
import type { DataFormat } from "src/definitions/dataformat";
export let data: PageData;
Expand All @@ -24,13 +25,17 @@
let formHasbeenTouched = false;
let freshDataFormat: DataFormat[] = [];
const handleCreateDataFormat = async (e: CustomEvent<string>) => {
await postDataFormat({
const dataFormat = await postDataFormat({
fetch,
apiToken: $apiToken,
value: e.detail,
});
freshDataFormat = [...freshDataFormat, dataFormat];
formats = await getDataFormats({ fetch, apiToken: $apiToken });
};
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down

0 comments on commit 46172a5

Please sign in to comment.