Skip to content

Commit

Permalink
fix: Problème des espaces dans titre ou description d'une table #184
Browse files Browse the repository at this point in the history
  • Loading branch information
pprev94 committed Jun 12, 2024
1 parent 527fe60 commit 547cd33
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
38 changes: 20 additions & 18 deletions assets/entrepot/pages/service/wfs/WfsServiceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,31 @@ import AdditionalInfo from "../metadatas/AdditionalInfo";
import Description from "../metadatas/Description";
import UploadMDFile from "../metadatas/UploadMDFile";
import TableInfosForm from "./TablesInfoForm";
import { trimObject } from "../../../../utils";

// Ajout du nom natif et trim sur les mots cles
const formatTablesInfos = (table_infos: Record<string, WfsTableInfos>) => {
const tInfos: object[] = [];
// Ajout du nom natif sur les tables et suppression des mots clés en doublon
const formatTablesInfos = (table_infos: Record<string, WfsTableInfos>): WfsTableInfos[] => {
const tInfos: WfsTableInfos[] = [];
for (const [name, infos] of Object.entries(table_infos)) {
tInfos.push({
native_name: name,
...infos,
});
const i = { native_name: name, ...infos };
i.keywords = Array.from(new Set(i.keywords)); // Suppression des doublons
tInfos.push(i);
}
return tInfos;
};

const createRequestBody = (formValues: WfsServiceFormValuesType) => {
// Nettoyage => trim sur toutes les chaines
const values = JSON.parse(
JSON.stringify(formValues, (key, value) => {
return typeof value === "string" ? value.trim() : value;
})
);
values.table_infos = formatTablesInfos(values.table_infos);
const values = trimObject(formValues) as WfsServiceFormValuesType;

values.free_keywords = Array.from(new Set(values.free_keywords)); // Suppression des doublons
values.table_infos = formatTablesInfos(values.table_infos as Record<string, WfsTableInfos>);
return values;
};

export type WfsServiceFormValuesType = ServiceFormValuesBaseType & {
selected_tables?: string[];
table_infos?: Record<string, WfsTableInfos>;
table_infos?: Record<string, WfsTableInfos> | WfsTableInfos[];
};

/**
Expand Down Expand Up @@ -188,8 +186,8 @@ const WfsServiceForm: FC<WfsServiceFormProps> = ({ datastoreId, vectorDbId, offe
selectedTableNamesList.forEach((table) => {
table_schemas[table] = yup.object({
public_name: yup.string().default(table),
title: yup.string().required(`Le titre de la table ${table} est obligatoire`),
description: yup.string().required(`Le résumé du contenu de la table ${table} est obligatoire`),
title: yup.string().trim(t("trimmed_error")).strict(true).required(`Le titre de la table ${table} est obligatoire`),
description: yup.string().trim(t("trimmed_error")).strict(true).required(`Le résumé du contenu de la table ${table} est obligatoire`),
keywords: yup.array().of(yup.string()),
});
});
Expand Down Expand Up @@ -226,11 +224,12 @@ const WfsServiceForm: FC<WfsServiceFormProps> = ({ datastoreId, vectorDbId, offe
formState: { errors },
getValues: getFormValues,
trigger,
watch,
} = form;

/*useEffect(() => {
useEffect(() => {
watch((value, { name, type }) => console.log(value, name, type));
}, [watch]); */
}, [watch]);

const selectedTableNamesList: string[] | undefined = useWatch({
control: form.control,
Expand Down Expand Up @@ -386,6 +385,7 @@ export const { i18n } = declareComponentKeys<
| "publish.in_progress"
| "modify.in_progress"
| "back_to_data_list"
| "trimmed_error"
>()({
WfsServiceForm,
});
Expand Down Expand Up @@ -418,6 +418,7 @@ export const WfsServiceFormFrTranslations: Translations<"fr">["WfsServiceForm"]
"publish.in_progress": "Création du service WFS en cours",
"modify.in_progress": "Modification des informations du service WFS en cours",
back_to_data_list: "Retour à mes données",
trimmed_error: "La chaîne de caractères ne doit contenir aucun espace en début et fin",
};

export const WfsServiceFormEnTranslations: Translations<"en">["WfsServiceForm"] = {
Expand All @@ -433,4 +434,5 @@ export const WfsServiceFormEnTranslations: Translations<"en">["WfsServiceForm"]
"publish.in_progress": undefined,
"modify.in_progress": undefined,
back_to_data_list: undefined,
trimmed_error: undefined,
};
29 changes: 29 additions & 0 deletions assets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@ const formatDateFromISO = (isoDateString: string): string => {
const getArrayRange = (start: number, stop: number, step: number = 1): number[] =>
Array.from({ length: (stop - start) / step + 1 }, (_, index) => start + index * step);

const trimObject = (obj: object): object => {
if (typeof obj !== "object" || obj === null) {
return obj;
}

if (Array.isArray(obj)) {
return obj.map((elem) => {
if (typeof elem === "string") {
return elem.trim();
}
return trimObject(elem);
});
}

const newObject = Object.keys(obj).reduce((acc, key) => {
let value = obj[key];

if (typeof value === "string") {
value = value.trim();
} else if (typeof value === "object") {
value = trimObject(value);
}
return { ...acc, [key]: value };
}, {});

return newObject;
};

export {
getInspireKeywords,
getLanguages,
Expand All @@ -184,4 +212,5 @@ export {
getFileExtension,
formatDateFromISO,
getArrayRange,
trimObject,
};
1 change: 0 additions & 1 deletion src/Services/CswMetadataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public function fromXml(string $metadataXml): CswMetadata
$cswMetadata->updateDate = $list->item(0)->textContent;
}

// TODO $cswMetadata->thematicCategories = $keywordsList;
$cswMetadata->contactEmail = $xpath->query('/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:electronicMailAddress/gco:CharacterString')->item(0)->textContent;
$cswMetadata->organisationName = $xpath->query('/gmd:MD_Metadata/gmd:contact/gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString')->item(0)->textContent;
$cswMetadata->organisationEmail = $xpath->query('/gmd:MD_Metadata/gmd:contact/gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:electronicMailAddress/gco:CharacterString')->item(0)->textContent;
Expand Down

0 comments on commit 547cd33

Please sign in to comment.