Commit 565f3c48 authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

[AFP-12] Fix merge conflict with dev

parents 2628a19b 8279f394
This diff is collapsed.
......@@ -25,7 +25,7 @@ export default function Main() {
<AuthRoute exact path="/products/new">
<ProductForm method="POST" />
</AuthRoute>
<AuthRoute exact path="/products/edit/:sku">
<AuthRoute exact path="/products/edit/:productId">
<ProductForm method="PUT" />
</AuthRoute>
<AuthRoute
......@@ -39,7 +39,9 @@ export default function Main() {
<Redirect to="/products" />
</AuthRoute>
<AuthRoute path="/searchResults" component={SearchResults}/>
<AuthRoute>404 PAGE</AuthRoute>
<AuthRoute>
<h1>404 Page Not Found</h1>
</AuthRoute>
</Switch>
</div>
);
......
import React, { useEffect, useState } from "react";
import { useHistory, useParams } from "react-router";
import Config from "../config";
import emptyProduct from "../emptProduct";
import Config from '../config';
const emptyForm = {
sku: "",
upc: "",
productName: "",
brand: "",
category: "",
price: 0.0,
availableStock: 0,
productDescription: "",
productImageUrl: "",
productImage: "",
...emptyProduct,
imageFile: "",
};
ProductForm.defaultProps = {
......@@ -21,27 +14,33 @@ ProductForm.defaultProps = {
export default function ProductForm(props) {
const { method } = props;
const [form, setForm] = useState(emptyForm);
const [form, setForm] = useState({ ...emptyForm });
const [imageMode, setImageMode] = useState("url");
const [errors, setErrors] = useState([]);
const history = useHistory();
const { sku } = useParams();
const { productId } = useParams("productId");
useEffect(() => {
fetch(`${Config.inventoryUrl}/${sku}`).then((res) => {
fetch(`http://localhost:8080/api/products/${productId}/`).then((res) => {
if (res.ok) {
res
.json()
.then((data) =>
setForm({ ...data, availableStock: data.stock, productImage: "" })
);
console.log(res);
res.json().then((data) => {
Object.keys(data).forEach((key) => {
if (data[key] === null) {
data[key] = "";
}
});
console.log(data);
setForm({ ...data, availableStock: data.stock });
});
}
});
}, [sku]);
}, [productId]);
const validate = () => {
setErrors([]);
const errs = [];
console.log(form)
if (form.sku.length < 3) {
errs.push("SKU must be at least 3 characters");
}
......@@ -70,12 +69,17 @@ export default function ProductForm(props) {
const onSubmit = (e) => {
e.preventDefault();
validate();
if (imageMode === "url") {
delete form.imageFile;
} else {
delete form.prodImgUrl;
}
if (errors.length === 0) {
// console.log(form);
const url =
method === "PUT"
? `${Config.inventoryUrl}/${sku}`
? `${Config.inventoryUrl}/${productId}`
: `${Config.inventoryUrl}`;
fetch(url, {
method,
......@@ -124,6 +128,7 @@ export default function ProductForm(props) {
type="text"
className="form-control"
id="productSku"
disabled={method === "PUT" ? true : false}
value={form.sku}
onChange={(e) => setForm({ ...form, sku: e.target.value })}
/>
......@@ -191,12 +196,12 @@ export default function ProductForm(props) {
</div>
</div>
<div className="col-6">
<label htmlFor="productDescription" className="form-label">
<label htmlFor="productDesc" className="form-label">
Description
</label>
<textarea
name="productDescription"
id="productDescription"
name="productDesciption"
id="productDesc"
cols="40"
rows="7"
className="form-control"
......@@ -229,51 +234,70 @@ export default function ProductForm(props) {
<input
disabled={method === "PUT" ? true : false}
required
name="stock"
name="availableStock"
type="number"
className="form-control"
id="stock"
value={form.availableStock}
onChange={(e) =>
setForm({ ...form, availableStock: e.target.value })
}
onChange={(e) => setForm({ ...form, availableStock: e.target.value })}
/>
</div>
</div>
<div className="row mt-3">
<label htmlFor="productImage" className="form-label">
Product Image
</label>
<input
id="productImageUrl"
name="productImageUrl"
className="form-control form-control-lg"
type="url"
placeholder="Enter image URL here or upload image below..."
value={form.productImageUrl}
onChange={(e) =>
setForm({
...form,
productImageUrl: e.target.value,
productImage: "",
})
}
></input>
<input
id="productImage"
name="productImage"
className="form-control form-control-lg"
type="file"
value={form.productImage}
onChange={(e) =>
setForm({
...form,
productImage: e.target.value,
productImageUrl: "",
})
}
></input>
</div>
{imageMode === "upload" ? (
<div className="row mt-3">
<div>
<label htmlFor="productImage" className="form-label">
Image File
</label>
<input
id="productImage"
name="imageFile"
className="form-control form-control-lg"
type="file"
value={form.imageFile}
onChange={(e) =>
setForm({ ...form, imageFile: e.target.value })
}
></input>
<button
type="button"
className="btn btn-outline-primary mt-3"
onClick={() => {
setForm({ ...form, imageFile: "" });
setImageMode("url");
}}
>
Specify Image URL
</button>
</div>
</div>
) : null}
{imageMode === "url" ? (
<div className="row mt-3">
<div>
<label htmlFor="prodImgUrl" className="form-label">
Image URL
</label>
<input
name="productImageUrl"
type="text"
className="form-control"
id="prodImgUrl"
value={form.productImageUrl}
onChange={(e) =>
setForm({ ...form, productImageUrl: e.target.value })
}
/>
<button
type="button"
className="btn btn-outline-primary mt-3"
onClick={() => setImageMode("upload")}
>
Upload Image
</button>
</div>
</div>
) : null}
<div className="row mt-3">
<div className="col-12">
......
......@@ -3,7 +3,8 @@ import Product from "./Product.jsx";
import { Col, Container, Row } from "react-bootstrap";
import "./../styles/ProductGrid.css";
import Config from "../config.js";
import { set } from "react-hook-form";
import { Link } from "react-router-dom";
export default function ProductGrid({ productData }) {
const [products, setProducts] = useState([]);
......@@ -18,11 +19,16 @@ export default function ProductGrid({ productData }) {
useEffect(() => fetchProducts(), []);
return (
<div>
<h1 id="title" className="text-center">
Inventory
</h1>
<Container id="prod-grid" className="mt-3">
<div class="container flex-column d-flex justify-content-center">
<div className="container mt-3 d-flex justify-content-between align-items-center">
<h1 id="title" className="text-center">
Inventory
</h1>
<Link type="link" className="btn btn-success" to="/products/new">
+ New Product
</Link>
</div>
<Container id="prod-grid" className="mt-3 mx-auto">
<Row xs={1} sm={2} md={3} lg={4}>
{products.map((product) => {
return (
......
const emptyProduct = {
sku: "",
upc: "",
productName: "",
brand: "",
category: "",
price: 0.0,
availableStock: 0,
productDesciption: "",
productImageUrl: "",
};
export default emptyProduct;
This source diff could not be displayed because it is too large. You can view the blob instead.
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