diff --git a/src/components/blocs/identifiers/index.js b/src/components/blocs/identifiers/index.js index a634b97f..9c5e541a 100644 --- a/src/components/blocs/identifiers/index.js +++ b/src/components/blocs/identifiers/index.js @@ -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(); @@ -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)); @@ -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') { @@ -116,7 +123,7 @@ export default function IdentifiersComponent() { if (el.type !== 'siret' && el.type !== 'cnrs-unit') { list.push( (el.type === type.value))?.label} + cardKey={options?.find((type) => el.type === type.value)?.label} cardValue={el.value} className={`card-${apiObject}`} copy diff --git a/src/components/notice/index.js b/src/components/notice/index.js index c95a136f..60a56dc6 100644 --- a/src/components/notice/index.js +++ b/src/components/notice/index.js @@ -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, }) { @@ -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', }; diff --git a/src/hooks/useNotice.js b/src/hooks/useNotice.js index 7a935f3a..02959b96 100644 --- a/src/hooks/useNotice.js +++ b/src/hooks/useNotice.js @@ -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(); diff --git a/src/utils/notice-contents.js b/src/utils/notice-contents.js index 872cbdd0..65094e20 100644 --- a/src/utils/notice-contents.js +++ b/src/utils/notice-contents.js @@ -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', +};