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, Skeleton, ColorPicker, Upload, Descriptions, Badge, Popconfirm } from "antd";
import { LoadingOutlined, UploadOutlined, BarcodeOutlined, LinkOutlined } from "@ant-design/icons";
import { AuthContext } from "../../Auth/AuthContext";
const { Title, Text } = Typography;
const EditFillament = ({ id, onOk }) => {
const [messageApi, contextHolder] = message.useMessage();
const [dataLoading, setDataLoading] = useState(false);
const [editFillamentLoading, setEditFillamentLoading] = useState(false);
const [deleteFillamentLoading, setDeleteFillamentLoading] = useState(false);
const [currentStep, setCurrentStep] = useState(0);
const [fillament, setFillament] = useState(null);
const [imageList, setImageList] = useState([]);
const [image, setImage] = useState("");
const [editFillamentForm] = Form.useForm();
const [editFillamentFormValues, setEditFillamentFormValues] = useState({});
const { token } = useContext(AuthContext);
useEffect(() => {
// Fetch printer details when the component mounts
const fetchFillamentDetails = async () => {
if (id) {
try {
setDataLoading(true);
const response = await axios.get(`http://localhost:8080/fillaments/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
setDataLoading(false);
editFillamentForm.setFieldsValue(response.data); // Set form values with fetched data
setEditFillamentFormValues(response.data);
} catch (error) {
messageApi.error('Error fetching printer details:' + error.message);
}
}
};
fetchFillamentDetails();
}, [id, editFillamentForm]);
const handleEditFillament = async () => {
setEditFillamentLoading(true);
// Exclude the 'online' field from the submission
try {
await axios.put(`http://localhost:8080/fillaments/${id}`, editFillamentFormValues, {
headers: {
Authorization: `Bearer ${token}`,
}
});
messageApi.success('Fillament details updated successfully.');
onOk();
} catch (error) {
messageApi.error('Error updating fillament details: ' + error.message);
} finally {
setEditFillamentLoading(false);
}
};
const handleDeleteFillament = async () => {
setDeleteFillamentLoading(true);
try {
await axios.delete(`http://localhost:8080/fillaments/${id}`, "", {
headers: {
Authorization: `Bearer ${token}`,
}
});
messageApi.success('Fillament deleted successfully.');
onOk();
} catch (error) {
messageApi.error('Error updating fillament details: ' + error.message);
} finally {
setDeleteFillamentLoading(false);
}
};
const handleImageUpload = ({ file, onSuccess }) => {
const reader = new FileReader();
reader.onload = (e) => {
console.log("Setting image buffer", e.target.result);
//setImage(e.target.result);
onSuccess("ok");
};
reader.readAsDataURL(file);
};
return (
<>
{contextHolder}