Commit dbc03aa1 authored by Pavel Mishakov's avatar Pavel Mishakov

finished lesson 72

parent 4820e13f
...@@ -14,7 +14,7 @@ function App() { ...@@ -14,7 +14,7 @@ function App() {
const dispatch = useDispatch() const dispatch = useDispatch()
useEffect(() => { useEffect(() => {
dispatch(getIngredients()) dispatch(getIngredients())
}, []) }, [])
return ( return (
<BrowserRouter> <BrowserRouter>
<Routes> <Routes>
......
...@@ -31,6 +31,13 @@ class ApiBurger { ...@@ -31,6 +31,13 @@ class ApiBurger {
console.log(err) console.log(err)
} }
} }
updateIngredient = async (id, ingredient) => {
try {
await burgerInstance.put(`/ingredients/${id}.json`, ingredient)
} catch (err) {
console.log(err)
}
}
} }
export const apiBurger = new ApiBurger() export const apiBurger = new ApiBurger()
\ No newline at end of file
import React from "react"; import React, { useState } from "react";
import { useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import Button from '../../components/UI/Button/Button'
import Modal from "../../components/UI/Modal/Modal";
import {updateIngredient} from '../../store/ingredients.slice'
const Ingredients = () => { const Ingredients = () => {
const ingredients = useSelector(state => state.ingredients.ingredients) const ingredients = useSelector(state => state.ingredients.ingredients)
const [showUpdateModal, setShowUpdateModal] = useState(false)
const [currentIngredient, setCurrentIngredient] = useState({})
const [currentKey, setCurrentKey] = useState('')
const dispatch = useDispatch()
const closeModal = () => {
setShowUpdateModal(false)
console.log(currentIngredient)
}
const openModal = (ingredient, key) => {
setCurrentIngredient(ingredient)
setCurrentKey(key)
setShowUpdateModal(true)
}
const inputHandler = (e) => {
setCurrentIngredient(prevState => {
if (e.target.name in prevState) {
return {...prevState, [e.target.name]: e.target.value}
}
return {...prevState, style: {...prevState.style, [e.target.name]: e.target.value}}
})
}
const createForm = (obj, arr = []) => {
Object.keys(obj).forEach((key) => {
if (typeof obj[key] === 'object') {
createForm(obj[key], arr)
} else {
arr.push(<div key={key}><input name={key} value={obj[key]} onChange={inputHandler} /></div>)
}
})
return arr
}
const submitUpdate = (e) => {
e.preventDefault()
dispatch(updateIngredient({id: currentKey, ingredient: currentIngredient}))
setShowUpdateModal(false)
}
return ( return (
<div> <div>
<Modal
show={showUpdateModal}
closed={closeModal}
>
<form onSubmit={submitUpdate}>
{createForm(currentIngredient)}
<Button
btnType={'Success'}
>Accept</Button>
</form>
</Modal>
{Object.keys(ingredients).map((key) => { {Object.keys(ingredients).map((key) => {
return <div key={key}> return <div key={key}
style={{border: '1px solid black', margin: '20px', padding: '20px'}}
>
<p>Name: {ingredients[key].name}</p> <p>Name: {ingredients[key].name}</p>
<p>Price {ingredients[key].price}</p> <p>Price {ingredients[key].price}</p>
<p>{JSON.stringify(ingredients[key].style)}</p>
<br/> <br/>
<Button
clicked={() => openModal(ingredients[key], key)}
btnType={'Success'}
>
Update
</Button>
</div> </div>
})} })}
</div> </div>
......
...@@ -17,6 +17,14 @@ export const addNewIngredient = createAsyncThunk( ...@@ -17,6 +17,14 @@ export const addNewIngredient = createAsyncThunk(
} }
) )
export const updateIngredient = createAsyncThunk(
`${namespace}/updateIngredient`,
async (data, {dispatch}) => {
await apiBurger.updateIngredient(data.id, data.ingredient)
dispatch(getIngredients())
}
)
export const ingredientsSlice = createSlice({ export const ingredientsSlice = createSlice({
name: namespace, name: namespace,
initialState: { initialState: {
...@@ -83,6 +91,15 @@ export const ingredientsSlice = createSlice({ ...@@ -83,6 +91,15 @@ export const ingredientsSlice = createSlice({
.addCase(addNewIngredient.fulfilled, (state) => { .addCase(addNewIngredient.fulfilled, (state) => {
state.loading = false state.loading = false
}) })
.addCase(updateIngredient.pending, (state) => {
state.loading = true
})
.addCase(updateIngredient.rejected, (state) => {
state.loading = false
})
.addCase(updateIngredient.fulfilled, (state) => {
state.loading = false
})
} }
}) })
......
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