|
1 | 1 | import * as React from 'react' |
2 | 2 |
|
3 | 3 | import { setPasswordReminder } from '../../actions/LocalSettingsActions' |
4 | | -import { connect } from '../../types/reactRedux' |
5 | | -import type { PasswordReminder } from '../../types/types' |
| 4 | +import { useAsyncEffect } from '../../hooks/useAsyncEffect' |
| 5 | +import { |
| 6 | + initialState, |
| 7 | + type PasswordReminderState |
| 8 | +} from '../../reducers/PasswordReminderReducer' |
| 9 | +import { useDispatch, useSelector } from '../../types/reactRedux' |
6 | 10 | import { matchJson } from '../../util/matchJson' |
7 | | -import { showError } from './AirshipInstance' |
8 | 11 |
|
9 | | -interface StateProps { |
10 | | - settingsLoaded: boolean | null |
11 | | - passwordReminder: PasswordReminder |
12 | | -} |
13 | | -interface DispatchProps { |
14 | | - setPasswordReminder: (passwordReminder: PasswordReminder) => Promise<void> |
15 | | -} |
16 | | -type Props = StateProps & DispatchProps |
| 12 | +interface Props {} |
17 | 13 |
|
18 | | -class PasswordReminderComponent extends React.PureComponent<Props> { |
19 | | - componentDidUpdate(prevProps: Props): void { |
20 | | - if ( |
21 | | - this.props.settingsLoaded === true && |
22 | | - !matchJson(prevProps.passwordReminder, this.props.passwordReminder) |
23 | | - ) { |
24 | | - this.props |
25 | | - .setPasswordReminder(this.props.passwordReminder) |
26 | | - .catch((error: unknown) => { |
27 | | - showError(error) |
28 | | - }) |
29 | | - } |
30 | | - } |
| 14 | +export const PasswordReminderService: React.FC<Props> = props => { |
| 15 | + const settingsLoaded = |
| 16 | + useSelector(state => state.ui.settings.settingsLoaded) ?? false |
| 17 | + const passwordReminder = useSelector(state => state.ui.passwordReminder) |
| 18 | + const lastPasswordReminder = React.useRef<PasswordReminderState>(initialState) |
| 19 | + const dispatch = useDispatch() |
31 | 20 |
|
32 | | - render(): React.JSX.Element | null { |
33 | | - return null |
34 | | - } |
35 | | -} |
| 21 | + useAsyncEffect( |
| 22 | + async () => { |
| 23 | + if ( |
| 24 | + settingsLoaded && |
| 25 | + !matchJson(passwordReminder, lastPasswordReminder.current) |
| 26 | + ) { |
| 27 | + lastPasswordReminder.current = passwordReminder |
| 28 | + await dispatch(setPasswordReminder(passwordReminder)) |
| 29 | + } |
| 30 | + }, |
| 31 | + [settingsLoaded, passwordReminder], |
| 32 | + 'PasswordReminderService' |
| 33 | + ) |
36 | 34 |
|
37 | | -export const PasswordReminderService = connect< |
38 | | - StateProps, |
39 | | - DispatchProps, |
40 | | - unknown |
41 | | ->( |
42 | | - state => ({ |
43 | | - settingsLoaded: state.ui.settings.settingsLoaded, |
44 | | - passwordReminder: state.ui.passwordReminder |
45 | | - }), |
46 | | - dispatch => ({ |
47 | | - async setPasswordReminder(passwordReminder: PasswordReminder) { |
48 | | - await dispatch(setPasswordReminder(passwordReminder)) |
49 | | - } |
50 | | - }) |
51 | | -)(PasswordReminderComponent) |
| 35 | + return null |
| 36 | +} |
0 commit comments