Skip to content

Commit

Permalink
feat: add nomAlt to commune
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Jan 8, 2025
1 parent 3c7a870 commit bffd6b4
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 8 deletions.
129 changes: 129 additions & 0 deletions components/bal/commune-editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { useState, useContext, useCallback, useEffect } from "react";
import { Pane, Button, Label } from "evergreen-ui";

import BalDataContext from "@/contexts/bal-data";

import useValidationMessage from "@/hooks/validation-messages";

import Form from "@/components/form";
import FormInput from "@/components/form-input";
import LanguesRegionalesForm from "@/components/langues-regionales-form";
import {
BaseLocale,
BasesLocalesService,
UpdateBaseLocaleDTO,
} from "@/lib/openapi-api-bal";
import LayoutContext from "@/contexts/layout";

interface CommuneEditorProps {
initialValue?: BaseLocale;
closeForm: () => void;
onSubmitted?: () => void;
}

function CommuneEditor({ initialValue, closeForm }: CommuneEditorProps) {
const [isLoading, setIsLoading] = useState(false);
const [nomAlt, setNomAlt] = useState(initialValue?.nomAlt);
const { reloadBaseLocale } = useContext(BalDataContext);
const { getValidationMessage, setValidationMessages } =
useValidationMessage();
const { toaster } = useContext(LayoutContext);

const onFormSubmit = useCallback(
async (e) => {
e.preventDefault();
setValidationMessages(null);
setIsLoading(true);

try {
const body: UpdateBaseLocaleDTO = {
nomAlt: Object.keys(nomAlt).length > 0 ? nomAlt : null,
};
// Add or edit a voie
const submit = toaster(
async () =>
BasesLocalesService.updateBaseLocale(initialValue.id, body),
"La bal a bien été modifiée",
"La bal n’a pas pu être modifiée",
(err) => {
setValidationMessages(err.body.message);
}
);

await submit();
await reloadBaseLocale();
closeForm();
} catch (err) {
console.error(err);
} finally {
setIsLoading(false);
}
},
[
initialValue,
nomAlt,
closeForm,
setValidationMessages,
toaster,
reloadBaseLocale,
]
);

const onFormCancel = useCallback(
(e) => {
e.preventDefault();
closeForm();
},
[closeForm]
);

// Reset validation messages on changes
useEffect(() => {
setValidationMessages(null);
}, [nomAlt, setValidationMessages]);

return (
<Form
editingId={initialValue?.id}
closeForm={closeForm}
onFormSubmit={onFormSubmit}
>
<Pane>
<FormInput>
<Label>Nom régionale de la commune</Label>
<LanguesRegionalesForm
initialValue={initialValue?.nomAlt}
validationMessage={getValidationMessage("langAlt")}
handleLanguages={setNomAlt}
autoOpen={true}
/>
</FormInput>
</Pane>

<Pane>
<Button
isLoading={isLoading}
type="submit"
appearance="primary"
intent="success"
>
{isLoading ? "En cours…" : "Enregistrer"}
</Button>

{closeForm && (
<Button
disabled={isLoading}
appearance="minimal"
marginLeft={8}
display="inline-flex"
onClick={onFormCancel}
>
Annuler
</Button>
)}
</Pane>
</Form>
);
}

export default CommuneEditor;
23 changes: 20 additions & 3 deletions components/langues-regionales-form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useCallback, useEffect, useState } from "react";
import { useCallback, useContext, useEffect, useState } from "react";
import { reduce, uniqueId } from "lodash";
import { Button, AddIcon } from "evergreen-ui";

import languesRegionales from "@ban-team/shared-data/langues-regionales.json";

import LanguageField from "./language-field";
import BalDataContext from "@/contexts/bal-data";

function objectToArray(obj) {
if (obj) {
Expand All @@ -25,21 +26,37 @@ function languagesArrayToObj(arr) {
);
}

function getInitialValue(initialValue: any, autoOpen: boolean) {
if (initialValue) {
return objectToArray(initialValue);
} else if (autoOpen) {
return [{ code: "", value: "", id: uniqueId() }];
}
return [];
}

interface LanguesRegionalesFormProps {
initialValue?: any;
validationMessage?: string;
handleLanguages: (value: any) => void;
autoOpen?: boolean;
}

function LanguesRegionalesForm({
initialValue,
validationMessage,
handleLanguages,
autoOpen = false,
}: LanguesRegionalesFormProps) {
const [nomAlt, setNomAlt] = useState(objectToArray(initialValue));
const { baseLocale } = useContext(BalDataContext);
const [nomAlt, setNomAlt] = useState(getInitialValue(initialValue, autoOpen));

const onAddForm = () => {
setNomAlt((prev) => [...prev, { code: null, value: "", id: uniqueId() }]);
let code: string = null;
if (baseLocale.nomAlt && Object.keys(baseLocale.nomAlt).length > 0) {
code = Object.keys(baseLocale.nomAlt)[0];
}
setNomAlt((prev) => [...prev, { code, value: "", id: uniqueId() }]);
};

const onLanguageChange = useCallback(
Expand Down
29 changes: 25 additions & 4 deletions components/sidebar/header.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
import React from "react";
import { Heading, Pane, Text } from "evergreen-ui";
import React, { useContext } from "react";
import { EditIcon, Heading, Pane, Text } from "evergreen-ui";

import { ExtendedVoieDTO } from "@/lib/openapi-api-bal";
import { BaseLocale, ExtendedVoieDTO } from "@/lib/openapi-api-bal";
import { CommuneType } from "@/types/commune";
import LanguagePreview from "../bal/language-preview";
import TokenContext from "@/contexts/token";
import BalDataContext from "@/contexts/bal-data";

interface HeaderSideBarProps {
baseLocale: BaseLocale;
commune: CommuneType;
voies: ExtendedVoieDTO[];
openForm: () => void;
}

function HeaderSideBar({ commune, voies }: HeaderSideBarProps) {
function HeaderSideBar({
baseLocale,
commune,
voies,
openForm,
}: HeaderSideBarProps) {
const { token } = useContext(TokenContext);
const { editingId, isEditing, numeros } = useContext(BalDataContext);
return (
<Pane display="flex" flexDirection="column" background="tint1" padding={16}>
<Heading>
{commune.nom} - {commune.code}
{!isEditing && token && (
<EditIcon
marginBottom={-2}
marginLeft={8}
color="black"
onClick={openForm}
/>
)}
</Heading>
{baseLocale.nomAlt && <LanguagePreview nomAlt={baseLocale.nomAlt} />}
{voies && (
<Text>
{voies.length} voie{voies.length > 1 ? "s" : ""}
Expand Down
1 change: 1 addition & 0 deletions lib/openapi-api-bal/models/BaseLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type BaseLocale = {
updatedAt: string;
deletedAt: string;
nom: string;
nomAlt: Record<string, any>;
commune: string;
emails: Array<string>;
token: string;
Expand Down
1 change: 1 addition & 0 deletions lib/openapi-api-bal/models/ExtendedBaseLocaleDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type ExtendedBaseLocaleDTO = {
updatedAt: string;
deletedAt: string;
nom: string;
nomAlt: Record<string, any>;
commune: string;
emails: Array<string>;
token: string;
Expand Down
1 change: 1 addition & 0 deletions lib/openapi-api-bal/models/UpdateBaseLocaleDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export type UpdateBaseLocaleDTO = {
nom?: string;
nomAlt?: Record<string, any> | null;
emails?: Array<string>;
};

20 changes: 19 additions & 1 deletion pages/bal/[balId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import MapContext from "@/contexts/map";
import LayoutContext from "@/contexts/layout";
import ToponymesList from "@/components/bal/toponymes-list";
import ConvertVoieWarning from "@/components/convert-voie-warning";
import CommuneEditor from "@/components/bal/commune-editor";

interface BaseLocalePageProps {
selectedTab: TabsEnum;
Expand All @@ -41,6 +42,7 @@ interface BaseLocalePageProps {

function BaseLocalePage({ commune }: BaseLocalePageProps) {
const [editedItem, setEditedItem] = useState<Voie | Toponyme | null>(null);
const [isCommuneFormOpen, setIsCommuneFormOpen] = useState<boolean>(false);
const [isFormOpen, setIsFormOpen] = useState<boolean>(false);
const [toConvert, setToConvert] = useState<string | null>(null);
const [onConvertLoading, setOnConvertLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -174,7 +176,14 @@ function BaseLocalePage({ commune }: BaseLocalePageProps) {
}}
onConfirm={onConvert}
/>
<HeaderSideBar commune={commune} voies={voies} />
<HeaderSideBar
baseLocale={baseLocale}
commune={commune}
voies={voies}
openForm={() => {
setIsCommuneFormOpen(true);
}}
/>
<Pane
position="relative"
display="flex"
Expand All @@ -183,6 +192,15 @@ function BaseLocalePage({ commune }: BaseLocalePageProps) {
width="100%"
overflow="hidden"
>
{isCommuneFormOpen && (
<CommuneEditor
initialValue={baseLocale as BaseLocale}
closeForm={() => {
setIsCommuneFormOpen(false);
}}
/>
)}

{isFormOpen && selectedTab === TabsEnum.VOIES && (
<VoieEditor
initialValue={editedItem as Voie}
Expand Down

0 comments on commit bffd6b4

Please sign in to comment.