add and delete data from db

This commit is contained in:
Ruidy Nemausat 2020-04-23 11:52:16 +02:00
parent 5e462dca58
commit 268314537a
3 changed files with 32 additions and 11 deletions

View file

@ -4,13 +4,12 @@ import {
Modal, Modal,
ModalHeader, ModalHeader,
ModalBody, ModalBody,
ModalFooter,
Form, Form,
FormGroup, FormGroup,
Label, Label,
Input, Input,
} from "reactstrap"; } from "reactstrap";
import { addItem } from "../../store/itemSlice"; import { addItemAsync } from "../../store/itemSlice";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
const useStyles = () => ({ const useStyles = () => ({
@ -28,7 +27,7 @@ export default function ItemModal() {
const toggle = () => setModal(!modal); const toggle = () => setModal(!modal);
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault(); e.preventDefault();
dispatch(addItem(name)); dispatch(addItemAsync(name));
toggle(); toggle();
}; };
const handleChange = (e) => setName(e.target.value); const handleChange = (e) => setName(e.target.value);

View file

@ -1,7 +1,11 @@
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import { Button, ListGroup, ListGroupItem } from "reactstrap"; import { Button, ListGroup, ListGroupItem } from "reactstrap";
import { useSelector, useDispatch } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
import { selectItems, deleteItem, getItemsAsync } from "../../store/itemSlice"; import {
selectItems,
deleteItemAsync,
getItemsAsync,
} from "../../store/itemSlice";
const useStyles = () => ({ const useStyles = () => ({
removeBtn: { removeBtn: {
@ -21,17 +25,17 @@ export default function List() {
// Precising dependencies so it doesn't render infinitely. // Precising dependencies so it doesn't render infinitely.
useEffect(() => { useEffect(() => {
dispatch(getItemsAsync()); dispatch(getItemsAsync());
}, [dispatch, getItemsAsync]); }, [dispatch]);
return ( return (
<ListGroup> <ListGroup>
{items.map(({ id, name }) => ( {items.map(({ _id, name }) => (
<ListGroupItem key={id}> <ListGroupItem key={_id}>
<Button <Button
style={styles.removeBtn} style={styles.removeBtn}
color="danger" color="danger"
size="small" size="small"
onClick={() => dispatch(deleteItem(id))} onClick={() => dispatch(deleteItemAsync(_id))}
> >
&times; &times;
</Button> </Button>

View file

@ -1,5 +1,4 @@
import { createSlice } from "@reduxjs/toolkit"; import { createSlice } from "@reduxjs/toolkit";
import { v1 } from "uuid";
import axios from "axios"; import axios from "axios";
import { ITEMS_URL } from "./urls"; import { ITEMS_URL } from "./urls";
@ -17,22 +16,41 @@ export const itemSlice = createSlice({
}), }),
addItem: (state, action) => ({ addItem: (state, action) => ({
...state, ...state,
items: [...state.items, { id: v1(), name: action.payload }], items: [action.payload, ...state.items],
}), }),
deleteItem: (state, action) => ({ deleteItem: (state, action) => ({
...state, ...state,
items: state.items.filter((i) => i.id !== action.payload), items: state.items.filter((i) => i._id !== action.payload),
}), }),
setItemsLoading: (state) => ({ ...state, loading: true }), setItemsLoading: (state) => ({ ...state, loading: true }),
}, },
}); });
/**
* redux-thunk which fetches Items asynchronously from db.
*/
export const getItemsAsync = () => async (dispatch) => { export const getItemsAsync = () => async (dispatch) => {
dispatch(setItemsLoading); dispatch(setItemsLoading);
const { data } = await axios.get(ITEMS_URL); const { data } = await axios.get(ITEMS_URL);
dispatch(getItems(data)); dispatch(getItems(data));
}; };
/**
* Creates a new Item in the db.
*/
export const addItemAsync = (name) => async (dispatch) => {
const { data } = await axios.post(ITEMS_URL, { name });
dispatch(addItem(data));
};
/**
* Deletes Item _id from the db.
*/
export const deleteItemAsync = (id) => async (dispatch) => {
await axios.delete(`${ITEMS_URL}/${id}`);
dispatch(deleteItem(id));
};
export const { export const {
getItems, getItems,
addItem, addItem,