diff --git a/src/App.tsx b/src/App.tsx index e9a6ca8..b1f721c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,12 +1,27 @@ -import { createContext } from 'react' +import { createContext, useEffect } from 'react' import RouterSetup from './RouterSetup' -import { useSelector } from 'react-redux' +import { useDispatch, useSelector } from 'react-redux' import type { StoreType } from './stores' import Loading from './components/Loading' +import { Apis } from './apis' +import { userAction } from './stores/slices/user.slice' export default function App() { const userStore = useSelector((store: StoreType) => store.user) + const dipatch = useDispatch() + useEffect(() => { + if(userStore.data && !userStore.loading) { + try { + Apis.cart.getCartByUserId(userStore.data.id) + .then(res => { + dipatch(userAction.initCartData(res)) + }) + }catch(err) { + + } + } + }, [userStore.data, userStore.loading]) return ( <> { diff --git a/src/RouterSetup.tsx b/src/RouterSetup.tsx index 30796bb..7363b88 100644 --- a/src/RouterSetup.tsx +++ b/src/RouterSetup.tsx @@ -8,10 +8,13 @@ import UserManagement from './pages/admin/User/UserManagement' import CategoryManagement from './pages/admin/Category/CategoryManagement' import ProductManagement from './pages/admin/Product/ProductManagement' import CreateProductForm from './pages/admin/Product/components/CreateProductForm' +import Cart from './pages/home/cart/Cart' export default function RouterSetup() { return - }> + }> + }> + }> diff --git a/src/pages/home/cart/Cart.tsx b/src/pages/home/cart/Cart.tsx new file mode 100644 index 0000000..dc37455 --- /dev/null +++ b/src/pages/home/cart/Cart.tsx @@ -0,0 +1,22 @@ +import React from 'react' +import { useSelector } from 'react-redux' +import type { StoreType } from '../../../stores' +import type { CartItem } from '../../../types/cart.type' + +export default function Cart() { + const userStore = useSelector((store: StoreType) => store.user) + + + return ( +
+ Cart Page +
    + { + userStore.cart?.map((item: CartItem) => { + return
  • {item.productId} - {item.quantity}
  • + }) + } +
+
+ ) +} diff --git a/src/stores/slices/user.slice.ts b/src/stores/slices/user.slice.ts index 892210c..cf0df4c 100644 --- a/src/stores/slices/user.slice.ts +++ b/src/stores/slices/user.slice.ts @@ -1,16 +1,19 @@ import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; import type { User } from "../../types/user.type"; import { Apis } from "../../apis"; +import type { CartItem } from "../../types/cart.type"; interface UserState { data: User | null, loading: boolean; + cart: CartItem[] | [] } const InitUserState: UserState = { data: null, - loading: false + loading: false, + cart: [] } @@ -18,7 +21,9 @@ const userSlice = createSlice({ name: "user", initialState: InitUserState, reducers: { - + initCartData: (state, action) =>{ + state.cart = action.payload + } }, extraReducers: (bd) => { bd.addCase(fetchUserData.pending, (state, action) => {