Skip to content

Commit 2fb9930

Browse files
authored
Merge pull request #1481 from w3bdesign/develop
Create CART_TODO.md
2 parents ede6b3e + 638bb9a commit 2fb9930

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

CART_TODO.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Cart Refactoring TODO
2+
3+
**Goal:** Move cart fetching and initialization logic out of `src/components/Layout/Layout.component.tsx` to improve separation of concerns, adhere to DRY principles, and centralize global state initialization.
4+
5+
**Current State:**
6+
* `src/components/Layout/Layout.component.tsx` uses `useQuery(GET_CART)` to fetch cart data, formats it using `getFormattedCart`, and syncs it with the Zustand store (`useCartStore`) via `syncWithWooCommerce`. It also uses `useEffect` to `refetch` the cart.
7+
8+
**Proposed Refactoring Steps:**
9+
10+
**Step 1: Create the Cart Initializer Component**
11+
12+
1. **Create File:** Create a new file named `src/components/Cart/CartInitializer.component.tsx`.
13+
2. **Add Content:** Populate the file with the following code. This component will encapsulate the cart fetching and synchronization logic previously in `Layout.component.tsx`.
14+
15+
```typescript
16+
// src/components/Cart/CartInitializer.component.tsx
17+
import { useEffect } from 'react';
18+
import { useQuery } from '@apollo/client';
19+
20+
// State
21+
import { useCartStore } from '@/stores/cartStore';
22+
23+
// Utils
24+
import { getFormattedCart } from '@/utils/functions/functions';
25+
26+
// GraphQL
27+
import { GET_CART } from '@/utils/gql/GQL_QUERIES';
28+
29+
/**
30+
* Non-rendering component responsible for initializing the cart state
31+
* by fetching data from WooCommerce and syncing it with the Zustand store.
32+
* This should be rendered once at the application root (_app.tsx).
33+
*/
34+
const CartInitializer = () => {
35+
const { syncWithWooCommerce } = useCartStore();
36+
37+
const { data, refetch } = useQuery(GET_CART, {
38+
notifyOnNetworkStatusChange: true,
39+
onCompleted: () => {
40+
// On successful fetch, format the data and sync with the store
41+
const updatedCart = getFormattedCart(data);
42+
if (updatedCart) {
43+
syncWithWooCommerce(updatedCart);
44+
}
45+
},
46+
// Consider error handling if needed (e.g., onError callback)
47+
});
48+
49+
// Refetch cart data on component mount or potentially other dependencies
50+
// Ensure this doesn't cause unnecessary refetches if not needed.
51+
// If refetching is only needed once on load, this might be redundant
52+
// if useQuery fetches automatically on mount. Review Apollo Client cache policy.
53+
useEffect(() => {
54+
refetch();
55+
}, [refetch]);
56+
57+
// This component does not render any UI
58+
return null;
59+
};
60+
61+
export default CartInitializer;
62+
```
63+
64+
**Step 2: Integrate Cart Initializer into `_app.tsx`**
65+
66+
1. **Edit File:** Open `src/pages/_app.tsx`.
67+
2. **Import:** Add the following import statement at the top:
68+
```typescript
69+
import CartInitializer from '@/components/Cart/CartInitializer.component';
70+
```
71+
3. **Render:** Locate the main return statement within the `MyApp` component. Inside the outermost fragment or div, *before* the `<Component {...pageProps} />` line, add the `CartInitializer` component. It should look something like this (adjust based on the exact structure of your `_app.tsx`):
72+
73+
```typescript
74+
// Example structure within MyApp component in src/pages/_app.tsx
75+
return (
76+
<ApolloProvider client={apolloClient}>
77+
{/* Other providers might be here */}
78+
<CartInitializer /> {/* <-- Add this line */}
79+
<Component {...pageProps} />
80+
{/* Other global components might be here */}
81+
</ApolloProvider>
82+
);
83+
```
84+
85+
**Step 3: Clean up `Layout.component.tsx`**
86+
87+
1. **Edit File:** Open `src/components/Layout/Layout.component.tsx`.
88+
2. **Remove Imports:** Delete the following import lines:
89+
```typescript
90+
import { useEffect } from 'react'; // If not used elsewhere in the file
91+
import { useQuery } from '@apollo/client';
92+
import { useCartStore } from '@/stores/cartStore';
93+
import { getFormattedCart } from '@/utils/functions/functions';
94+
import { GET_CART } from '@/utils/gql/GQL_QUERIES';
95+
```
96+
3. **Remove Hook Call:** Delete the line:
97+
```typescript
98+
const { syncWithWooCommerce } = useCartStore();
99+
```
100+
4. **Remove `useQuery`:** Delete the entire `useQuery` hook call block:
101+
```typescript
102+
const { data, refetch } = useQuery(GET_CART, {
103+
notifyOnNetworkStatusChange: true,
104+
onCompleted: () => {
105+
const updatedCart = getFormattedCart(data);
106+
if (updatedCart) {
107+
syncWithWooCommerce(updatedCart);
108+
}
109+
},
110+
});
111+
```
112+
5. **Remove `useEffect`:** Delete the `useEffect` hook block:
113+
```typescript
114+
useEffect(() => {
115+
refetch();
116+
}, [refetch]);
117+
```
118+
6. **Verify:** Ensure `Layout.component.tsx` now only contains logic related to page structure and passing the `title` prop.
119+
120+
**Verification:**
121+
* After implementing these changes, run the application.
122+
* Check that the cart icon in the header still displays the correct item count.
123+
* Navigate to the cart page (`/handlekurv`) and verify its contents are displayed correctly.
124+
* Add/remove items from the cart and ensure the state updates correctly across the application.

0 commit comments

Comments
 (0)