|
| 1 | +import type { FC } from 'react'; |
| 2 | +import { useMemo, useCallback, useEffect, useState } from 'react'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { Box } from 'react-native-flex-layout'; |
| 5 | +import { logger } from 'react-native-logs'; |
| 6 | +import type { ModalProps } from 'react-native-paper'; |
| 7 | +import { Button, Portal, Text, useTheme } from 'react-native-paper'; |
| 8 | + |
| 9 | +import { updateDtuUserString } from '@/slices/settings'; |
| 10 | + |
| 11 | +import BaseModal from '@/components/BaseModal'; |
| 12 | +import StyledTextInput from '@/components/styled/StyledTextInput'; |
| 13 | + |
| 14 | +import { useApi } from '@/api/ApiHandler'; |
| 15 | +import { defaultUser } from '@/constants'; |
| 16 | +import { useAppDispatch, useAppSelector } from '@/store'; |
| 17 | + |
| 18 | +export interface ChangeOpendtuCredentialsModalProps |
| 19 | + extends Omit<ModalProps, 'children'> { |
| 20 | + index: number; |
| 21 | +} |
| 22 | + |
| 23 | +const log = logger.createLogger(); |
| 24 | + |
| 25 | +const ChangeOpendtuCredentialsModal: FC< |
| 26 | + ChangeOpendtuCredentialsModalProps |
| 27 | +> = props => { |
| 28 | + const { onDismiss, index } = props; |
| 29 | + const dispatch = useAppDispatch(); |
| 30 | + const theme = useTheme(); |
| 31 | + const { t } = useTranslation(); |
| 32 | + |
| 33 | + const currentUserString = useAppSelector( |
| 34 | + state => state.settings.dtuConfigs[index].userString, |
| 35 | + ); |
| 36 | + |
| 37 | + const currentUsername = useMemo(() => { |
| 38 | + if (!currentUserString) return null; |
| 39 | + |
| 40 | + return atob(currentUserString).split(':')[0]; |
| 41 | + }, [currentUserString]); |
| 42 | + |
| 43 | + const [username, setUsername] = useState<string>( |
| 44 | + currentUsername ?? defaultUser, |
| 45 | + ); |
| 46 | + const [password, setPassword] = useState<string>(''); |
| 47 | + |
| 48 | + const [loading, setLoading] = useState<boolean>(false); |
| 49 | + const [error, setError] = useState<string | null>(null); |
| 50 | + |
| 51 | + const openDtuApi = useApi(); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + if (currentUsername) { |
| 55 | + setUsername(currentUsername); |
| 56 | + } |
| 57 | + }, [currentUsername]); |
| 58 | + |
| 59 | + const handleAbort = useCallback(() => { |
| 60 | + onDismiss?.(); |
| 61 | + }, [onDismiss]); |
| 62 | + |
| 63 | + const handleSave = useCallback(async () => { |
| 64 | + if (!username || !password) return; |
| 65 | + |
| 66 | + setLoading(true); |
| 67 | + setError(null); |
| 68 | + |
| 69 | + const result = await openDtuApi.checkCredentials({ |
| 70 | + username, |
| 71 | + password, |
| 72 | + }); |
| 73 | + |
| 74 | + if (result === false) { |
| 75 | + setError(t('setup.errors.invalidCredentials')); |
| 76 | + log.info('Invalid credentials'); |
| 77 | + setLoading(false); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (result?.authdata) { |
| 82 | + onDismiss?.(); |
| 83 | + setPassword(''); |
| 84 | + setLoading(false); |
| 85 | + |
| 86 | + dispatch(updateDtuUserString({ userString: result.authdata, index })); |
| 87 | + |
| 88 | + return; |
| 89 | + } else { |
| 90 | + setError(t('setup.errors.genericError')); |
| 91 | + log.info('Something went wrong! Please try again.'); |
| 92 | + } |
| 93 | + |
| 94 | + setLoading(false); |
| 95 | + |
| 96 | + onDismiss?.(); |
| 97 | + }, [openDtuApi, username, password, dispatch, index, onDismiss, t]); |
| 98 | + |
| 99 | + const handleClearCredentials = useCallback(() => { |
| 100 | + dispatch(updateDtuUserString({ userString: null, index })); |
| 101 | + onDismiss?.(); |
| 102 | + }, [dispatch, index, onDismiss]); |
| 103 | + |
| 104 | + return ( |
| 105 | + <Portal> |
| 106 | + <BaseModal {...props}> |
| 107 | + <Box p={16}> |
| 108 | + <Box mb={8}> |
| 109 | + <Text variant="bodyLarge"> |
| 110 | + {t('settings.changeOpendtuCredentials')} |
| 111 | + </Text> |
| 112 | + </Box> |
| 113 | + <StyledTextInput |
| 114 | + label={t('setup.username')} |
| 115 | + mode="outlined" |
| 116 | + value={username} |
| 117 | + onChangeText={setUsername} |
| 118 | + textContentType="username" |
| 119 | + style={{ backgroundColor: theme.colors.elevation.level3 }} |
| 120 | + /> |
| 121 | + <StyledTextInput |
| 122 | + label={t('setup.password')} |
| 123 | + mode="outlined" |
| 124 | + value={password} |
| 125 | + onChangeText={setPassword} |
| 126 | + textContentType="password" |
| 127 | + style={{ backgroundColor: theme.colors.elevation.level3 }} |
| 128 | + /> |
| 129 | + {error ? ( |
| 130 | + <Box mt={8}> |
| 131 | + <Text variant="bodySmall" style={{ color: theme.colors.error }}> |
| 132 | + {error} |
| 133 | + </Text> |
| 134 | + </Box> |
| 135 | + ) : null} |
| 136 | + </Box> |
| 137 | + <Box |
| 138 | + style={{ |
| 139 | + flexDirection: 'row', |
| 140 | + justifyContent: 'flex-end', |
| 141 | + alignItems: 'center', |
| 142 | + padding: 8, |
| 143 | + }} |
| 144 | + > |
| 145 | + <Button |
| 146 | + mode="text" |
| 147 | + onPress={handleClearCredentials} |
| 148 | + style={{ marginRight: 8 }} |
| 149 | + disabled={loading} |
| 150 | + > |
| 151 | + {t('clear')} |
| 152 | + </Button> |
| 153 | + <Button |
| 154 | + mode="text" |
| 155 | + onPress={handleAbort} |
| 156 | + style={{ marginRight: 8 }} |
| 157 | + disabled={loading} |
| 158 | + > |
| 159 | + {t('cancel')} |
| 160 | + </Button> |
| 161 | + <Button |
| 162 | + mode="text" |
| 163 | + onPress={handleSave} |
| 164 | + disabled={loading} |
| 165 | + loading={loading} |
| 166 | + > |
| 167 | + {t('change')} |
| 168 | + </Button> |
| 169 | + </Box> |
| 170 | + </BaseModal> |
| 171 | + </Portal> |
| 172 | + ); |
| 173 | +}; |
| 174 | + |
| 175 | +export default ChangeOpendtuCredentialsModal; |
0 commit comments