Changeset View
Changeset View
Standalone View
Standalone View
native/account/register-panel.react.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { | import { | ||||
Text, | Text, | ||||
View, | View, | ||||
StyleSheet, | StyleSheet, | ||||
Platform, | Platform, | ||||
Keyboard, | Keyboard, | ||||
Alert, | Alert, | ||||
Linking, | Linking, | ||||
} from 'react-native'; | } from 'react-native'; | ||||
import Animated from 'react-native-reanimated'; | import Animated from 'react-native-reanimated'; | ||||
import { useDispatch } from 'react-redux'; | |||||
import { setDataLoadedActionType } from 'lib/actions/client-db-store-actions.js'; | |||||
import { registerActionTypes, register } from 'lib/actions/user-actions.js'; | import { registerActionTypes, register } from 'lib/actions/user-actions.js'; | ||||
import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; | import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; | ||||
import { validUsernameRegex } from 'lib/shared/account-utils.js'; | import { validUsernameRegex } from 'lib/shared/account-utils.js'; | ||||
import type { | import type { | ||||
RegisterInfo, | RegisterInfo, | ||||
LogInExtraInfo, | LogInExtraInfo, | ||||
RegisterResult, | RegisterResult, | ||||
LogInStartingPayload, | LogInStartingPayload, | ||||
} from 'lib/types/account-types.js'; | } from 'lib/types/account-types.js'; | ||||
import type { LoadingStatus } from 'lib/types/loading-types.js'; | import type { LoadingStatus } from 'lib/types/loading-types.js'; | ||||
import type { Dispatch } from 'lib/types/redux-types.js'; | |||||
import { | import { | ||||
useServerCall, | useServerCall, | ||||
useDispatchActionPromise, | useDispatchActionPromise, | ||||
type DispatchActionPromise, | type DispatchActionPromise, | ||||
} from 'lib/utils/action-utils.js'; | } from 'lib/utils/action-utils.js'; | ||||
import { TextInput } from './modal-components.react.js'; | import { TextInput } from './modal-components.react.js'; | ||||
import { setNativeCredentials } from './native-credentials.js'; | import { setNativeCredentials } from './native-credentials.js'; | ||||
Show All 15 Lines | type BaseProps = { | ||||
+setActiveAlert: (activeAlert: boolean) => void, | +setActiveAlert: (activeAlert: boolean) => void, | ||||
+opacityValue: Animated.Node, | +opacityValue: Animated.Node, | ||||
+registerState: StateContainer<RegisterState>, | +registerState: StateContainer<RegisterState>, | ||||
}; | }; | ||||
type Props = { | type Props = { | ||||
...BaseProps, | ...BaseProps, | ||||
+loadingStatus: LoadingStatus, | +loadingStatus: LoadingStatus, | ||||
+logInExtraInfo: () => Promise<LogInExtraInfo>, | +logInExtraInfo: () => Promise<LogInExtraInfo>, | ||||
+dispatch: Dispatch, | |||||
+dispatchActionPromise: DispatchActionPromise, | +dispatchActionPromise: DispatchActionPromise, | ||||
+register: (registerInfo: RegisterInfo) => Promise<RegisterResult>, | +register: (registerInfo: RegisterInfo) => Promise<RegisterResult>, | ||||
+getInitialNotificationsEncryptedMessage: () => Promise<string>, | +getInitialNotificationsEncryptedMessage: () => Promise<string>, | ||||
}; | }; | ||||
type State = { | type State = { | ||||
+confirmPasswordFocused: boolean, | +confirmPasswordFocused: boolean, | ||||
}; | }; | ||||
class RegisterPanel extends React.PureComponent<Props, State> { | class RegisterPanel extends React.PureComponent<Props, State> { | ||||
▲ Show 20 Lines • Show All 270 Lines • ▼ Show 20 Lines | class RegisterPanel extends React.PureComponent<Props, State> { | ||||
async registerAction(extraInfo: LogInExtraInfo) { | async registerAction(extraInfo: LogInExtraInfo) { | ||||
try { | try { | ||||
const result = await this.props.register({ | const result = await this.props.register({ | ||||
...extraInfo, | ...extraInfo, | ||||
username: this.props.registerState.state.usernameInputText, | username: this.props.registerState.state.usernameInputText, | ||||
password: this.props.registerState.state.passwordInputText, | password: this.props.registerState.state.passwordInputText, | ||||
}); | }); | ||||
this.props.setActiveAlert(false); | this.props.setActiveAlert(false); | ||||
this.props.dispatch({ | |||||
type: setDataLoadedActionType, | |||||
payload: { | |||||
dataLoaded: true, | |||||
}, | |||||
}); | |||||
await setNativeCredentials({ | await setNativeCredentials({ | ||||
username: result.currentUserInfo.username, | username: result.currentUserInfo.username, | ||||
password: this.props.registerState.state.passwordInputText, | password: this.props.registerState.state.passwordInputText, | ||||
}); | }); | ||||
return result; | return result; | ||||
} catch (e) { | } catch (e) { | ||||
if (e.message === 'username_reserved') { | if (e.message === 'username_reserved') { | ||||
Alert.alert( | Alert.alert( | ||||
▲ Show 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | React.memo<BaseProps>(function ConnectedRegisterPanel(props: BaseProps) { | ||||
const navContext = React.useContext(NavContext); | const navContext = React.useContext(NavContext); | ||||
const logInExtraInfo = useSelector(state => | const logInExtraInfo = useSelector(state => | ||||
nativeLogInExtraInfoSelector({ | nativeLogInExtraInfoSelector({ | ||||
redux: state, | redux: state, | ||||
navContext, | navContext, | ||||
}), | }), | ||||
); | ); | ||||
const dispatch = useDispatch(); | |||||
const dispatchActionPromise = useDispatchActionPromise(); | const dispatchActionPromise = useDispatchActionPromise(); | ||||
const callRegister = useServerCall(register); | const callRegister = useServerCall(register); | ||||
const getInitialNotificationsEncryptedMessage = | const getInitialNotificationsEncryptedMessage = | ||||
useInitialNotificationsEncryptedMessage(); | useInitialNotificationsEncryptedMessage(); | ||||
return ( | return ( | ||||
<RegisterPanel | <RegisterPanel | ||||
{...props} | {...props} | ||||
loadingStatus={loadingStatus} | loadingStatus={loadingStatus} | ||||
logInExtraInfo={logInExtraInfo} | logInExtraInfo={logInExtraInfo} | ||||
dispatch={dispatch} | |||||
dispatchActionPromise={dispatchActionPromise} | dispatchActionPromise={dispatchActionPromise} | ||||
register={callRegister} | register={callRegister} | ||||
getInitialNotificationsEncryptedMessage={ | getInitialNotificationsEncryptedMessage={ | ||||
getInitialNotificationsEncryptedMessage | getInitialNotificationsEncryptedMessage | ||||
} | } | ||||
/> | /> | ||||
); | ); | ||||
}); | }); | ||||
export default ConnectedRegisterPanel; | export default ConnectedRegisterPanel; |