Skip to content

Commit

Permalink
carform submit
Browse files Browse the repository at this point in the history
  • Loading branch information
hi6724 committed Dec 11, 2022
1 parent e2a629d commit b2fc1a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
14 changes: 12 additions & 2 deletions cars/src/components/CarForm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useDispatch, useSelector } from "react-redux";
import { changeCost, changeName } from "../store";
import { addCar, changeCost, changeName } from "../store";
import { reset } from "../store/slices/formSlice";

function CarForm() {
const dispatch = useDispatch();
Expand All @@ -14,10 +15,16 @@ function CarForm() {
dispatch(changeCost(carCost));
};

const handleSubmit = (e) => {
e.preventDefault();
dispatch(addCar({ name, cost }));
dispatch(reset());
};

return (
<div className="car-form panel">
<h4 className="subtitle is-3">Add Car</h4>
<form>
<form onSubmit={handleSubmit}>
<div className="field-group">
<div className="field">
<label className="label">Name</label>
Expand All @@ -38,6 +45,9 @@ function CarForm() {
/>
</div>
</div>
<div className="field">
<button className="button is-link">Submit</button>
</div>
</form>
</div>
);
Expand Down
14 changes: 9 additions & 5 deletions cars/src/store/slices/formSlice.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
name: "",
cost: 0,
};

const formSlice = createSlice({
name: "form",
initialState: {
name: "",
cost: 0,
},
initialState,
reducers: {
changeName(state, action) {
state.name = action.payload;
},
changeCost(state, action) {
state.cost = action.payload;
},
reset(state, action) {
return initialState;
},
},
});

export const { changeCost, changeName } = formSlice.actions;
export const { changeCost, changeName, reset } = formSlice.actions;
export const formReducer = formSlice.reducer;

0 comments on commit b2fc1a0

Please sign in to comment.