Skip to content

Commit

Permalink
Merge pull request #555 from ikuseiGmbH/feature/HDVT-39-registration-…
Browse files Browse the repository at this point in the history
…one-time-code

feat(volunteer): integrate one time code in registration
  • Loading branch information
donni106 authored Jun 20, 2022
2 parents 419cd5d + 8e691ce commit f890d8f
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 44 deletions.
12 changes: 9 additions & 3 deletions src/config/navigation/defaultStackConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
BookmarkScreen,
ConstructionSiteDetailScreen,
ConstructionSiteOverviewScreen,
ConsulDetailScreen,
ConsulHomeScreen,
ConsulIndexScreen,
ConsulLoginScreen,
ConsulRegisterScreen,
ConsulRegisteredScreen,
ConsulIndexScreen,
ConsulDetailScreen,
ConsulRegisterScreen,
ConsulStartNewScreen,
DataProviderScreen,
DetailScreen,
Expand Down Expand Up @@ -49,6 +49,7 @@ import {
VolunteerPersonalScreen,
VolunteerRegisteredScreen,
VolunteerRegistrationScreen,
VolunteerSignupScreen,
WasteCollectionScreen,
WasteReminderScreen,
WeatherScreen,
Expand Down Expand Up @@ -337,6 +338,11 @@ export const defaultStackConfig = ({
screenComponent: VolunteerRegistrationScreen,
screenOptions: { title: texts.screenTitles.volunteer.home }
},
{
routeName: ScreenName.VolunteerSignup,
screenComponent: VolunteerSignupScreen,
screenOptions: { title: texts.screenTitles.volunteer.home }
},
{
routeName: ScreenName.WasteCollection,
screenComponent: WasteCollectionScreen,
Expand Down
5 changes: 5 additions & 0 deletions src/config/texts.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ export const texts = {
emailInvalid: 'E-Mail ungültig',
endDate: 'Enddatum',
endTime: 'Endzeit',
enterCode: 'Code eingeben',
entranceFee: 'Eintrittspreis',
errorLoadingUser: 'Beim Laden deiner Daten ist ein Fehler aufgetreten. Bitte erneut einloggen.',
eventRecord: {
Expand Down Expand Up @@ -833,10 +834,14 @@ export const texts = {
save: 'Speichern',
search: 'Suche',
send: 'Senden',
signupFailedBody: 'Bitte Eingaben überprüfen und erneut versuchen.',
signupFailedTitle: 'Fehler bei der Registrierung',
startDate: 'Startdatum',
startTime: 'Startzeit',
tags: 'Themen',
title: 'Titel',
token: 'Code',
tokenError: 'Code muss ausgefüllt werden',
topics: 'Themen',
username: 'Benutzername',
usernameError: 'Benutzername muss ausgefüllt werden',
Expand Down
22 changes: 21 additions & 1 deletion src/queries/volunteer/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
volunteerApiV2Url,
volunteerAuthToken
} from '../../helpers/volunteerHelper';
import { VolunteerRegistration } from '../../types';
import { VolunteerRegistration, VolunteerSignup } from '../../types';

export const logIn = async ({ username, password }: { username: string; password: string }) => {
const formData = new FormData();
Expand Down Expand Up @@ -55,6 +55,26 @@ export const register = async ({
return (await fetch(`${volunteerApiV2Url}auth/register`, fetchObj)).json();
};

export const signup = async ({ email, token }: VolunteerSignup) => {
const formData = {
signup: {
email,
token
}
};

const fetchObj = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
};

return (await fetch(`${volunteerApiV2Url}auth/signup`, fetchObj)).json();
};

// TODO: possible and needed?
export const logOut = async () => {
const authToken = await volunteerAuthToken();
Expand Down
24 changes: 15 additions & 9 deletions src/screens/volunteer/VolunteerLoginScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,28 @@ export const VolunteerLoginScreen = ({ navigation }: StackScreenProps<any>) => {
data: dataMe
} = useQuery(QUERY_TYPES.VOLUNTEER.ME, me, {
enabled: !!data?.auth_token, // the query will not execute until the auth token exists
onSuccess: (dataMe) => {
if (dataMe?.account) {
// save user data to global state
storeVolunteerUserData(dataMe.account);

// refreshUser param causes the home screen to update and no longer show the welcome component
navigation.navigate(ScreenName.VolunteerHome, { refreshUser: new Date().valueOf() });
onSuccess: (responseData) => {
if (!responseData?.account) {
return;
}

// save user data to global state
storeVolunteerUserData(responseData.account);

// refreshUser param causes the home screen to update and no longer show the welcome component
navigation.navigate(ScreenName.VolunteerHome, { refreshUser: new Date().valueOf() });
}
});

const onSubmit = (loginData: VolunteerLogin) =>
mutateLogIn(loginData, {
onSuccess: (data) => {
onSuccess: (responseData) => {
if (!responseData?.auth_token) {
return;
}

// wait for saving auth token to global state
return storeVolunteerAuthToken(data.auth_token);
return storeVolunteerAuthToken(responseData.auth_token);
}
});

Expand Down
51 changes: 20 additions & 31 deletions src/screens/volunteer/VolunteerRegistrationScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StackScreenProps } from '@react-navigation/stack';
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { Alert, ScrollView, StyleSheet } from 'react-native';
import { useMutation, useQuery } from 'react-query';
import { useMutation } from 'react-query';

import * as appJson from '../../../app.json';
import {
Expand All @@ -13,6 +13,7 @@ import {
Input,
InputSecureTextIcon,
LoadingModal,
RegularText,
SafeAreaViewFlex,
Title,
TitleContainer,
Expand All @@ -22,9 +23,7 @@ import {
WrapperWithOrientation
} from '../../components';
import { consts, secrets, texts } from '../../config';
import { storeVolunteerAuthToken, storeVolunteerUserData } from '../../helpers';
import { QUERY_TYPES } from '../../queries';
import { me, register } from '../../queries/volunteer';
import { register } from '../../queries/volunteer';
import { ScreenName, VolunteerRegistration } from '../../types';

const { a11yLabel, EMAIL_REGEX } = consts;
Expand Down Expand Up @@ -62,43 +61,27 @@ export const VolunteerRegistrationScreen = ({ navigation }: StackScreenProps<any
const { mutate: mutateRegister, isLoading, isError, isSuccess, data, reset } = useMutation(
register
);
const {
isLoading: isLoadingMe,
isError: isErrorMe,
isSuccess: isSuccessMe,
data: dataMe
} = useQuery(QUERY_TYPES.VOLUNTEER.ME, me, {
enabled: !!data?.auth_token, // the query will not execute until the auth token exists
onSuccess: (dataMe) => {
if (dataMe?.account) {
// save user data to global state
storeVolunteerUserData(dataMe.account);

navigation.navigate(ScreenName.VolunteerRegistered);
}
}
});

const onSubmit = (registerData: VolunteerRegistration) => {
if (!hasAcceptedDataPrivacy) return showPrivacyCheckedAlert();

mutateRegister(
{ ...registerData, dataPrivacyCheck: hasAcceptedDataPrivacy },
{
onSuccess: (data) => {
// wait for saving auth token to global state
return storeVolunteerAuthToken(data.auth_token);
onSuccess: (responseData) => {
if (responseData?.code !== 200) {
return;
}

navigation.navigate(ScreenName.VolunteerSignup, {
email: registerData.email
});
}
}
);
};

if (
isError ||
isErrorMe ||
(isSuccess && data?.code && data?.code !== 200) ||
(isSuccessMe && dataMe?.status && dataMe?.status !== 200)
) {
if (isError || (isSuccess && data?.code && data?.code !== 200)) {
showRegistrationFailAlert();
reset();
}
Expand Down Expand Up @@ -220,8 +203,14 @@ export const VolunteerRegistrationScreen = ({ navigation }: StackScreenProps<any
<Button
onPress={handleSubmit(onSubmit)}
title={texts.volunteer.next}
disabled={isLoading || isLoadingMe}
disabled={isLoading}
/>
<Touchable onPress={() => navigation.navigate(ScreenName.VolunteerSignup)}>
<BoldText center primary underline>
{texts.volunteer.enterCode.toUpperCase()}
</BoldText>
</Touchable>
<RegularText />
<Touchable onPress={() => navigation.goBack()}>
<BoldText center primary underline>
{texts.volunteer.abort.toUpperCase()}
Expand All @@ -230,7 +219,7 @@ export const VolunteerRegistrationScreen = ({ navigation }: StackScreenProps<any
</Wrapper>
</WrapperWithOrientation>

<LoadingModal loading={isLoading || isLoadingMe} />
<LoadingModal loading={isLoading} />
</ScrollView>
</DefaultKeyboardAvoidingView>
</SafeAreaViewFlex>
Expand Down
Loading

0 comments on commit f890d8f

Please sign in to comment.