2022-09-06 16:53:21 +00:00
|
|
|
import { useNavigation } from '@react-navigation/native';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import React, { useEffect, useLayoutEffect, useState } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { ScrollView, StyleSheet, Text } from 'react-native';
|
2022-09-06 16:53:21 +00:00
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
import * as yup from 'yup';
|
|
|
|
import { yupResolver } from '@hookform/resolvers/yup';
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2022-02-18 13:53:36 +00:00
|
|
|
import { loginRequest } from '../actions/login';
|
2018-11-14 21:42:03 +00:00
|
|
|
import Button from '../containers/Button';
|
2022-02-18 13:53:36 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2022-09-06 16:53:21 +00:00
|
|
|
import { ControlledFormTextInput } from '../containers/TextInput';
|
2023-08-18 17:48:33 +00:00
|
|
|
import { SetUsernameStackParamList } from '../definitions/navigationTypes';
|
2018-11-14 21:42:03 +00:00
|
|
|
import I18n from '../i18n';
|
2022-04-13 20:43:56 +00:00
|
|
|
import KeyboardView from '../containers/KeyboardView';
|
2022-02-18 13:53:36 +00:00
|
|
|
import { getUserSelector } from '../selectors/login';
|
2022-09-06 16:53:21 +00:00
|
|
|
import { useTheme } from '../theme';
|
2023-09-08 13:51:12 +00:00
|
|
|
import { showErrorAlert } from '../lib/methods/helpers';
|
2022-06-06 14:17:51 +00:00
|
|
|
import scrollPersistTaps from '../lib/methods/helpers/scrollPersistTaps';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2022-09-06 16:53:21 +00:00
|
|
|
import { useAppSelector } from '../lib/hooks';
|
2018-11-14 21:42:03 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
loginTitle: {
|
|
|
|
marginVertical: 0,
|
|
|
|
marginTop: 15
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
interface ISubmit {
|
2021-12-02 13:29:01 +00:00
|
|
|
username: string;
|
|
|
|
}
|
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
const schema = yup.object().shape({
|
|
|
|
username: yup.string().required()
|
|
|
|
});
|
2021-12-02 13:29:01 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
const SetUsernameView = () => {
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
handleSubmit,
|
|
|
|
formState: { isValid },
|
|
|
|
setValue
|
|
|
|
} = useForm<ISubmit>({ mode: 'onChange', resolver: yupResolver(schema) });
|
|
|
|
const [loading, setLoading] = useState(false);
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
const { colors } = useTheme();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { server, token } = useAppSelector(state => ({ server: state.server.server, token: getUserSelector(state).token }));
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
const navigation = useNavigation<StackNavigationProp<SetUsernameStackParamList, 'SetUsernameView'>>();
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({ title: server });
|
|
|
|
}, [navigation, server]);
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const init = async () => {
|
|
|
|
const suggestion = await Services.getUsernameSuggestion();
|
|
|
|
if (suggestion.success) {
|
|
|
|
setValue('username', suggestion.result, { shouldValidate: true });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
init();
|
|
|
|
}, []);
|
2018-12-05 20:52:08 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
const submit = async ({ username }: ISubmit) => {
|
|
|
|
if (!isValid) {
|
2018-12-05 20:52:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-06 16:53:21 +00:00
|
|
|
setLoading(true);
|
2018-12-05 20:52:08 +00:00
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.saveUserProfile({ username });
|
2022-02-18 13:53:36 +00:00
|
|
|
dispatch(loginRequest({ resume: token }));
|
2021-12-02 13:29:01 +00:00
|
|
|
} catch (e: any) {
|
2020-02-21 15:53:07 +00:00
|
|
|
showErrorAlert(e.message, I18n.t('Oops'));
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
2022-09-06 16:53:21 +00:00
|
|
|
setLoading(false);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
return (
|
|
|
|
<KeyboardView style={{ backgroundColor: colors.auxiliaryBackground }} contentContainerStyle={sharedStyles.container}>
|
|
|
|
<StatusBar />
|
|
|
|
<ScrollView {...scrollPersistTaps} contentContainerStyle={sharedStyles.containerScrollView}>
|
|
|
|
<SafeAreaView testID='set-username-view'>
|
|
|
|
<Text style={[sharedStyles.loginTitle, sharedStyles.textBold, styles.loginTitle, { color: colors.titleText }]}>
|
|
|
|
{I18n.t('Username')}
|
|
|
|
</Text>
|
|
|
|
<Text style={[sharedStyles.loginSubtitle, sharedStyles.textRegular, { color: colors.titleText }]}>
|
|
|
|
{I18n.t('Set_username_subtitle')}
|
|
|
|
</Text>
|
|
|
|
<ControlledFormTextInput
|
|
|
|
control={control}
|
|
|
|
name='username'
|
|
|
|
autoFocus
|
|
|
|
placeholder={I18n.t('Username')}
|
|
|
|
returnKeyType='send'
|
|
|
|
onSubmitEditing={handleSubmit(submit)}
|
|
|
|
testID='set-username-view-input'
|
|
|
|
clearButtonMode='while-editing'
|
|
|
|
containerStyle={sharedStyles.inputLastChild}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Register')}
|
|
|
|
type='primary'
|
|
|
|
onPress={handleSubmit(submit)}
|
|
|
|
testID='set-username-view-submit'
|
|
|
|
disabled={!isValid}
|
|
|
|
loading={loading}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
};
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2022-09-06 16:53:21 +00:00
|
|
|
export default SetUsernameView;
|