Skip to content

Commit 8580a56

Browse files
authored
Dev v0.0.18 (#47)
* Small auth context refactor to ensure customer state updated * Fixed customer state with update action in authReducer
1 parent 29c460e commit 8580a56

File tree

8 files changed

+24
-59
lines changed

8 files changed

+24
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fleetbase/storefront-app",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"private": true,
55
"scripts": {
66
"android": "react-native run-android",

src/contexts/AuthContext.native.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const authReducer = (state, action) => {
1515
switch (action.type) {
1616
case 'RESTORE_SESSION':
1717
return { ...state, customer: action.customer };
18+
case 'UPDATE':
19+
return { ...state, customer: action.customer };
1820
case 'LOGIN':
1921
return { ...state, phone: action.phone, isSendingCode: action.isSendingCode ?? false };
2022
case 'CREATING_ACCOUNT':
@@ -69,6 +71,7 @@ export const AuthProvider = ({ children }) => {
6971
}
7072

7173
setStoredCustomer(customerInstance.serialize());
74+
dispatch({ type: 'UPDATE', customer: customerInstance });
7275
EventRegister.emit('customer.updated', customerInstance);
7376
},
7477
[storefront, setStoredCustomer, authToken]
@@ -109,7 +112,7 @@ export const AuthProvider = ({ children }) => {
109112

110113
// Update customer meta attributes
111114
const updateCustomerMeta = async (newMeta = {}) => {
112-
const meta = { ...state.customer.getAttribute('meta'), ...newMeta };
115+
const meta = { ...state.customer.getAttribute('meta', {}), ...newMeta };
113116
try {
114117
const customer = await state.customer.update({ meta });
115118
setCustomer(customer);

src/contexts/AuthContext.web.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const authReducer = (state, action) => {
1414
switch (action.type) {
1515
case 'RESTORE_SESSION':
1616
return { ...state, customer: action.customer };
17+
case 'UPDATE':
18+
return { ...state, customer: action.customer };
1719
case 'LOGIN':
1820
return { ...state, phone: action.phone, isSendingCode: action.isSendingCode ?? false };
1921
case 'CREATING_ACCOUNT':
@@ -68,6 +70,7 @@ export const AuthProvider = ({ children }) => {
6870
}
6971

7072
setStoredCustomer(customerInstance.serialize());
73+
dispatch({ type: 'UPDATE', customer: customerInstance });
7174
EventRegister.emit('customer.updated', customerInstance);
7275
},
7376
[storefront, setStoredCustomer, authToken]
@@ -108,7 +111,7 @@ export const AuthProvider = ({ children }) => {
108111

109112
// Update customer meta attributes
110113
const updateCustomerMeta = async (newMeta = {}) => {
111-
const meta = { ...state.customer.getAttribute('meta'), ...newMeta };
114+
const meta = { ...state.customer.getAttribute('meta', {}), ...newMeta };
112115
try {
113116
const customer = await state.customer.update({ meta });
114117
setCustomer(customer);

src/hooks/use-qpay-checkout.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ export default function useQPayCheckout({ onOrderComplete }) {
2626
const { currentLocation: deliveryLocation, updateDefaultLocation } = useCurrentLocation();
2727
const { listen } = useSocketClusterClient();
2828
const [cart, updateCart] = useCart();
29-
const defaultCompanyRegistrationNo = useMemo(() => {
30-
return customer?.getAttribute('meta.ebarimt_registration_no', '') ?? '';
31-
}, [customer]);
3229
const [checkoutOptions, setCheckoutOptions] = useState({
3330
leavingTip: false,
3431
tip: 0,
3532
leavingDeliveryTip: false,
3633
deliveryTip: 0,
3734
pickup: storefrontConfig('prioritizePickup') ? 1 : 0,
38-
ebarimt_registration_no: defaultCompanyRegistrationNo,
3935
});
4036
const [invoice, setInvoice] = useState();
4137
const [checkoutId, setCheckoutId] = useState();
@@ -44,9 +40,17 @@ export default function useQPayCheckout({ onOrderComplete }) {
4440
const [isServiceQuoteUnavailable, setIsServiceQuoteUnavailable] = useState(false);
4541
const [isLoading, setIsLoading] = useState(true);
4642
const [isCapturingOrder, setIsCapturingOrder] = useState(false);
47-
const [isPersonal, setIsPersonal] = useState(isBlank(defaultCompanyRegistrationNo));
4843
const [error, setError] = useState(false);
44+
// Order notes
4945
const [orderNotes, setOrderNotes] = useStorage(`${customer?.id ?? 'anon'}_order_notes`, '');
46+
// Ebarimt company registration no
47+
const companyRegistrationNumber = useMemo(() => {
48+
if (customer && typeof customer.getAttribute === 'function') {
49+
return customer.getAttribute('meta.ebarimt_registration_no', '');
50+
}
51+
return '';
52+
}, [customer]);
53+
const [isPersonal, setIsPersonal] = useState(isBlank(companyRegistrationNumber));
5054
const listenerRef = useRef();
5155
const hasOrderCompleted = useRef(false);
5256
const cartContentsString = JSON.stringify(cart.contents() || []);
@@ -141,7 +145,7 @@ export default function useQPayCheckout({ onOrderComplete }) {
141145
const debouncedUpdateRegistration = useMemo(
142146
() =>
143147
debounce((registrationNumber) => {
144-
setCheckoutOptions((prev) => ({ ...prev, ebarimt_registration_no: registrationNumber }));
148+
updateCustomerMeta({ ebarimt_registration_no: registrationNumber });
145149
}, 500), // 500ms delay
146150
[]
147151
);
@@ -372,7 +376,7 @@ export default function useQPayCheckout({ onOrderComplete }) {
372376
isPersonal,
373377
setIsPersonal,
374378
isCompany: !isPersonal,
375-
companyRegistrationNumber: customer?.getAttribute('meta.ebarimt_registration_no', '') ?? '',
379+
companyRegistrationNumber,
376380
setCompanyRegistrationNumber,
377381
}),
378382
[
@@ -397,6 +401,7 @@ export default function useQPayCheckout({ onOrderComplete }) {
397401
isServiceQuoteUnavailable,
398402
isPersonal,
399403
setIsPersonal,
404+
companyRegistrationNumber,
400405
setCompanyRegistrationNumber,
401406
]
402407
);

src/screens/CartScreen.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useRef, useState, useEffect, useCallback } from 'react';
2-
import { useNavigation, useFocusEffect } from '@react-navigation/native';
2+
import { useNavigation } from '@react-navigation/native';
33
import { Animated, Pressable, StyleSheet, LayoutAnimation, UIManager, Platform } from 'react-native';
44
import { useSafeTabBarHeight as useBottomTabBarHeight } from '../hooks/use-safe-tab-bar-height';
55
import { useSafeAreaInsets } from 'react-native-safe-area-context';
@@ -149,13 +149,6 @@ const CartScreen = ({ route }) => {
149149
setDisplayedItems(cart ? cart.contents() : []);
150150
}, [cart]);
151151

152-
// Run once
153-
useFocusEffect(
154-
useCallback(() => {
155-
refreshCustomer();
156-
}, [])
157-
);
158-
159152
const renderRightActions = (cartItem) => (
160153
<XStack height='100%' width={200} minHeight={100} maxHeight={125}>
161154
<Pressable style={{ flex: 1 }} onPress={() => handleEdit(cartItem)}>

test-vehicle-instantiation.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

translations/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@
285285
"vatRegistration": "VAT Registration",
286286
"personal": "Personal",
287287
"company": "Company",
288-
"companyRegistrationNumber": "Company Registration No."
288+
"companyRegistrationNumber": "Company Registration"
289289
},
290290
"SavedLocationScreen": {
291291
"addressSaved": "Address saved.",

translations/mn.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@
283283
"vatRegistration": "НӨАТ Registration",
284284
"personal": "Хувь хүн",
285285
"company": "Байгуулга",
286-
"companyRegistrationNumber": "Компанийн регистерийн дугаар"
286+
"companyRegistrationNumber": "Компани рд"
287287
},
288288
"SavedLocationScreen": {
289289
"addressSaved": "Хаяг хадгалагдсан.",

0 commit comments

Comments
 (0)