Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created a crud application to manage a list of packages #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"uuid": "^9.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
38 changes: 0 additions & 38 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +0,0 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
78 changes: 62 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,69 @@
import logo from './logo.svg';
import './App.css';
import logo from "./logo.svg";
import "./App.css";
import { useState } from "react";

import PackageForm from "./components/PackageForm";
import PackageList from "./components/PackageList";

function App() {
const [packages, setPackages] = useState([]);
const [editingIndex, setEditingindex] = useState(-1);

const addPackage = (newPackage) => {
setPackages([...packages, newPackage]);
};

const removePackage = (id) => {
const targetPackageIndex = packages.findIndex((p) => p.id === id);
if (targetPackageIndex < 0) {
return;
}
const updatedPackages = [
...packages.slice(0, targetPackageIndex),
...packages.slice(targetPackageIndex + 1),
];

setPackages(updatedPackages);
};

const selectPackageToEdit = (id) => {
const targetPackageIndex = packages.findIndex((p) => p.id === id);

if (targetPackageIndex < 0) {
return;
}

setEditingindex(targetPackageIndex);
};

const editPackage = (updatedPackage) => {
const updatedPackages = [
...packages.slice(0, editingIndex),
{
...packages[editingIndex],
...updatedPackage,
},
...packages.slice(editingIndex + 1),
];

setPackages(updatedPackages);
setEditingindex(-1);
};

const currentPackage = packages[editingIndex] || null;

return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<PackageForm
addPackage={addPackage}
editPackage={editPackage}
currentPackage={currentPackage}
/>
<PackageList
packages={packages}
removePackage={removePackage}
selectPackageToEdit={selectPackageToEdit}
/>
</div>
);
}
Expand Down
105 changes: 105 additions & 0 deletions src/components/PackageForm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { useState, useEffect } from "react";
import { v4 as uuidv4 } from "uuid";

const PackageForm = ({ addPackage, editPackage, currentPackage }) => {
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [category, setCategory] = useState("");
const [price, setPrice] = useState("");

useEffect(() => {
if (currentPackage) {
setName(currentPackage.name);
setDescription(currentPackage.description);
setCategory(currentPackage.category);
setPrice(currentPackage.price);
}
}, [currentPackage]);

const onInputChange = (event) => {
const { name, value } = event.target;
switch (name) {
case "name":
setName(value);
break;
case "description":
setDescription(value);
break;
case "category":
setCategory(value);
break;
case "price":
setPrice(value);
break;
default:
break;
}
};

const onFormSubmit = (event) => {
event.preventDefault();

if (currentPackage) {
const updatedPackage = {
...currentPackage,
name,
description,
category,
price,
};
editPackage(updatedPackage);
} else {
const newPackage = {
id: uuidv4(),
name,
description,
category,
price,
};

addPackage(newPackage);
}
};

return (
<form onSubmit={onFormSubmit}>
<div className="name">
<label htmlFor="name">Package Name</label>
<input name="name" type="text" value={name} onChange={onInputChange} />
</div>
<div className="descrip">
<label htmlFor="description">Package Description</label>
<input
name="description"
type="text"
value={description}
onChange={onInputChange}
/>
</div>
<div className="category">
<label htmlFor="category">Package Category</label>
<input
name="category"
type="text"
value={category}
onChange={onInputChange}
/>
</div>
<div className="price">
<label htmlFor="price">Package Price</label>
<input
name="price"
type="number"
value={price}
onChange={onInputChange}
/>
</div>
<input
type="submit"
value={currentPackage ? "Edit Package" : "Add Package"}
/>
</form>
);
};

export default PackageForm;
16 changes: 16 additions & 0 deletions src/components/PackageItem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const PackageItem = ({ item, removePackage, selectPackageToEdit }) => {
const { name, description, category, price, id } = item;

return (
<div>
<span> { name }</span>
<span> { description }</span>
<span> { category }</span>
<span> { price }</span>
<button onClick={() => removePackage(id)}>Remove</button>
<button onClick={() => selectPackageToEdit(id)}>Edit</button>
</div>
);
}

export default PackageItem;
22 changes: 22 additions & 0 deletions src/components/PackageList/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import PackageItem from "../PackageItem";

const PackageList = (props) => {
const { packages, removePackage, selectPackageToEdit } = props;

return (
<div>
<h2>Packages</h2>
<div>
{packages.length ? (
packages.map((p, i) =>
<PackageItem key={p.id} item={p} removePackage={removePackage} selectPackageToEdit={selectPackageToEdit} />
)
) : (
<span>No Packages Here!</span>
)}
</div>
</div>
);
};

export default PackageList;
15 changes: 15 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
*{
background-color: black;
color: beige;
}

button,input {
background-color: chocolate;
}

.App {
color: beige;

text-align: center;
margin-top: 100px;
}
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@