import React, {useState} from "react"; import "../../index.css"; import {Button, HStack, Stack, useToast,} from "@chakra-ui/react"; import Header from "./Header"; import {CustomInput, CustomInputDate} from "./CustomComponents"; import TextBox from "./TextBox"; import {addTableRecord, updateTableByPrimaryKey} from "./../utils/GenericQueries"; import {useMutation} from "@apollo/client"; import {getSinglePrimaryKey} from "./../utils/CustomeTableUtils"; import {PhotoUpload} from "./PhotoUpload"; import {VideoUpload} from "./VideoUpload"; let uploadIcon = "https://firebasestorage.googleapis.com/v0/b/imagestorage-7b2d2.appspot.com/o/uploadIcon.svg?alt=media&token=5c5650be-aa0e-49de-9bc7-dcf1d706d1e3"; interface inputProps { title: string; OnChange: any; } const CreateVideo = (props: any) => { const toast = useToast(); const {title, tablename, col, isUpdate, goBack} = props const [formdata, setFormData] = useState(props.formdata); const [isSaving, setIsSaving] = useState(false); const updatebyPk = updateTableByPrimaryKey(tablename, col) const [updateByPrimaryKey] = useMutation(updatebyPk); const [addRecord] = useMutation(addTableRecord(tablename, col)); function getFieldData(field: string) { return formdata[field] } function handleChange(e: any, field: string) { const {value} = e.target; let formDataTemp = {...formdata}; formDataTemp[field] = value; setFormData(formDataTemp); } function handleChangeValue(value: any, field: string) { let formDataTemp = {...formdata}; formDataTemp[field] = value; setFormData(formDataTemp); } function save(e: any) { if (isSaving) { return; } setIsSaving(true) if (isUpdate) { updateByPrimaryKey({ variables: formdata }).then(res => { toast({ title: "Success", description: 'updated.', status: "success", duration: 9000, isClosable: true }) setIsSaving(false) props.refetch(); }).catch(e => { toast({ title: "Error", description: e.message, status: "error", duration: 9000, isClosable: true }) setIsSaving(false) console.log("error ", e) }) } else { //remove primary key let singlePrimaryKeyCol = getSinglePrimaryKey(col); delete formdata[singlePrimaryKeyCol]; //ADD RECORD addRecord({ variables: { object: formdata } }).then(r => { toast({ title: "Success", description: 'saved.', status: "success", duration: 9000, isClosable: true }) setIsSaving(false) props.refetch(); }).catch(e => { setIsSaving(false) console.log("error ", e) toast({ title: "Error", description: e.message, status: "error", duration: 9000, isClosable: true }) }) } } function getTitle() { return isUpdate ? "Update Video" : "Create Video" } return ( <>
{ handleChange(e, "category") }} value={getFieldData("category")} /> { handleChange(e, "title") }} value={getFieldData("title")} /> { handleChangeValue(url, "video") }} /> { handleChangeValue(url, "poster_image") }} /> { handleChangeValue(value, "content") }} value={getFieldData("content")} /> { handleChange(e, "publish_date") }} value={getFieldData("publish_date")} /> ); }; export default CreateVideo;