/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import React, { useState, useEffect } from "react"; import { Card, Button, MenuItem } from "@blueprintjs/core"; import { Select } from "@blueprintjs/select"; import Swal from "sweetalert2"; import { renderSelect } from "../js/select"; import Axios from "../js/request"; import { Navigation } from "./common/Nav"; import { ProductTypes } from "../js/types"; interface Product { productId?: string; productTypeId: string; productName: string; productPrice: string; productImage: string; } const RESET_PRODUCT = { productTypeId: "", productName: "", productPrice: "", productImage: "", }; const FilmSelect = Select.ofType(); const Create = () => { const [product, setProduct] = useState(RESET_PRODUCT); const [loadingCreate, setLoadingCreate] = useState(false); const [productType, setProductType] = useState(); const [productTypeList, setProductTypeList] = useState([]); // const [film, setFilm] = useState(TOP_100_FILMS[0]); const createProduct = () => { product.productTypeId = productType?.id?.toString() || "0"; setLoadingCreate(true); Axios.post("api/product", product) .then((res) => { console.info("res:", res); setProduct(RESET_PRODUCT); setLoadingCreate(false); Swal.fire({ icon: "success", text: "Add Successful.", confirmButtonColor: "#3085d6", }); }) .catch((err) => { console.error(err); setLoadingCreate(false); }); }; const getProductType = () => { Axios.get("api/producttypes").then((res) => { console.info("res:", res); setProductTypeList(res.data); if (res.data && res.data.length > 0) { setProductType(res.data[0]); } }); }; useEffect(() => { getProductType(); }, []); return (
Add New Product
Product Category
} onItemSelect={setProductType} >
Product Name
{ setProduct((prev) => { return { ...prev, productName: event.target.value }; }); }} />
Product Price
{ setProduct((prev) => { return { ...prev, productPrice: event.target.value }; }); }} />
Product Image
{ setProduct((prev) => { return { ...prev, productImage: event.target.value }; }); }} />
); }; export default Create;