Skip to content

Instantly share code, notes, and snippets.

@YohannesTz
Created September 23, 2023 12:34
Show Gist options
  • Save YohannesTz/184485e3509c7bbf799117dcb39bf4c2 to your computer and use it in GitHub Desktop.
Save YohannesTz/184485e3509c7bbf799117dcb39bf4c2 to your computer and use it in GitHub Desktop.
frontend fix
import { React, useEffect, useState } from "react";
import CustomInput from "../components/CustomInput";
import ReactQuill from "react-quill";
import { useNavigate } from "react-router-dom";
import "react-quill/dist/quill.snow.css";
import { toast } from "react-toastify";
import * as yup from "yup";
import { useFormik } from "formik";
import { useDispatch, useSelector } from "react-redux";
import { getBrands } from "../features/brand/brandSlice";
import { getCategories } from "../features/pcategory/pcategorySlice";
import { getColors } from "../features/color/colorSlice";
import { Select } from "antd";
import Dropzone from "react-dropzone";
import { delImg, uploadImg } from "../features/upload/uploadSlice";
import { createProducts, resetState } from "../features/product/productSlice";
let schema = yup.object().shape({
title: yup.string().required("Title is Required"),
description: yup.string().required("Description is Required"),
price: yup.number().required("Price is Required"),
tags: yup.string().required("Tag is Required"),
quantity: yup.number().required("Quantity is Required"),
});
const Addproduct = () => {
const dispatch = useDispatch();
const navigate = useNavigate();
const [color, setColor] = useState([]);
const [images, setImages] = useState([]);
useEffect(() => {
dispatch(getBrands());
dispatch(getCategories());
dispatch(getColors());
}, []);
const brandState = useSelector((state) => state.brand.brands);
const catState = useSelector((state) => state.pCategory.pCategories);
const colorState = useSelector((state) => state.color.colors);
const imgState = useSelector((state) => state.upload.images);
const newProduct = useSelector((state) => state.product);
const { isSuccess, isError, isLoading, createdProduct } = newProduct;
useEffect(() => {
if (isSuccess && createdProduct) {
toast.success("Product Added Successfullly!");
}
if (isError) {
toast.error("Something Went Wrong!");
}
}, [isSuccess, isError, isLoading]);
const coloropt = [];
colorState.forEach((i) => {
coloropt.push({
label: i.title,
value: i._id,
});
});
const img = [];
imgState.forEach((i) => {
img.push({
public_id: i.public_id,
url: i.url,
});
});
useEffect(() => {
formik.values.color = color ? color : " ";
formik.values.images = img;
}, [color, img]);
const formik = useFormik({
initialValues: {
title: "",
description: "",
price: "",
tags: "",
quantity: "",
images: "",
},
validationSchema: schema,
onSubmit: (values) => {
console.log("onsubmit triggered!");
alert(JSON.stringify(values))
dispatch(createProducts(values));
formik.resetForm();
setColor(null);
setTimeout(() => {
//dispatch(resetState());
navigate("/admin/list-product");
}, 3000);
},
});
const handleColors = (e) => {
setColor(e);
console.log(color);
};
const handleQuillChange = (content) => {
formik.setFieldValue('description', content);
};
console.log(formik.errors);
return (
<div>
<h3 className="mb-4 title">Add Product</h3>
<div>
<form
onSubmit={formik.handleSubmit}
className="d-flex gap-3 flex-column"
>
<CustomInput
type="text"
label="Enter Product Title"
name="title"
onChng={formik.handleChange}
onBlur={formik.handleBlur}
val={formik.values.title}
/>
<div className="error">
{formik.touched.title && formik.errors.title}
</div>
<div className="">
<ReactQuill
theme="snow"
name="description"
onChange={handleQuillChange}
value={formik.values.description}
/>
</div>
<div className="error">
{formik.touched.description && formik.errors.description}
</div>
<CustomInput
type="number"
label="Enter Product Price"
name="price"
onChng={formik.handleChange}
onBlr={formik.handleBlur}
val={formik.values.price}
/>
<div className="error">
{formik.touched.price && formik.errors.price}
</div>
<select
name="tags"
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.tags}
className="form-control py-3 mb-3"
id=""
>
<option value="" disabled>
Select Category
</option>
<option value="featured">Featured</option>
<option value="popular">Popular</option>
<option value="special">Special</option>
</select>
<div className="error">
{formik.touched.tags && formik.errors.tags}
</div>
<CustomInput
type="number"
label="Enter Product Quantity"
name="quantity"
onChng={formik.handleChange}
onBlr={formik.handleBlur}
val={formik.values.quantity}
/>
<div className="error">
{formik.touched.quantity && formik.errors.quantity}
</div>
<div className="bg-white border-1 p-5 text-center">
<Dropzone
onDrop={(acceptedFiles) => dispatch(uploadImg(acceptedFiles))}
>
{({ getRootProps, getInputProps }) => (
<section>
<div {...getRootProps()}>
<input {...getInputProps()} />
<p>
Drag 'n' drop some files here, or click to select files
</p>
</div>
</section>
)}
</Dropzone>
</div>
<div className="showimages d-flex flex-wrap gap-3">
{imgState?.map((i, j) => {
return (
<div className=" position-relative" key={j}>
<button
type="button"
onClick={() => dispatch(delImg(i.public_id))}
className="btn-close position-absolute"
style={{ top: "10px", right: "10px" }}
></button>
<img src={i.url} alt="" width={200} height={200} />
</div>
);
})}
</div>
<button
className="btn btn-success border-0 rounded-3 my-5"
type="submit"
>
Add Product
</button>
</form>
</div>
</div>
);
};
export default Addproduct;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment