11import { useNavigate } from 'react-router-dom'
2- import { FC , MutableRefObject , useEffect , useMemo , useRef } from 'react'
2+ import { FC , MutableRefObject , useEffect , useRef , useState } from 'react'
33import { connect } from 'react-redux'
4+ import { pick } from 'lodash'
45import classNames from 'classnames'
56
67import { Button , IconOutline , PageDivider } from '~/libs/ui'
78import { FormInputCheckbox } from '~/apps/self-service/src/components/form-elements'
89
9- import { createMemberPersonalizations , updateMemberPersonalizations } from '../../redux/actions/member'
1010import { ProgressBar } from '../../components/progress-bar'
11- import { useAutoSavePersonalization , useAutoSavePersonalizationType } from '../../hooks/useAutoSavePersonalization'
12- import PersonalizationInfo , { emptyPersonalizationInfo } from '../../models/PersonalizationInfo'
11+ import { updateMemberOpenForWork } from '../../redux/actions/member'
1312
1413import styles from './styles.module.scss'
1514
1615const FormInputCheckboxMiddleware : any = FormInputCheckbox as any
1716
18- const blankPersonalizationInfo : PersonalizationInfo = emptyPersonalizationInfo ( )
19-
20- interface PageOpenToWorkContentReduxProps {
21- reduxPersonalizations : PersonalizationInfo [ ] | undefined
22- loadingMemberTraits : boolean
23- }
24-
25- interface PageOpenToWorkContentProps extends PageOpenToWorkContentReduxProps {
26- updateMemberPersonalizations : ( infos : PersonalizationInfo [ ] ) => void
27- createMemberPersonalizations : ( infos : PersonalizationInfo [ ] ) => void
17+ interface PageOpenToWorkContentProps {
18+ availableForGigs : boolean
19+ updateMemberOpenForWork : ( isOpenForWork : boolean ) => void
2820}
2921
3022export const PageOpenToWorkContent : FC < PageOpenToWorkContentProps > = props => {
3123 const navigate : any = useNavigate ( )
3224
25+ const [ loading , setLoading ] = useState < boolean > ( false )
26+
3327 const shouldSavingData : MutableRefObject < boolean > = useRef < boolean > ( false )
3428 const shouldNavigateTo : MutableRefObject < string > = useRef < string > ( '' )
3529
36- const {
37- loading,
38- personalizationInfo,
39- setPersonalizationInfo,
40- } : useAutoSavePersonalizationType = useAutoSavePersonalization (
41- props . reduxPersonalizations ,
42- [ 'availableForGigs' ] ,
43- props . updateMemberPersonalizations ,
44- props . createMemberPersonalizations ,
45- shouldSavingData ,
46- )
47-
48- const availableForGigsValue : boolean | undefined = useMemo ( ( ) => {
49- if ( ! personalizationInfo || personalizationInfo . availableForGigs === undefined ) {
50- return blankPersonalizationInfo . availableForGigs
51- }
52-
53- return personalizationInfo . availableForGigs
54- } , [ personalizationInfo ] )
55-
5630 useEffect ( ( ) => {
5731 if ( ! loading && ! shouldSavingData . current && ! ! shouldNavigateTo . current ) {
5832 navigate ( shouldNavigateTo . current )
5933 }
6034 /* eslint-disable react-hooks/exhaustive-deps */
6135 } , [ loading ] )
6236
63- function checkToNavigateNextPage ( pageUrl : string ) : void {
64- if ( ! personalizationInfo || personalizationInfo . availableForGigs === undefined ) {
65- shouldNavigateTo . current = pageUrl
66- setPersonalizationInfo ( {
67- ...( personalizationInfo || { } ) ,
68- availableForGigs : blankPersonalizationInfo . availableForGigs ,
69- } )
70- } else {
71- navigate ( pageUrl )
72- }
37+ function goToPreviousStep ( ) : void {
38+ navigate ( '../skills' )
39+ }
40+
41+ function goToNextStep ( ) : void {
42+ navigate ( '../works' )
43+ }
44+
45+ async function handleSaveAvailableForGigs ( e : any ) : Promise < void > {
46+ setLoading ( true )
47+ await props . updateMemberOpenForWork ( e . target . checked )
48+ setLoading ( false )
7349 }
7450
7551 return (
@@ -90,15 +66,10 @@ export const PageOpenToWorkContent: FC<PageOpenToWorkContentProps> = props => {
9066 < div className = 'mt-26' >
9167 < FormInputCheckboxMiddleware
9268 label = 'Yes, I’m open to work'
93- checked = { availableForGigsValue }
69+ checked = { props . availableForGigs }
9470 inline
95- onChange = { function onChange ( e : any ) {
96- setPersonalizationInfo ( {
97- ...( personalizationInfo || { } ) ,
98- availableForGigs : e . target . checked ,
99- } )
100- } }
101- disabled = { props . loadingMemberTraits || loading }
71+ onChange = { handleSaveAvailableForGigs }
72+ disabled = { loading }
10273 />
10374 </ div >
10475 </ div >
@@ -117,18 +88,14 @@ export const PageOpenToWorkContent: FC<PageOpenToWorkContentProps> = props => {
11788 iconToLeft
11889 disabled = { loading }
11990 icon = { IconOutline . ChevronLeftIcon }
120- onClick = { function previousPage ( ) {
121- checkToNavigateNextPage ( '../skills' )
122- } }
91+ onClick = { goToPreviousStep }
12392 />
12493 < Button
12594 size = 'lg'
12695 primary
12796 iconToLeft
12897 disabled = { loading }
129- onClick = { function nextPage ( ) {
130- checkToNavigateNextPage ( '../works' )
131- } }
98+ onClick = { goToNextStep }
13299 >
133100 next
134101 </ Button >
@@ -137,22 +104,10 @@ export const PageOpenToWorkContent: FC<PageOpenToWorkContentProps> = props => {
137104 )
138105}
139106
140- const mapStateToProps : ( state : any ) => PageOpenToWorkContentReduxProps
141- = ( state : any ) : PageOpenToWorkContentReduxProps => {
142- const {
143- loadingMemberTraits,
144- personalizations,
145- } : any = state . member
146-
147- return {
148- loadingMemberTraits,
149- reduxPersonalizations : personalizations ,
150- }
151- }
107+ const mapStateToProps : any = ( state : any ) => pick ( state . member , 'availableForGigs' )
152108
153109const mapDispatchToProps : any = {
154- createMemberPersonalizations,
155- updateMemberPersonalizations,
110+ updateMemberOpenForWork,
156111}
157112
158113export const PageOpenToWork : any = connect ( mapStateToProps , mapDispatchToProps ) ( PageOpenToWorkContent )
0 commit comments