2024-03-27 15:34:55 +01:00
|
|
|
import React, { useState, useEffect } from "react";
|
2024-05-13 00:13:34 +02:00
|
|
|
import { Form, Input, InputNumber, Button, Select, DatePicker } from "antd";
|
2024-03-27 15:34:55 +01:00
|
|
|
import axios from "axios";
|
2024-05-13 19:42:49 +02:00
|
|
|
import { createItem } from '../../api/item'
|
|
|
|
|
|
|
|
|
2024-03-27 15:34:55 +01:00
|
|
|
|
|
|
|
const { TextArea } = Input;
|
|
|
|
const { Option } = Select;
|
2024-05-13 00:13:34 +02:00
|
|
|
const dateFormat = "YYYY-MM-DD";
|
2024-03-27 15:34:55 +01:00
|
|
|
|
2024-05-13 00:13:34 +02:00
|
|
|
export const FormCreateItem = ({ onClose }) => {
|
2024-03-27 15:34:55 +01:00
|
|
|
const [form] = Form.useForm();
|
|
|
|
const [rooms, setRooms] = useState([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchRooms = async () => {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(
|
|
|
|
`${import.meta.env.VITE_API_URL}/room`,
|
|
|
|
);
|
|
|
|
setRooms(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchRooms();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onFinish = async (values) => {
|
2024-05-13 19:42:49 +02:00
|
|
|
let response = await createItem(values);
|
|
|
|
if (response?.status >= 200 && response?.status < 300) {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
2024-03-27 15:34:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form form={form} onFinish={onFinish}>
|
|
|
|
<h1>Create Item</h1>
|
|
|
|
<Form.Item label="Brand" name="brand">
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Model" name="model">
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Room" name="room">
|
|
|
|
<Select placeholder="Select a room">
|
|
|
|
{rooms.map((room) => (
|
|
|
|
<Option key={room._id} value={room._id}>
|
|
|
|
{room.name}
|
|
|
|
</Option>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Price" name="price">
|
|
|
|
<InputNumber />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Purchase Date" name="purchaseDate">
|
2024-05-13 00:13:34 +02:00
|
|
|
<DatePicker format={dateFormat} />
|
2024-03-27 15:34:55 +01:00
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Description" name="description">
|
|
|
|
<TextArea rows={4} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Link" name="link">
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item>
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormCreateItem;
|