1
- import { useEffect , useState } from 'react' ;
2
- import { Alert } from 'react-bootstrap' ;
1
+ import { useCallback , useEffect , useMemo , useState } from 'react' ;
3
2
import { Trans , useTranslation } from 'react-i18next' ;
4
3
import { useQueryClient } from 'react-query' ;
5
4
@@ -12,7 +11,9 @@ import {
12
11
} from '@/hooks/api/ownerships' ;
13
12
import { Organizer } from '@/types/Organizer' ;
14
13
import { Values } from '@/types/Values' ;
14
+ import { Alert , AlertVariants } from '@/ui/Alert' ;
15
15
import { Box } from '@/ui/Box' ;
16
+ import { getInlineProps , InlineProps } from '@/ui/Inline' ;
16
17
import { Modal , ModalSizes , ModalVariants } from '@/ui/Modal' ;
17
18
18
19
export const ActionType = {
@@ -24,6 +25,49 @@ export const ActionType = {
24
25
25
26
type ActionType = Values < typeof ActionType > ;
26
27
28
+ const OwnershipActionsAlert = ( {
29
+ request,
30
+ actionType,
31
+ onClose,
32
+ ...props
33
+ } : {
34
+ request ?: OwnershipRequest ;
35
+ actionType : ActionType ;
36
+ onClose : ( ) => void ;
37
+ } & InlineProps ) => {
38
+ const { i18n } = useTranslation ( ) ;
39
+ const translationsPath = `organizers.ownerships.${ actionType } _modal` ;
40
+
41
+ const getOrganizerByIdQuery = useGetOrganizerByIdQuery ( {
42
+ id : request ?. itemId ,
43
+ } ) ;
44
+
45
+ // @ts -expect-error
46
+ const organizer : Organizer = getOrganizerByIdQuery ?. data ;
47
+ const organizerName =
48
+ organizer ?. name ?. [ i18n . language ] ??
49
+ organizer ?. name ?. [ organizer . mainLanguage ] ??
50
+ organizer ?. name ;
51
+
52
+ return (
53
+ < Alert
54
+ variant = { AlertVariants . SUCCESS }
55
+ fullWidth
56
+ closable
57
+ onClose = { onClose }
58
+ { ...getInlineProps ( props ) }
59
+ >
60
+ < Trans
61
+ i18nKey = { `${ translationsPath } .success` }
62
+ values = { {
63
+ ownerEmail : request ?. ownerEmail ,
64
+ organizerName,
65
+ } }
66
+ />
67
+ </ Alert >
68
+ ) ;
69
+ } ;
70
+
27
71
const OwnershipActionModal = ( {
28
72
request,
29
73
actionType,
@@ -40,9 +84,16 @@ const OwnershipActionModal = ({
40
84
const { t, i18n } = useTranslation ( ) ;
41
85
const translationsPath = `organizers.ownerships.${ actionType } _modal` ;
42
86
43
- const getOrganizerByIdQuery = useGetOrganizerByIdQuery ( {
44
- id : request ?. itemId ,
45
- } ) ;
87
+ const getOrganizerByIdQuery = useGetOrganizerByIdQuery (
88
+ {
89
+ id : request ?. itemId ,
90
+ } ,
91
+ {
92
+ // This is needed if the filtered organizer is the same as the one from the action
93
+ // otherwise the query in the action will trigger the query on the page continuously
94
+ queryKey : [ 'organizers' , 'action-modal' ] ,
95
+ } ,
96
+ ) ;
46
97
47
98
// @ts -expect-error
48
99
const organizer : Organizer = getOrganizerByIdQuery ?. data ;
@@ -78,13 +129,13 @@ const OwnershipActionModal = ({
78
129
export const useOwnershipActions = ( ) => {
79
130
const queryClient = useQueryClient ( ) ;
80
131
81
- const [ isSuccessAlertVisible , setIsSuccessAlertVisible ] = useState ( false ) ;
132
+ const [ successfulAction , setSuccessfulAction ] = useState < ActionType > ( ) ;
82
133
const [ selectedRequest , setSelectedRequest ] = useState < OwnershipRequest > ( ) ;
83
134
const [ actionType , setActionType ] = useState < ActionType > ( ) ;
84
135
85
136
const approveOwnershipRequestMutation = useApproveOwnershipRequestMutation ( {
86
137
onSuccess : async ( ) => {
87
- setIsSuccessAlertVisible ( true ) ;
138
+ setSuccessfulAction ( ActionType . APPROVE ) ;
88
139
setActionType ( undefined ) ;
89
140
setSelectedRequest ( undefined ) ;
90
141
await queryClient . invalidateQueries ( 'ownership-requests' ) ;
@@ -93,7 +144,7 @@ export const useOwnershipActions = () => {
93
144
94
145
const rejectOwnershipRequestMutation = useRejectOwnershipRequestMutation ( {
95
146
onSuccess : async ( ) => {
96
- setIsSuccessAlertVisible ( true ) ;
147
+ setSuccessfulAction ( ActionType . REJECT ) ;
97
148
setActionType ( undefined ) ;
98
149
setSelectedRequest ( undefined ) ;
99
150
await queryClient . invalidateQueries ( 'ownership-requests' ) ;
@@ -102,6 +153,7 @@ export const useOwnershipActions = () => {
102
153
103
154
const deleteOwnershipRequestMutation = useDeleteOwnershipRequestMutation ( {
104
155
onSuccess : async ( ) => {
156
+ setSuccessfulAction ( ActionType . DELETE ) ;
105
157
setActionType ( undefined ) ;
106
158
setSelectedRequest ( undefined ) ;
107
159
await queryClient . invalidateQueries ( 'ownership-requests' ) ;
@@ -133,22 +185,36 @@ export const useOwnershipActions = () => {
133
185
}
134
186
} ;
135
187
136
- return {
137
- deleteOwnership : ( request : OwnershipRequest ) => {
138
- setActionType ( ActionType . DELETE ) ;
188
+ const triggerAction =
189
+ ( actionType : ActionType ) => ( request : OwnershipRequest ) => {
190
+ setActionType ( actionType ) ;
139
191
setSelectedRequest ( request ) ;
140
- } ,
141
- Modal : ( ) => {
142
- return (
143
- < OwnershipActionModal
144
- request = { selectedRequest }
145
- actionType = { actionType }
146
- isLoading = { isLoading }
147
- onClose = { ( ) => setSelectedRequest ( undefined ) }
148
- onConfirm = { handleConfirm }
192
+ } ;
193
+
194
+ return {
195
+ deleteOwnership : triggerAction ( ActionType . DELETE ) ,
196
+ approveOwnership : triggerAction ( ActionType . APPROVE ) ,
197
+ rejectOwnership : triggerAction ( ActionType . REJECT ) ,
198
+ Modal : ( ) => (
199
+ < OwnershipActionModal
200
+ request = { selectedRequest }
201
+ actionType = { actionType }
202
+ isLoading = { isLoading }
203
+ onClose = { ( ) => setSelectedRequest ( undefined ) }
204
+ onConfirm = { handleConfirm }
205
+ />
206
+ ) ,
207
+ Alert : ( props : InlineProps ) =>
208
+ successfulAction ? (
209
+ < OwnershipActionsAlert
210
+ actionType = { successfulAction }
211
+ onClose = { ( ) => {
212
+ setSuccessfulAction ( undefined ) ;
213
+ setSelectedRequest ( undefined ) ;
214
+ setActionType ( undefined ) ;
215
+ } }
216
+ { ...getInlineProps ( props ) }
149
217
/>
150
- ) ;
151
- } ,
152
- Alert : ( ) => ( isSuccessAlertVisible ? < Alert > test</ Alert > : null ) ,
218
+ ) : null ,
153
219
} ;
154
220
} ;
0 commit comments