|
1 | 1 | import Button from "@codegouvfr/react-dsfr/Button"; |
2 | | -import type { ReactNode } from "react"; |
3 | | -import { type AgencyRight, domElementIds, frontRoutes } from "shared"; |
| 2 | +import { createModal } from "@codegouvfr/react-dsfr/Modal"; |
| 3 | +import { type ReactNode, useMemo } from "react"; |
| 4 | +import { createPortal } from "react-dom"; |
| 5 | +import { useDispatch } from "react-redux"; |
| 6 | +import { |
| 7 | + type AgencyRight, |
| 8 | + domElementIds, |
| 9 | + frontRoutes, |
| 10 | + type User, |
| 11 | +} from "shared"; |
| 12 | +import { removeUserFromAgencySlice } from "src/core-logic/domain/agencies/remove-user-from-agency/removeUserFromAgency.slice"; |
| 13 | +import { feedbackSlice } from "src/core-logic/domain/feedback/feedback.slice"; |
| 14 | +import { |
| 15 | + makeRemoveUserAgencyRightsButtonProps, |
| 16 | + makeRemoveUserAgencyRightsModalProps, |
| 17 | +} from "../../removeUserAgencyRights"; |
4 | 18 |
|
5 | 19 | export const AgencyLineRightsCTAs = ({ |
6 | 20 | agencyRight, |
7 | 21 | isBackofficeAdmin, |
8 | 22 | onUpdateClicked, |
9 | 23 | onRegistrationCancelledClicked, |
| 24 | + user, |
10 | 25 | }: { |
11 | 26 | agencyRight: AgencyRight; |
12 | 27 | onUpdateClicked?: (agencyRight: AgencyRight) => void; |
13 | 28 | onRegistrationCancelledClicked?: (agencyRight: AgencyRight) => void; |
14 | | - isBackofficeAdmin?: boolean | undefined; |
15 | | -}): ReactNode => ( |
16 | | - <> |
17 | | - {onUpdateClicked && ( |
18 | | - <Button |
19 | | - size="small" |
20 | | - priority="secondary" |
21 | | - id={`${domElementIds.myAccount.editRoleButton}-${agencyRight.agency.id}`} |
22 | | - onClick={() => { |
23 | | - onUpdateClicked(agencyRight); |
24 | | - }} |
25 | | - > |
26 | | - Modifier |
27 | | - </Button> |
28 | | - )} |
29 | | - {isBackofficeAdmin && ( |
30 | | - <Button |
31 | | - priority="tertiary no outline" |
32 | | - id={`${domElementIds.myAccount.adminAgencyLink}-${agencyRight.agency.id}`} |
33 | | - size="small" |
34 | | - linkProps={ |
35 | | - frontRoutes.adminAgencyDetail({ |
36 | | - agencyId: agencyRight.agency.id, |
37 | | - }).link |
38 | | - } |
39 | | - > |
40 | | - Voir l'agence comme admin IF |
41 | | - </Button> |
42 | | - )} |
43 | | - {onRegistrationCancelledClicked && ( |
| 29 | + isBackofficeAdmin?: boolean; |
| 30 | + user: User; |
| 31 | +}): ReactNode => { |
| 32 | + const removeUserModalId = `${domElementIds.myAccount.removeAgencyRightModal}-${agencyRight.agency.id}-${user.id}`; |
| 33 | + const userRightToRemove = { |
| 34 | + agencyRight, |
| 35 | + userEmail: user.email, |
| 36 | + userId: user.id, |
| 37 | + isSelfRemoval: true, |
| 38 | + }; |
| 39 | + const dispatch = useDispatch(); |
| 40 | + const removeUserModal = useMemo( |
| 41 | + () => |
| 42 | + createModal({ |
| 43 | + isOpenedByDefault: false, |
| 44 | + id: removeUserModalId, |
| 45 | + }), |
| 46 | + [removeUserModalId], |
| 47 | + ); |
| 48 | + |
| 49 | + return ( |
| 50 | + <> |
| 51 | + {onUpdateClicked && ( |
| 52 | + <Button |
| 53 | + size="small" |
| 54 | + priority="secondary" |
| 55 | + id={`${domElementIds.myAccount.editRoleButton}-${agencyRight.agency.id}`} |
| 56 | + onClick={() => { |
| 57 | + onUpdateClicked(agencyRight); |
| 58 | + }} |
| 59 | + > |
| 60 | + Modifier |
| 61 | + </Button> |
| 62 | + )} |
44 | 63 | <Button |
45 | | - priority="secondary" |
46 | | - id={`${domElementIds.myAccount.cancelRegistrationButton}-${agencyRight.agency.id}`} |
47 | | - size="small" |
48 | | - onClick={() => { |
49 | | - onRegistrationCancelledClicked(agencyRight); |
50 | | - }} |
51 | | - > |
52 | | - Annuler la demande |
53 | | - </Button> |
54 | | - )} |
55 | | - </> |
56 | | -); |
| 64 | + {...makeRemoveUserAgencyRightsButtonProps({ |
| 65 | + onDeleteClicked: () => { |
| 66 | + dispatch(feedbackSlice.actions.clearFeedbacksTriggered()); |
| 67 | + removeUserModal.open(); |
| 68 | + }, |
| 69 | + removeUserButtonId: `${domElementIds.myAccount.removeAgencyRightButton}-${agencyRight.agency.id}-${user.id}`, |
| 70 | + userRightToRemove, |
| 71 | + size: "small", |
| 72 | + })} |
| 73 | + /> |
| 74 | + {isBackofficeAdmin && ( |
| 75 | + <Button |
| 76 | + priority="tertiary no outline" |
| 77 | + id={`${domElementIds.myAccount.adminAgencyLink}-${agencyRight.agency.id}`} |
| 78 | + size="small" |
| 79 | + linkProps={ |
| 80 | + frontRoutes.adminAgencyDetail({ |
| 81 | + agencyId: agencyRight.agency.id, |
| 82 | + }).link |
| 83 | + } |
| 84 | + > |
| 85 | + Voir l'agence comme admin IF |
| 86 | + </Button> |
| 87 | + )} |
| 88 | + {onRegistrationCancelledClicked && ( |
| 89 | + <Button |
| 90 | + priority="secondary" |
| 91 | + id={`${domElementIds.myAccount.cancelRegistrationButton}-${agencyRight.agency.id}`} |
| 92 | + size="small" |
| 93 | + onClick={() => { |
| 94 | + onRegistrationCancelledClicked(agencyRight); |
| 95 | + }} |
| 96 | + > |
| 97 | + Annuler la demande |
| 98 | + </Button> |
| 99 | + )} |
| 100 | + {createPortal( |
| 101 | + removeUserModal.Component( |
| 102 | + makeRemoveUserAgencyRightsModalProps({ |
| 103 | + onCancel: () => removeUserModal.close(), |
| 104 | + onSubmitted: () => { |
| 105 | + dispatch( |
| 106 | + removeUserFromAgencySlice.actions.removeUserFromAgencyRequested( |
| 107 | + { |
| 108 | + userId: userRightToRemove.userId, |
| 109 | + agencyId: userRightToRemove.agencyRight.agency.id, |
| 110 | + feedbackTopic: "agency-user-right-self", |
| 111 | + }, |
| 112 | + ), |
| 113 | + ); |
| 114 | + removeUserModal.close(); |
| 115 | + }, |
| 116 | + userRightToRemove, |
| 117 | + }), |
| 118 | + ), |
| 119 | + document.body, |
| 120 | + )} |
| 121 | + </> |
| 122 | + ); |
| 123 | +}; |
0 commit comments