make boxes look better

This commit is contained in:
np.w
2020-11-25 19:06:47 -06:00
parent 3b71bb652d
commit 12b12cfe84
5 changed files with 84 additions and 36 deletions
+24 -22
View File
@@ -14,11 +14,12 @@ function App() {
fetchData(); fetchData();
}, []); }, []);
const [data, setData] = useState([]); const [data, setData] = useState();
const [boxName, setBoxName] = useState(''); const [boxName, setBoxName] = useState('');
const [boxDescription, setBoxDescription] = useState(''); const [boxDescription, setBoxDescription] = useState('');
const [showFormBox, setShowFormBox] = useState(false);
const fetchDataAsync = async () => { const fetchDataAsync = async () => {
try { try {
@@ -40,10 +41,10 @@ function App() {
} }
} }
const deleteBox = async (id) => { const removeBox = async (id) => {
try { try {
const response = await axios.delete(`${api.baseApiUrl}/${id}`); const response = await axios.delete(`${api.baseApiUrl}/${id}`);
setData(response.data); setData(data.filter(box => box._id !== id));
} }
catch (error) { catch (error) {
console.log(error); console.log(error);
@@ -60,6 +61,8 @@ function App() {
const handleSubmitBox = (event) => { const handleSubmitBox = (event) => {
event.preventDefault(); event.preventDefault();
setShowFormBox(false);
clearForm();
const boxData = { const boxData = {
name: boxName, name: boxName,
description: boxDescription description: boxDescription
@@ -69,29 +72,21 @@ function App() {
} }
const handleRemoveBox = (id) => { const handleRemoveBox = (id) => {
deleteBox(id); removeBox(id);
} }
const handleAddBox = () => {
setShowFormBox(true);
}
const clearForm = () => {
setBoxName();
setBoxDescription();
}
const boxHandler = { const boxHandler = {
handleSubmit: handleSubmitBox, handleSubmit: handleSubmitBox,
handleRemove: handleRemoveBox, handleRemove: handleRemoveBox,
handleName: handleBoxName, handleName: handleBoxName,
handleDescription: handleBoxDescription handleDescription: handleBoxDescription
}; };
const lstBoxes = data.map((box) => {
return (
<ul className="list-box">
<Box boxData={box} handleRemove={boxHandler.handleRemove}/>
</ul>
);
});
const formNewBox = const formNewBox =
<form onSubmit={boxHandler.handleSubmit}> <form onSubmit={boxHandler.handleSubmit}>
<legend>New Box</legend> <legend>New Box</legend>
@@ -108,8 +103,15 @@ function App() {
return ( return (
<> <>
{formNewBox} <ul className="list-box">
{lstBoxes} {data && data.map((box) => {
return (
<Box boxData={box} handleRemove={boxHandler.handleRemove} />
);
})}
</ul>
{!showFormBox && <button onClick={handleAddBox}>add box</button>}
{showFormBox && formNewBox}
</> </>
); );
+8 -5
View File
@@ -4,6 +4,7 @@ import api from './api';
import axios from 'axios'; import axios from 'axios';
const Box = ({ boxData, handleRemove }) => { const Box = ({ boxData, handleRemove }) => {
console.log(boxData);
const [data, setData] = useState(boxData); const [data, setData] = useState(boxData);
const [showFormItem, setShowFormItem] = useState(false); const [showFormItem, setShowFormItem] = useState(false);
@@ -86,16 +87,18 @@ const Box = ({ boxData, handleRemove }) => {
return ( return (
<li className="box"> <li className="box">
<p className="box-name">{data.name}</p> <h2 className="box-title">{data.name}</h2>
<p className="box-description">{data.description}</p> <div className="box-subtitle">
<h3>{data.description}</h3>
<button onClick={handleAddItem}>add item</button>
</div>
<ul className="list-box-item"> <ul className="list-box-item">
<p>{data.item.length}</p>
{data.item.map(((item) => { {data.item.map(((item) => {
return <Item data={item} handleRemove={itemHandler.handleRemove} /> return <Item data={item} handleRemove={itemHandler.handleRemove} />
}))} }))}
</ul> </ul>
<button onClick={() => handleRemove(data._id)}>delete</button> <button className="remove-box" onClick={() => handleRemove(data._id)}>remove box</button>
<button onClick={handleAddItem}>add item</button>
{showFormItem && formNewItem} {showFormItem && formNewItem}
</li> </li>
) )
+5 -9
View File
@@ -1,14 +1,10 @@
import React, {useState, setState } from 'react'; import React, { useState, setState } from 'react';
const item = ({data, handleRemove}) => { const item = ({ data, handleRemove }) => {
return (<li className="item"> return (<li className="item">
<p>{data.name}</p> <p className="item-name">{data.name}</p>
<ul> <button onClick={() => handleRemove(data._id)}>remove</button>
<li>{data.description}</li> </li>);
<li>{data.quantity}</li>
</ul>
<button onClick={() => handleRemove(data._id)}>remove</button>
</li>);
}; };
export default item; export default item;
+46
View File
@@ -4,4 +4,50 @@ form > fieldset > label {
form > fieldset { form > fieldset {
border: none; border: none;
}
li {
list-style: none;
font-size: 11px;
}
li.item {
display: grid;
grid-template-columns: 1fr 4em;
column-gap: 1em;
margin: 0.4em 0 0 0;
}
h1, h2, h3, h4 {
font-weight:normal;
margin: 0;
padding: 0;
}
button {
border: none;
font-size: 0.8em;
padding: 0.1em;
}
button.remove-box {
width: 100%;
}
button:hover {
cursor: pointer;
}
.box-subtitle {
display: grid;
grid-template-columns: 1fr 1fr;
font-size: 12px;
text-transform: lowercase;
}
ul.list-box {
display: grid;
grid-template-columns: repeat(5, 1fr);
padding: 0 0 0 1em;
margin: 0;
}
ul.list-box-item {
padding: 0 0 0 1em;
display: grid;
}
p {
margin: 0;
} }
+1
View File
@@ -60,6 +60,7 @@ app.post('/box', async (request, response) => {
app.delete('/box/:id', async (request, response) => { app.delete('/box/:id', async (request, response) => {
await Box.deleteOne({_id: request.params.id}); await Box.deleteOne({_id: request.params.id});
const updatedBoxes = await getAllBoxes(); const updatedBoxes = await getAllBoxes();
console.log(updatedBoxes);
response.send(updatedBoxes); response.send(updatedBoxes);
}); });