Skip to content

Commit

Permalink
feat: add comment to voie (#969)
Browse files Browse the repository at this point in the history
* feat: add comment to voie

* feat: add comment to voie

* openapi: delete adminService

* return Jules

* correct wording

* delete cookie

* correct

* correct

* delete package cookie
  • Loading branch information
fufeck authored Dec 18, 2024
1 parent 5e6d302 commit ecf9dee
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 22 deletions.
6 changes: 5 additions & 1 deletion components/bal/toponymes-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ function ToponymesList({
}
comment={
toponyme.commentedNumeros.length > 0 ? (
<CommentsContent comments={toponyme.commentedNumeros} />
<CommentsContent
commentedNumeros={(
toponyme.commentedNumeros as Numero[]
).map(({ comment }) => comment)}
/>
) : null
}
/>
Expand Down
8 changes: 8 additions & 0 deletions components/bal/voie-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
VoiesService,
} from "@/lib/openapi-api-bal";
import LayoutContext from "@/contexts/layout";
import Comment from "../comment";

interface VoieEditorProps {
initialValue?: Voie;
Expand All @@ -43,6 +44,9 @@ function VoieEditor({
initialValue ? initialValue.typeNumerotation === "metrique" : false
);
const [nom, onNomChange] = useInput(initialValue ? initialValue.nom : "");
const [comment, onCommentChange] = useInput(
initialValue ? initialValue.comment : ""
);
const { getValidationMessage, setValidationMessages } =
useValidationMessage();
const [nomAlt, setNomAlt] = useState(initialValue?.nomAlt);
Expand All @@ -66,6 +70,7 @@ function VoieEditor({
nomAlt: Object.keys(nomAlt).length > 0 ? nomAlt : null,
typeNumerotation: isMetric ? "metrique" : "numerique",
trace: data ? data.geometry : null,
comment: comment ? comment : null,
};

// Add or edit a voie
Expand Down Expand Up @@ -126,6 +131,7 @@ function VoieEditor({
baseLocale.id,
initialValue,
nom,
comment,
isMetric,
data,
nomAlt,
Expand Down Expand Up @@ -203,6 +209,8 @@ function VoieEditor({
/>
</FormInput>

<Comment input={comment} onChange={onCommentChange} />

{isMetric && <DrawEditor />}
</Pane>

Expand Down
7 changes: 5 additions & 2 deletions components/bal/voies-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,11 @@ function VoiesList({
: null
}
comment={
voie.comments.length > 0 ? (
<CommentsContent comments={voie.comments} />
voie.comment?.length || voie.commentedNumeros?.length > 0 ? (
<CommentsContent
mainComment={voie.comment}
commentedNumeros={voie.commentedNumeros}
/>
) : null
}
warning={
Expand Down
29 changes: 22 additions & 7 deletions components/comments-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@ import { uniqueId } from "lodash";
const COMMENTS_LIMIT = 10;

interface CommentsContentProps {
comments: string[];
mainComment?: string;
commentedNumeros?: string[];
}

function CommentsContent({ comments }: CommentsContentProps) {
const filteredComments = comments.slice(0, COMMENTS_LIMIT);
const nbComments = comments.length;
function CommentsContent({
mainComment,
commentedNumeros,
}: CommentsContentProps) {
const filteredComments = commentedNumeros.slice(0, COMMENTS_LIMIT);
const nbComments = commentedNumeros.length;
const remainComments = nbComments - COMMENTS_LIMIT;

return (
<>
<Pane marginBottom={8}>
<Text color="white">
Commentaire{`${comments.length > 0 ? "s" : ""}`} :
</Text>
{mainComment ? (
<Pane
whiteSpace="nowrap"
overflow="hidden"
textOverflow="ellipsis"
color="white"
>
<Text color="white">{mainComment}</Text>
</Pane>
) : (
<Text color="white">
Commentaire{`${commentedNumeros.length > 0 ? "s" : ""}`}
</Text>
)}
</Pane>
{filteredComments.map((comment) => (
<Pane
Expand Down
1 change: 0 additions & 1 deletion components/table-row/table-row-notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function TableRowNotifications({
warning,
}: TableRowNotificationsProps) {
const { token } = useContext(TokenContext);

return (
<>
{comment && token && (
Expand Down
5 changes: 5 additions & 0 deletions components/voie/voie-heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function VoieHeading({ voie }: VoieHeadingProps) {
{numeros.length} numéro{numeros.length > 1 ? "s" : ""}
</Text>
)}
{voie.comment && (
<Text color="gray" fontStyle="italic">
{voie.comment}
</Text>
)}
</Pane>
{voie.nomAlt && <LanguagePreview nomAlt={voie.nomAlt} />}
</Heading>
Expand Down
5 changes: 4 additions & 1 deletion contexts/bal-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ interface BALDataContextType {
isHabilitationValid: boolean;
parcelles: Array<string>;
reloadParcelles: () => Promise<void>;
setVoies: React.Dispatch<React.SetStateAction<ExtendedVoieDTO[]>>;
voie: Voie;
setVoie: (voie: Voie) => void;
setVoie: React.Dispatch<React.SetStateAction<Voie>>;
toponyme: Toponyme;
setToponyme: (Toponyme: Toponyme) => void;
numeros: Array<Numero>;
Expand Down Expand Up @@ -298,6 +299,7 @@ export function BalDataContextProvider({
reloadToponymes,
reloadBaseLocale,
setVoie,
setVoies,
setToponyme,
certifyAllNumeros,
uncertifyAllNumeros,
Expand Down Expand Up @@ -326,6 +328,7 @@ export function BalDataContextProvider({
initialToponymes,
voies,
toponymes,
setVoies,
reloadNumeros,
reloadVoies,
reloadToponymes,
Expand Down
1 change: 1 addition & 0 deletions lib/openapi-api-bal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type { UpdateToponymeDTO } from './models/UpdateToponymeDTO';
export { UpdateVoieDTO } from './models/UpdateVoieDTO';
export type { ValidatePinCodeDTO } from './models/ValidatePinCodeDTO';
export { Voie } from './models/Voie';
export type { VoieMetas } from './models/VoieMetas';

export { BasesLocalesService } from './services/BasesLocalesService';
export { CommuneService } from './services/CommuneService';
Expand Down
1 change: 1 addition & 0 deletions lib/openapi-api-bal/models/CreateVoieDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type CreateVoieDTO = {
nomAlt?: Record<string, any> | null;
typeNumerotation?: CreateVoieDTO.typeNumerotation;
trace?: LineString;
comment?: string | null;
};

export namespace CreateVoieDTO {
Expand Down
3 changes: 2 additions & 1 deletion lib/openapi-api-bal/models/ExtendedVoieDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ export type ExtendedVoieDTO = {
centroid: Record<string, any>;
trace: Record<string, any>;
bbox: Array<string>;
comment: string;
baseLocale: BaseLocale;
numeros: Array<Numero>;
nbNumeros: number;
nbNumerosCertifies: number;
isAllCertified: boolean;
comments: Array<string>;
commentedNumeros: Array<string>;
};

export namespace ExtendedVoieDTO {
Expand Down
1 change: 1 addition & 0 deletions lib/openapi-api-bal/models/UpdateVoieDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type UpdateVoieDTO = {
nomAlt?: Record<string, any> | null;
typeNumerotation?: UpdateVoieDTO.typeNumerotation;
trace?: LineString;
comment?: string | null;
};

export namespace UpdateVoieDTO {
Expand Down
1 change: 1 addition & 0 deletions lib/openapi-api-bal/models/Voie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Voie = {
centroid: Record<string, any>;
trace: Record<string, any>;
bbox: Array<string>;
comment: string;
baseLocale: BaseLocale;
numeros: Array<Numero>;
};
Expand Down
13 changes: 13 additions & 0 deletions lib/openapi-api-bal/models/VoieMetas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

export type VoieMetas = {
id: string;
nbNumeros: number;
nbNumerosCertifies: number;
isAllCertified: boolean;
commentedNumeros: Array<string>;
};

19 changes: 19 additions & 0 deletions lib/openapi-api-bal/services/BasesLocalesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { UpdateBaseLocaleDemoDTO } from '../models/UpdateBaseLocaleDemoDTO'
import type { UpdateBaseLocaleDTO } from '../models/UpdateBaseLocaleDTO';
import type { UpdateBatchNumeroDTO } from '../models/UpdateBatchNumeroDTO';
import type { Voie } from '../models/Voie';
import type { VoieMetas } from '../models/VoieMetas';

import type { CancelablePromise } from '../core/CancelablePromise';
import { OpenAPI } from '../core/OpenAPI';
Expand Down Expand Up @@ -534,6 +535,24 @@ export class BasesLocalesService {
});
}

/**
* Find all Metas Voie in Bal
* @param baseLocaleId
* @returns VoieMetas
* @throws ApiError
*/
public static findVoieMetasByBal(
baseLocaleId: string,
): CancelablePromise<Array<VoieMetas>> {
return __request(OpenAPI, {
method: 'GET',
url: '/v2/bases-locales/{baseLocaleId}/voies/metas',
path: {
'baseLocaleId': baseLocaleId,
},
});
}

/**
* Find all Toponymes in Bal
* @param baseLocaleId
Expand Down
19 changes: 19 additions & 0 deletions lib/openapi-api-bal/services/VoiesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { RestoreVoieDTO } from '../models/RestoreVoieDTO';
import type { Toponyme } from '../models/Toponyme';
import type { UpdateVoieDTO } from '../models/UpdateVoieDTO';
import type { Voie } from '../models/Voie';
import type { VoieMetas } from '../models/VoieMetas';

import type { CancelablePromise } from '../core/CancelablePromise';
import { OpenAPI } from '../core/OpenAPI';
Expand Down Expand Up @@ -74,6 +75,24 @@ export class VoiesService {
});
}

/**
* Find Voie Metas by id
* @param voieId
* @returns VoieMetas
* @throws ApiError
*/
public static findVoieMetas(
voieId: string,
): CancelablePromise<VoieMetas> {
return __request(OpenAPI, {
method: 'GET',
url: '/v2/voies/{voieId}/metas',
path: {
'voieId': voieId,
},
});
}

/**
* Soft delete Voie by id
* @param voieId
Expand Down
35 changes: 32 additions & 3 deletions pages/bal/[balId]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useCallback, useContext, useEffect, useState } from "react";
import React, { useState, useCallback, useContext, useEffect } from "react";
import { keyBy } from "lodash";
import { Pane, Paragraph } from "evergreen-ui";

import TokenContext from "@/contexts/token";
Expand All @@ -17,6 +18,9 @@ import {
Toponyme,
Voie,
VoiesService,
VoieMetas,
ExtendedVoieDTO,
BasesLocalesService,
} from "@/lib/openapi-api-bal";
import usePublishProcess from "@/hooks/publish-process";
import HeaderSideBar from "@/components/sidebar/header";
Expand All @@ -42,9 +46,15 @@ function BaseLocalePage({ commune }: BaseLocalePageProps) {
const [onConvertLoading, setOnConvertLoading] = useState<boolean>(false);

const { token } = useContext(TokenContext);
const { voies, toponymes, baseLocale, habilitation, isHabilitationValid } =
useContext(BalDataContext);
const { toaster } = useContext(LayoutContext);
const {
voies,
toponymes,
baseLocale,
habilitation,
isHabilitationValid,
setVoies,
} = useContext(BalDataContext);
const { reloadTiles } = useContext(MapContext);
const { setIsRecoveryDisplayed } = useContext(BALRecoveryContext);
const { handleShowHabilitationProcess } = usePublishProcess(commune);
Expand All @@ -62,6 +72,25 @@ function BaseLocalePage({ commune }: BaseLocalePageProps) {
}
useHelp(help);

useEffect(() => {
async function addCommentsToVoies() {
try {
const voieMetas: VoieMetas[] =
await BasesLocalesService.findVoieMetasByBal(baseLocale.id);
const voiesMetasByVoieId = keyBy(voieMetas, "id");
setVoies((voies: ExtendedVoieDTO[]) =>
voies.map((v) => ({ ...v, ...voiesMetasByVoieId[v.id] }))
);
} catch (e) {
console.error("Impossible de charger les commentaires de voies", e);
}
}

if (token) {
addCommentsToVoies();
}
}, [baseLocale.id, setVoies, token]);

useEffect(() => {
if (
token &&
Expand Down
Loading

0 comments on commit ecf9dee

Please sign in to comment.