Skip to content

Commit

Permalink
feat: filtrage des tables geometriques
Browse files Browse the repository at this point in the history
  • Loading branch information
pprev94 committed Oct 10, 2023
1 parent 1222942 commit a2617ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
22 changes: 22 additions & 0 deletions assets/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { StoredDataDetailsRelationDto } from "./types/entrepot";

/**
* Ne conserve que les tables geometriques
* @param relation : StoredDataDetailsRelationDto
*/
const filterGeometricRelations = (relations: StoredDataDetailsRelationDto[], filter: boolean = false): StoredDataDetailsRelationDto[] => {
return relations.filter((relation) => {
const hasGeometry = (attributes) => {
for (const type of Object.values(attributes)) {
if (/^geometry\(/.test(type as string)) return true;
}
return false;
};

if ("type" in relation && relation.type !== "TABLE") return false;
if (!filter) return true;
return "attributes" in relation && hasGeometry(relation.attributes);
});
};

export { filterGeometricRelations };
14 changes: 2 additions & 12 deletions assets/pages/service/TableSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { fr } from "@codegouvfr/react-dsfr";
import { Checkbox } from "@codegouvfr/react-dsfr/Checkbox";
import { FC, useEffect, useState } from "react";
import { type UseFormReturn } from "react-hook-form";

import Translator from "../../modules/Translator";
import { type VectorDb } from "../../types/app";
import { filterGeometricRelations } from "../../helpers";

type TablesSelectionProps = {
filterGeometric?: boolean;
Expand All @@ -21,17 +21,7 @@ const TableSelection: FC<TablesSelectionProps> = ({ filterGeometric = false, vec
} = form;

const relations = vectorDb.type_infos?.relations || [];
const tables = relations.filter((relation) => {
const isGeometric = (attributes) => {
for (const type of Object.values(attributes)) {
if (/^geometry\(/.test(type as string)) return true;
}
};

if (relation.type !== "TABLE") return false;
if (!filterGeometric) return true;
return "attributes" in relation && isGeometric(relation.attributes);
});
const tables = filterGeometricRelations(relations, filterGeometric);

const [selectedTables, setSelectedTables] = useState<string[]>([]);

Expand Down

0 comments on commit a2617ef

Please sign in to comment.