1
1
import { useEffect , useState } from 'react' ;
2
+ import clsx from 'clsx' ;
2
3
import { FullPageMobileWrapper , PageReturn } from '@/components' ;
3
- import { api } from '@/hooks' ;
4
+ import { api , useModalManager } from '@/hooks' ;
4
5
import { LabelPairedChevronRightLgRegularIcon } from '@deriv/quill-icons' ;
5
6
import { useTranslations } from '@deriv-com/translations' ;
6
7
import { Modal , Text , ToggleSwitch , useDevice } from '@deriv-com/ui' ;
8
+ import { LeaveFilterModal } from '../LeaveFilterModal' ;
7
9
import { FilterModalContent } from './FilterModalContent' ;
8
10
import { FilterModalFooter } from './FilterModalFooter' ;
9
11
import './FilterModal.scss' ;
@@ -25,13 +27,15 @@ const FilterModal = ({
25
27
selectedPaymentMethods,
26
28
setSelectedPaymentMethods,
27
29
} : TFilterModalProps ) => {
30
+ const { hideModal, isModalOpenFor, showModal } = useModalManager ( ) ;
28
31
const { data } = api . paymentMethods . useGet ( ) ;
29
32
const { localize } = useTranslations ( ) ;
30
33
const [ showPaymentMethods , setShowPaymentMethods ] = useState < boolean > ( false ) ;
31
34
const [ isMatching , setIsMatching ] = useState < boolean > ( isToggled ) ;
32
35
const [ paymentMethods , setPaymentMethods ] = useState < string [ ] > ( selectedPaymentMethods ) ;
33
36
const [ paymentMethodNames , setPaymentMethodNames ] = useState < string > ( 'All' ) ;
34
37
const { isMobile } = useDevice ( ) ;
38
+ const [ isHidden , setIsHidden ] = useState ( false ) ;
35
39
36
40
const filterOptions = [
37
41
{
@@ -83,11 +87,28 @@ const FilterModal = ({
83
87
}
84
88
} , [ data , paymentMethods ] ) ;
85
89
90
+ const closeCancelModal = ( hideAll = false ) => {
91
+ hideModal ( { shouldHideAllModals : hideAll } ) ;
92
+ if ( ! hideAll ) {
93
+ setIsHidden ( false ) ;
94
+ }
95
+ } ;
96
+
97
+ const onClickClose = ( ) => {
98
+ const isValid = ( showPaymentMethods && hasSamePaymentMethods ) || ( ! showPaymentMethods && hasSameFilters ) ;
99
+ if ( isValid ) {
100
+ onRequestClose ( ) ;
101
+ } else {
102
+ setIsHidden ( true ) ;
103
+ showModal ( 'LeaveFilterModal' ) ;
104
+ }
105
+ } ;
106
+
86
107
if ( isMobile && isModalOpen ) {
87
108
return (
88
109
< FullPageMobileWrapper
89
110
className = 'filter-modal'
90
- onBack = { showPaymentMethods ? ( ) => setShowPaymentMethods ( false ) : onRequestClose }
111
+ onBack = { showPaymentMethods ? ( ) => setShowPaymentMethods ( false ) : onClickClose }
91
112
renderFooter = { ( ) => (
92
113
< FilterModalFooter
93
114
hasSameFilters = { hasSameFilters }
@@ -103,46 +124,60 @@ const FilterModal = ({
103
124
{ headerText }
104
125
</ Text >
105
126
) }
127
+ shouldShowBackIcon = { showPaymentMethods }
128
+ shouldShowCloseIcon
106
129
>
107
130
< FilterModalContent
108
131
filterOptions = { filterOptions }
109
132
paymentMethods = { paymentMethods }
110
133
setPaymentMethods = { setPaymentMethods }
111
134
showPaymentMethods = { showPaymentMethods }
112
135
/>
136
+ { isModalOpenFor ( 'LeaveFilterModal' ) && (
137
+ < LeaveFilterModal isModalOpen onRequestClose = { closeCancelModal } />
138
+ ) }
113
139
</ FullPageMobileWrapper >
114
140
) ;
115
141
}
116
142
117
143
return (
118
- < Modal ariaHideApp = { false } className = 'filter-modal' isOpen = { isModalOpen } onRequestClose = { onRequestClose } >
119
- < Modal . Header onRequestClose = { onRequestClose } >
120
- < PageReturn
121
- onClick = { ( ) => setShowPaymentMethods ( false ) }
122
- pageTitle = { headerText }
123
- shouldHideBackButton = { ! showPaymentMethods }
124
- weight = 'bold'
125
- />
126
- </ Modal . Header >
127
- < Modal . Body >
128
- < FilterModalContent
129
- filterOptions = { filterOptions }
130
- paymentMethods = { paymentMethods }
131
- setPaymentMethods = { setPaymentMethods }
132
- showPaymentMethods = { showPaymentMethods }
133
- />
134
- </ Modal . Body >
135
- < Modal . Footer className = 'p-0' >
136
- < FilterModalFooter
137
- hasSameFilters = { hasSameFilters }
138
- hasSamePaymentMethods = { hasSamePaymentMethods }
139
- onApplyConfirm = { onApplyConfirm }
140
- onResetClear = { onResetClear }
141
- paymentMethods = { paymentMethods }
142
- showPaymentMethods = { showPaymentMethods }
143
- />
144
- </ Modal . Footer >
145
- </ Modal >
144
+ < >
145
+ < Modal
146
+ ariaHideApp = { false }
147
+ className = { clsx ( 'filter-modal' , { hidden : isHidden } ) }
148
+ isOpen = { isModalOpen }
149
+ onRequestClose = { onClickClose }
150
+ style = { { overlay : { background : isHidden ? 'transparent' : 'rgba(0, 0, 0, 0.72)' , zIndex : 9999 } } }
151
+ >
152
+ < Modal . Header onRequestClose = { onClickClose } >
153
+ < PageReturn
154
+ onClick = { ( ) => setShowPaymentMethods ( false ) }
155
+ pageTitle = { headerText }
156
+ shouldHideBackButton = { ! showPaymentMethods }
157
+ weight = 'bold'
158
+ />
159
+ </ Modal . Header >
160
+ < Modal . Body >
161
+ < FilterModalContent
162
+ filterOptions = { filterOptions }
163
+ paymentMethods = { paymentMethods }
164
+ setPaymentMethods = { setPaymentMethods }
165
+ showPaymentMethods = { showPaymentMethods }
166
+ />
167
+ </ Modal . Body >
168
+ < Modal . Footer className = 'p-0' >
169
+ < FilterModalFooter
170
+ hasSameFilters = { hasSameFilters }
171
+ hasSamePaymentMethods = { hasSamePaymentMethods }
172
+ onApplyConfirm = { onApplyConfirm }
173
+ onResetClear = { onResetClear }
174
+ paymentMethods = { paymentMethods }
175
+ showPaymentMethods = { showPaymentMethods }
176
+ />
177
+ </ Modal . Footer >
178
+ </ Modal >
179
+ { isModalOpenFor ( 'LeaveFilterModal' ) && < LeaveFilterModal isModalOpen onRequestClose = { closeCancelModal } /> }
180
+ </ >
146
181
) ;
147
182
} ;
148
183
0 commit comments