Skip to content

Commit

Permalink
feat(structures): DIsplay warning notice if identifier with same type…
Browse files Browse the repository at this point in the history
… and value already exists for this structure
  • Loading branch information
annelhote committed Oct 3, 2024
1 parent 2775693 commit 1bb0548
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
43 changes: 25 additions & 18 deletions src/components/blocs/identifiers/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { Col, Modal, ModalContent, ModalTitle, Text } from '@dataesr/react-dsfr';
import {
Col,
Modal,
ModalContent,
ModalTitle,
Text,
} from '@dataesr/react-dsfr';
import { useState } from 'react';

import IdentifierForm from '../../forms/identifier';
import ExpendableListCards from '../../card/expendable-list-cards';
import {
Bloc,
BlocActionButton,
BlocContent,
BlocModal,
BlocTitle,
} from '../../bloc';
import KeyValueCard from '../../card/key-value-card';
import useEnums from '../../../hooks/useEnums';
import useFetch from '../../../hooks/useFetch';
import useNotice from '../../../hooks/useNotice';
import useUrl from '../../../hooks/useUrl';
import api from '../../../utils/api';
import getLink from '../../../utils/get-links';
import { getTvaIntraFromSiren } from '../../../utils/get-tva-intra';
import {
deleteError,
deleteSuccess,
saveDuplicate,
saveError,
saveSuccess,
} from '../../../utils/notice-contents';
import getLink from '../../../utils/get-links';
import {
Bloc,
BlocActionButton,
BlocContent,
BlocModal,
BlocTitle,
} from '../../bloc';
import ExpendableListCards from '../../card/expendable-list-cards';
import KeyValueCard from '../../card/key-value-card';
import CopyButton from '../../copy/copy-button';
import IdentifierForm from '../../forms/identifier';

export default function IdentifiersComponent() {
const { notice } = useNotice();
Expand All @@ -40,8 +47,8 @@ export default function IdentifiersComponent() {
const method = itemId ? 'patch' : 'post';
const saveUrl = itemId ? `${url}/${itemId}` : url;
await api[method](saveUrl, body)
.then(() => {
notice(saveSuccess);
.then((response) => {
notice(response.status === 204 ? saveDuplicate : saveSuccess);
reload();
})
.catch(() => notice(saveError));
Expand Down Expand Up @@ -78,13 +85,13 @@ export default function IdentifiersComponent() {
const renderCards = () => {
if (!data) return null;
const list = [];
const inactives = data.data.filter((el) => (el.active === false));
const actives = data.data.filter((el) => (el.active !== false));
const inactives = data.data.filter((el) => el.active === false);
const actives = data.data.filter((el) => el.active !== false);
const orderedList = [...actives, ...inactives];

if (data) {
orderedList?.forEach((el) => {
const inactive = (el.active === false);
const inactive = el.active === false;
let siretCard = el.value;

if (el.type === 'siret') {
Expand Down Expand Up @@ -116,7 +123,7 @@ export default function IdentifiersComponent() {
if (el.type !== 'siret' && el.type !== 'cnrs-unit') {
list.push(
<KeyValueCard
cardKey={options?.find((type) => (el.type === type.value))?.label}
cardKey={options?.find((type) => el.type === type.value)?.label}
cardValue={el.value}
className={`card-${apiObject}`}
copy
Expand Down
7 changes: 4 additions & 3 deletions src/components/notice/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import PropTypes from 'prop-types';

import usePausableTimer from '../../hooks/usePausableTimer';

import './notice.scss';

export default function Notice({
content,
autoDismissAfter,
content,
remove,
type,
}) {
Expand Down Expand Up @@ -43,14 +44,14 @@ export default function Notice({
}

Notice.propTypes = {
content: PropTypes.string,
autoDismissAfter: PropTypes.number,
content: PropTypes.string,
remove: PropTypes.func.isRequired,
type: PropTypes.oneOf(['info', 'success', 'error', 'warning']),
};

Notice.defaultProps = {
content: '',
autoDismissAfter: 6000,
content: '',
type: 'info',
};
7 changes: 4 additions & 3 deletions src/hooks/useNotice.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import PropTypes from 'prop-types';
import {
createContext,
useContext,
useState,
useCallback,
useContext,
useMemo,
useState,
} from 'react';
import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';

import Notice from '../components/notice';

const NoticeContext = createContext();
Expand Down
24 changes: 20 additions & 4 deletions src/utils/notice-contents.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
export const deleteError = { content: "Une erreur s'est produite. L'élément n'a pas pu être supprimé", autoDismissAfter: 6000, type: 'error' };
export const saveError = { content: "Une erreur s'est produite lors de l'envoi du formulaire.", autoDismissAfter: 6000, type: 'error' };
export const saveSuccess = { content: 'Vos modifications ont été correctement enregistrées', autoDismissAfter: 6000, type: 'success' };
export const deleteSuccess = { content: "L'élément a été supprimé avec succès.", autoDismissAfter: 6000, type: 'success' };
export const deleteError = {
content: "Une erreur s'est produite. L'élément n'a pas pu être supprimé",
type: 'error',
};
export const deleteSuccess = {
content: "L'élément a été supprimé avec succès.",
type: 'success',
};
export const saveError = {
content: "Une erreur s'est produite lors de l'envoi du formulaire.",
type: 'error',
};
export const saveSuccess = {
content: 'Vos modifications ont été correctement enregistrées',
type: 'success',
};
export const saveDuplicate = {
content: "Vos modifications n'ont pas été prises en compte car un élément identique existait déjà.",
type: 'warning',
};

0 comments on commit 1bb0548

Please sign in to comment.