Commit 69aa60aa authored by Nurasyl's avatar Nurasyl

update async redux

parent d4ec6f02
import React, { useState } from 'react'; import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import type { Ingredients, IngredientNames } from '@/interfaces/Ingredients'; import type { Ingredients, IngredientNames } from '@/interfaces/Ingredients';
import Burger from '@/components/Burger/Burger'; import Burger from '@/components/Burger/Burger';
...@@ -9,6 +9,7 @@ import { OrderSummary } from '@/components/Burger/OrderSummary'; ...@@ -9,6 +9,7 @@ import { OrderSummary } from '@/components/Burger/OrderSummary';
import { useAppSelector } from '@/store'; import { useAppSelector } from '@/store';
import { useAppDispatch } from '@/store'; import { useAppDispatch } from '@/store';
import { setIngredients } from '@/store/ingredients.slice'; import { setIngredients } from '@/store/ingredients.slice';
import { getOrder } from '@/store/orders.slice';
const BurgerBuilder = () => { const BurgerBuilder = () => {
const navigate = useNavigate(); const navigate = useNavigate();
...@@ -52,6 +53,10 @@ const BurgerBuilder = () => { ...@@ -52,6 +53,10 @@ const BurgerBuilder = () => {
navigate({pathname: "/checkout"}) navigate({pathname: "/checkout"})
}; };
useEffect(() => {
dispatch(getOrder())
}, [])
return ( return (
<> <>
<Modal show={show} onClosed={onClosedHandler}> <Modal show={show} onClosed={onClosedHandler}>
......
...@@ -22,6 +22,18 @@ export const postOrder = createAsyncThunk( ...@@ -22,6 +22,18 @@ export const postOrder = createAsyncThunk(
} }
) )
export const getOrder = createAsyncThunk(
"orders/getOrder",
async () => {
try {
const data = await axiosBurger.get("orders.json")
return data
} catch (error: any) {
throw new Error(error)
}
}
)
export const ordersSlice = createSlice({ export const ordersSlice = createSlice({
name: "orders", name: "orders",
initialState, initialState,
...@@ -50,5 +62,21 @@ export const ordersSlice = createSlice({ ...@@ -50,5 +62,21 @@ export const ordersSlice = createSlice({
state.contactData = contactData state.contactData = contactData
state.price = price state.price = price
}) })
.addCase(getOrder.pending, (state) => {
state.isLoading = true
console.log("PENDING");
})
.addCase(getOrder.rejected, (state, action) => {
state.isLoading = false
state.error = action.error as Error
console.log("REJECT");
})
.addCase(getOrder.fulfilled, (state, action) => {
console.log(action.payload, "FULFILLED");
state.isLoading = false
})
} }
}); });
\ No newline at end of file
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