Skip to content

Commit 78894a0

Browse files
committed
Replace onMounted with initialState for orderInput & customer
1 parent cf8e60d commit 78894a0

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

woonuxt_base/app/composables/useAuth.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@ export const useAuth = () => {
55
const { refreshCart } = useCart();
66
const { logGQLError } = useHelpers();
77

8-
const customer = useState<Customer>('customer', () => ({ billing: {}, shipping: {} }));
8+
const customer = useState<Customer>('customer', () => {
9+
10+
if (!import.meta.env.SSR) {
11+
const savedCustomer = localStorage.getItem('WooNuxtCustomer');
12+
if (savedCustomer) {
13+
try {
14+
return JSON.parse(savedCustomer) as Customer;
15+
} catch (e) {
16+
localStorage.removeItem('WooNuxtCustomer');
17+
console.error('Failed to parse saved customer:', e);
18+
}
19+
}
20+
}
21+
return { billing: {}, shipping: {} };
22+
});
23+
924
const viewer = useState<Viewer | null>('viewer', () => null);
1025
const isPending = useState<boolean>('isPending', () => false);
1126
const orders = useState<Order[] | null>('orders', () => null);
1227
const downloads = useState<DownloadableItem[] | null>('downloads', () => null);
1328

14-
onMounted(() => {
15-
const savedCustomer = localStorage.getItem('WooNuxtCustomer');
16-
if (savedCustomer) {
17-
customer.value = JSON.parse(savedCustomer);
18-
}
19-
});
20-
2129
// Log in the user
2230
const loginUser = async (credentials: CreateAccountInput): Promise<{ success: boolean; error: any }> => {
2331
isPending.value = true;

woonuxt_base/app/composables/useCheckout.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ export function useCheckout() {
88
const { storeSettings } = useAppConfig();
99
const errorMessage = useState<string | null>('errorMessage', () => null);
1010
const orderInput = useState<any>('orderInput', () => {
11+
12+
if (!import.meta.env.SSR) {
13+
try {
14+
// Try to load saved order input from localStorage
15+
const savedOrderInput = localStorage.getItem('WooNuxtOrderInput');
16+
if (savedOrderInput) {
17+
return JSON.parse(savedOrderInput);
18+
}
19+
} catch (error) {
20+
localStorage.removeItem('WooNuxtOrderInput');
21+
console.error('Failed to load order input from localStorage:', error);
22+
}
23+
}
24+
1125
return {
1226
customerNote: '',
1327
paymentMethod: '',
@@ -16,13 +30,6 @@ export function useCheckout() {
1630
};
1731
});
1832

19-
onMounted(() => {
20-
const savedOrderInput = localStorage.getItem('WooNuxtOrderInput');
21-
if (savedOrderInput) {
22-
orderInput.value = JSON.parse(savedOrderInput);
23-
}
24-
});
25-
2633
const isProcessingOrder = useState<boolean>('isProcessingOrder', () => false);
2734

2835
// if Country or State are changed, calculate the shipping rates again
@@ -175,9 +182,9 @@ export function useCheckout() {
175182
if (error) {
176183
throw new CheckoutInlineError(error.message);
177184
}
178-
185+
179186
const { source } = await stripe.createSource(cardElement as CreateSourceData);
180-
187+
181188
if (source) orderInput.value.metaData.push({ key: '_stripe_source_id', value: source.id });
182189
if (setupIntent) orderInput.value.metaData.push({ key: '_stripe_intent_id', value: setupIntent.id });
183190

@@ -251,7 +258,7 @@ export function useCheckout() {
251258

252259
useRouter().push({ query: {} });
253260
manageCheckoutLocalStorage(false);
254-
261+
255262
if (error instanceof CheckoutInlineError) {
256263
errorMessage.value = error.message;
257264
} else {

0 commit comments

Comments
 (0)