import {Button, Grid, GridItem, HStack, Input, Stack, useToast} from "@chakra-ui/react" import {convertColTypeToInputType, underScoreToCapitalize,} from "../utils/TransformText"; import React, {useState} from "react"; import {addTableRecord, updateTableByPrimaryKey,} from "../utils/GenericQueries"; import {getSinglePrimaryKey} from "../utils/CustomeTableUtils"; import DropdownSelect from "../Dropdown"; import CustomStaticDropDown from "./CustomStaticDropDown"; import {PhotoUpload} from "../Babbu/PhotoUpload"; import {useMutation} from "@apollo/client"; import {Text} from "@chakra-ui/layout"; import {inputCardProps} from "./FormColDesign"; import {Col} from "../intefaces/interfaces"; interface AddEditFormColProps { title: string; tablename: string; col: Col[]; noOfCols: number; formdata: any; isUpdate: boolean; close: () => void; refetch: () => void; } function AddEditFormCol(props: AddEditFormColProps) { const toast = useToast() const {title, tablename, col, isUpdate} = 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 handleChangeSelect(value: any, field: string) { let formDataTemp = {...formdata}; formDataTemp[field] = value; setFormData(formDataTemp); } function save(e: any) { if (isSaving) { return; } delete formdata["__typename"]; setIsSaving(true) if (isUpdate) { updateByPrimaryKey({ variables: formdata }).then(res => { 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 => { 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 checkForStaticDropDown(tablecolField: any) { if (typeof tablecolField.staticDropDown !== 'undefined' && tablecolField.staticDropDown) { return true } return false } function isImage(tablecolField: any) { if (tablecolField?.type == 'image' || tablecolField?.type == 'image!') { return true } return false } let arrInputs = [ { label: "CTA Button Title", placeholder: "Enter the title of your button i.e. Redeem", spacing: "0" }, { label: "CTA Button Title 2", placeholder: "Enter the title of your button i.e. Redeem 2", spacing: "1" }, { label: "CTA Button Title 3", placeholder: "Enter the title of your button i.e. Redeem 3", spacing: "2" } ]; return ( <> {/* {*/} {/* console.log(e.target.value);*/} {/* }}*/} {/*/>*/} <> {title} {col.map((tablecolField: any) => { return ( {underScoreToCapitalize(tablecolField.name)} {tablecolField.isForeignKey && { handleChangeSelect(val, tablecolField.name) }} /> } {!tablecolField.isForeignKey && (isImage(tablecolField)) && { let formDataTemp = {...formdata}; formDataTemp[tablecolField.name] = file; setFormData(formDataTemp); }} /> } {checkForStaticDropDown(tablecolField) ? { handleChangeSelect(val, tablecolField.name) }} /> : { handleChange(e, tablecolField.name) }} value={getFieldData(tablecolField.name)} h="38px" border="1px solid " borderColor={"#ACABAB"} borderRadius={"4px"} placeholder={underScoreToCapitalize(tablecolField.name)} /> } ) }) } {/*{arrInputs.map((input, index) => {*/} {/* return (*/} {/* */} {/* */} {/* {input.label}*/} {/* */} {/* */} {/* */} {/* )*/} {/*})*/} {/*}*/} {/**/} {/* */} {/* {title}*/} {/* */} {/* {col.map((tablecolField: any) => {*/} {/* return (*/} {/* <>*/} {/* {tablecolField.isForeignKey &&*/} {/* */} {/* {*/} {/* handleChangeSelect(val, tablecolField.name)*/} {/* }}*/} {/* />*/} {/* */} {/* }*/} {/* {!tablecolField.isForeignKey && (tablecolField.type == 'image' || tablecolField.type == 'image!') &&*/} {/* */} {/* {*/} {/* let formDataTemp = {...formdata};*/} {/* formDataTemp[tablecolField.name] = file;*/} {/* setFormData(formDataTemp);*/} {/* }}*/} {/* />*/} {/* */} {/* }*/} {/* {checkForStaticDropDown(tablecolField) ?*/} {/* {*/} {/* handleChangeSelect(val, tablecolField.name)*/} {/* }}*/} {/* />*/} {/* :*/} {/* */} {/* */} {/* {underScoreToCapitalize(tablecolField.name)}*/} {/* */} {/* {*/} {/* handleChange(e, tablecolField.name)*/} {/* }}*/} {/* value={getFieldData(tablecolField.name)}*/} {/* />*/} {/* */} {/* }*/} {/* */} {/* )*/} {/* })*/} {/* }*/} {/* */} {/* {*/} {/* props.close()*/} {/* }}*/} {/* disabled={isSaving}*/} {/* fontWeight="700"*/} {/* fontSize="12px"*/} {/* color="black"*/} {/* border="1px"*/} {/* className="interFonts"*/} {/* w="76px"*/} {/* h="28px"*/} {/* bg=""*/} {/* alignSelf="flex-end"*/} {/* borderRadius="2.89px"*/} {/* _hover={{color: "#009CC4", bg: "white", border: "1px solid #009CC4"}}*/} {/* >*/} {/* Cancel*/} {/* */} {/* */} {/* Save*/} {/* */} {/* */} {/**/} ) } const PanelForm = (props: inputCardProps) => { return ( <> {props.title} {props.inputs.map((input, index) => { return ( {props.inputs[index].label} ) }) } {/*{props.inputs.map((input, index) => {*/} {/* return (*/} {/* */} {/* */} {/* */} {/* {props.inputs[index].label}*/} {/* */} {/* */} {/* */} {/* */} {/* )*/} {/*})}*/} ); }; export default AddEditFormCol