import React, { useEffect, useState, useContext } from 'react';
import axios from 'axios';
import { useLocation, useNavigate } from 'react-router-dom';
import { Form, Input, InputNumber, Button, message, Spin, Typography, Select, Flex, Steps, Col, Row, Divider, ColorPicker, Upload, Descriptions, Badge, } from "antd";
import { LoadingOutlined, UploadOutlined, BarcodeOutlined, LinkOutlined } from "@ant-design/icons";
import { AuthContext } from "../../Auth/AuthContext";
import GCodeFileIcon from '../../Icons/GCodeFileIcon';
import FillamentSelect from '../common/FillamentSelect';
const { Dragger } = Upload;
const { Title, Text } = Typography;
const initialNewGCodeFileForm = {
name: "",
brand: "",
type: "",
price: 0,
color: "#FFFFFF",
diameter: "1.75",
image: null,
url: "",
barcode: "",
};
const NewGCodeFile = ({ onOk, reset }) => {
const [messageApi, contextHolder] = message.useMessage();
const [newGCodeFileLoading, setNewGCodeFileLoading] = useState(false);
const [currentStep, setCurrentStep] = useState(0);
const [nextEnabled, setNextEnabled] = useState(false);
const [newGCodeFileForm] = Form.useForm();
const [newGCodeFileFormValues, setNewGCodeFileFormValues] = useState(initialNewGCodeFileForm);
const [imageList, setImageList] = useState([]);
const [gcode, setGCode] = useState("");
const newGCodeFileFormUpdateValues = Form.useWatch([], newGCodeFileForm);
const { token } = useContext(AuthContext);
React.useEffect(() => {
newGCodeFileForm
.validateFields({
validateOnly: true,
})
.then(() => setNextEnabled(true))
.catch(() => setNextEnabled(false));
}, [newGCodeFileForm, newGCodeFileFormUpdateValues]);
const summaryItems = [
{
key: 'name',
label: 'Name',
children: newGCodeFileFormValues.name,
},
{
key: 'brand',
label: 'Brand',
children: newGCodeFileFormValues.brand,
},
{
key: 'type',
label: 'Material',
children: newGCodeFileFormValues.type,
},
{
key: 'price',
label: 'Price',
children: "£" + newGCodeFileFormValues.price + " per kg",
},
{
key: 'color',
label: 'Colour',
children: (),
},
{
key: 'url',
label: 'URL',
children: newGCodeFileFormValues.url,
},
{
key: 'barcode',
label: 'Barcode',
children: newGCodeFileFormValues.barcode,
},
];
React.useEffect(() => {
console.log("reset changed")
if (reset) {
console.log("resetting")
newGCodeFileForm.resetFields();
}
}, [reset, newGCodeFileForm])
const handleNewGCodeFile = async () => {
setNewGCodeFileLoading(true);
try {
await axios.post(`http://localhost:8080/gcodefiles`, newGCodeFileFormValues, {
headers: {
Authorization: `Bearer ${token}`,
}
});
messageApi.success('New G Code file created successfully.');
onOk();
} catch (error) {
messageApi.error('Error creating new gcode file: ' + error.message);
} finally {
setNewGCodeFileLoading(false);
}
};
const getBase64 = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
});
};
const handleImageUpload = async ({ file, fileList }) => {
console.log(fileList);
if (fileList.length == 0) {
setImageList(fileList)
newGCodeFileForm.setFieldsValue({ image: "" });
return;
}
const base64 = await getBase64(file);
setNewGCodeFileFormValues((prevValues) => ({
...prevValues,
image: base64,
}));
fileList[0].name = "GCodeFile Image"
setImageList(fileList)
newGCodeFileForm.setFieldsValue({ image: base64 });
};
const steps = [
{
title: 'Details',
key: 'details',
content: (
<>
Click or drag .gcode or .g file here.
Support for a single or bulk upload. Strictly prohibited from uploading company data or other banned files.