Fakultas Ilmu Komputer UI

Commit 81927ccb authored by wulanmantiri's avatar wulanmantiri

[REFACTOR] Fix signup, validation, quiz result bugs

parent f3928d0b
Pipeline #84574 passed with stages
in 8 minutes and 36 seconds
......@@ -42,6 +42,8 @@ export const dietReportTextFields: { [_: string]: TextFieldSchema[] } = {
name: 'water_consumption',
required: true,
placeholder: 'ex: 8',
fieldType: FieldType.NUMBER,
max: 20,
keyboardType: 'numeric',
errorMessage: 'Konsumsi minuman',
},
......
......@@ -21,6 +21,7 @@ import {
import { setAuthHeader, resetAuthHeader } from 'services/api';
import { iUserContext } from './types';
import { TransactionStatus } from 'services/payment/models';
export const initialUser = {
id: null,
......@@ -88,27 +89,39 @@ export const useUserContext = (): iUserContext => {
setUser(data.user);
};
const linkUserData = async (email: string) => {
// For signup, link user to cart and diet profile
const linkUserData = async (data: LoginResponse) => {
const dietProfileId = await getCache(CACHE_KEYS.dietProfileId);
const cartId = await getCache(CACHE_KEYS.cartId);
if (dietProfileId && cartId) {
const response = await linkUserDataApi({
email,
email: data.user.email,
diet_profile_id: parseInt(dietProfileId, 10),
cart_id: parseInt(cartId, 10),
});
return response;
await authSuccess({
...data,
user: {
...data.user,
cart_id: response.data?.cart.id || null,
transaction_status: TransactionStatus.UNPAID,
},
});
} else {
await logout();
Toast.show({
type: 'error',
text1: 'Gagal registrasi akun',
text2: 'Terjadi kesalahan di sisi kami. Silakan coba lagi',
});
}
return {
success: false,
};
};
const signup = async (registerData: RegistrationRequest) => {
const response = await signupApi(registerData);
if (response.success && response.data) {
await linkUserData(response.data.user.email);
await authSuccess(response.data);
await linkUserData(response.data);
}
return response;
};
......@@ -130,20 +143,8 @@ export const useUserContext = (): iUserContext => {
access_token: tokens.accessToken,
});
if (response.success && response.data) {
// If signup, link user to cart and diet profile
if (!isLogin) {
const linkResponse = await linkUserData(response.data.user.email);
if (!linkResponse.success) {
await logout();
Toast.show({
type: 'error',
text1: 'Gagal registrasi dengan Google',
text2: 'Terjadi kesalahan di sisi kami. Silakan coba lagi',
});
}
}
await authSuccess(response.data);
const authProcess = isLogin ? authSuccess : linkUserData;
await authProcess(response.data);
} else {
await logout();
}
......
......@@ -48,8 +48,7 @@ const statusBeratBadan = (response: DietProfileResponse): ResultPageContent => {
},
},
{
header:
'Untuk orang dengan tinggi 156 cm, berikut rentang berat badan yang ideal:',
header: `Untuk orang dengan tinggi ${height} cm, berikut rentang berat badan yang ideal:`,
content: {
statistics: [
[
......
......@@ -40,7 +40,7 @@ describe('WeeklyReport', () => {
sweet_snacks: 1,
fruits_portion: 1,
vegetables_portion: 1,
water_consumption: '80',
water_consumption: '8',
physical_activity: [1],
physical_activity_other: '',
time_for_activity: 1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment