feat: add update status and masters (#67) #68

Merged
237x237 merged 3 commits from feature/updateOrders into main 2025-01-26 12:15:46 +03:00
8 changed files with 95 additions and 15 deletions

View File

@ -58,7 +58,8 @@
"dry-wash.arm.order.status.pending": "Pending",
"dry-wash.arm.order.status.working": "Working",
"dry-wash.arm.order.status.canceled": "Canceled",
"dry-wash.arm.order.status.placeholder": "Select Status",
"dry-wash.arm.order.status.placeholder": "Select status",
"dry-wash.arm.order.master.placeholder": "Select master",
"dry-wash.arm.order.table.header.carNumber": "Car Number",
"dry-wash.arm.order.table.header.washingTime": "Washing Time",
"dry-wash.arm.order.table.header.orderDate": "Order Date",

View File

@ -7,6 +7,7 @@
"dry-wash.arm.order.status.working": "В работе",
"dry-wash.arm.order.status.canceled": "Отменено",
"dry-wash.arm.order.status.placeholder": "Выберите статус",
"dry-wash.arm.order.master.placeholder": "Выберите мастера",
"dry-wash.arm.order.table.header.carNumber": "Номер машины",
"dry-wash.arm.order.table.header.washingTime": "Время мойки",
"dry-wash.arm.order.table.header.orderDate": "Дата заказа",

View File

@ -72,6 +72,34 @@ const armService = () => {
return await response.json();
};
const updateOrders = async ({
id,
status,
notes,
masterId,
}: {
id: string;
status?: string;
notes?: string;
masterId?: string;
}) => {
const body = JSON.stringify({ status, notes, masterId });
const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}/${id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body,
});
if (!response.ok) {
throw new Error(`Failed to fetch update masters: ${response.status}`);
}
return await response.json();
};
const updateMaster = async ({
id,
name,
@ -98,7 +126,14 @@ const armService = () => {
return await response.json();
};
return { fetchOrders, fetchMasters, addMaster, deleteMaster, updateMaster };
return {
fetchOrders,
fetchMasters,
addMaster,
deleteMaster,
updateMaster,
updateOrders,
};
};
export { armService, ArmEndpoints };

View File

@ -1,10 +1,11 @@
import React, { useState } from 'react';
import React, { ChangeEvent, useState } from 'react';
import { Td, Tr, Link, Select } from '@chakra-ui/react';
import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
import { MasterProps } from '../MasterItem/MasterItem';
import { getTimeSlot } from '../../lib';
import { armService } from '../../api/arm';
const statuses = [
'pending' as const,
@ -28,6 +29,7 @@ export type OrderProps = {
master: MasterProps;
notes: '';
allMasters: MasterProps[];
id: string;
};
type Status = (typeof statuses)[number];
@ -50,7 +52,10 @@ const OrderItem = ({
location,
master,
allMasters,
id,
}: OrderProps) => {
const { updateOrders } = armService();
const { t } = useTranslation('~', {
keyPrefix: 'dry-wash.arm.order',
});
@ -59,15 +64,37 @@ const OrderItem = ({
const bgColor = statusColors[statusSelect];
const [masterSelect, setMaster] = useState(master?.name);
const handelChangeMasters = (e: ChangeEvent<HTMLSelectElement>) => {
const masterName = e.target.value;
const selectedMaster = allMasters.find(
(master) => master.name === masterName,
);
if (selectedMaster) {
setMaster(masterName);
updateOrders({ id, masterId: selectedMaster.id });
} else {
console.error('Master not found');
}
};
const handeChangeStatus = (e: ChangeEvent<HTMLSelectElement>) => {
const status = e.target.value;
updateOrders({ id, status });
setStatus(e.target.value as OrderProps['status']);
};
return (
<Tr>
<Td>{carNumber}</Td>
<Td>{getTimeSlot(startWashTime, endWashTime)}</Td>
<Td>{dayjs(orderDate).format('DD.MM.YYYY')}</Td>
<Td>
{dayjs(orderDate).format('DD.MM.YYYY')} <br />
{getTimeSlot(startWashTime, endWashTime)}
</Td>
<Td>
<Select
value={statusSelect}
onChange={(e) => setStatus(e.target.value as OrderProps['status'])}
onChange={handeChangeStatus}
placeholder={t(`status.placeholder`)}
bg={bgColor}
>
@ -81,8 +108,8 @@ const OrderItem = ({
<Td>
<Select
value={masterSelect}
onChange={(e) => setMaster(e.target.value as OrderProps['status'])}
placeholder={t(`status.placeholder`)}
onChange={handelChangeMasters}
placeholder={t(`master.placeholder`)}
>
{allMasters.map((item) => (
<option key={item.id} value={item.name}>

View File

@ -23,7 +23,6 @@ import { MasterProps } from '../MasterItem/MasterItem';
const TABLE_HEADERS = [
'carNumber' as const,
'washingTime' as const,
'orderDate' as const,
'status' as const,
'masters' as const,

View File

@ -53,6 +53,16 @@ router.delete('/arm/masters/:id', (req, res) => {
);
});
router.patch('/orders/:id', (req, res) => {
res
.status(/error/.test(STUBS.orders) ? 500 : 200)
.send(
/^error$/.test(STUBS.orders)
? commonError
: require(`../json/arm-orders/${STUBS.orders}.json`),
);
});
router.post('/arm/orders', (req, res) => {
res
.status(/error/.test(STUBS.orders) ? 500 : 200)
@ -77,9 +87,7 @@ router.get('/order/:orderId', ({ params }, res) => {
});
router.post('/order/create', (req, res) => {
res
.status(200)
.send({ success: true, body: { ok: true } });
res.status(200).send({ success: true, body: { ok: true } });
});
router.use('/admin', require('./admin'));

View File

@ -6,9 +6,14 @@
"name": "Иван Иванов",
"phone": "+7 900 123 45 67"
},
{
"name": "Олег Макаров",
"phone": "79001234567",
"id": "23423442"
},
{
"id": "345354234",
"name": "Иван Иванов",
"name": "Иван Галкин",
"schedule": [ {
"id": "order1",
"startWashTime": "2024-11-24T10:30:00.000Z",

View File

@ -7,10 +7,14 @@
"startWashTime": "2024-11-24T10:30:00.000Z",
"endWashTime": "2024-11-24T16:30:00.000Z",
"orderDate": "2024-11-24T08:41:46.366Z",
"status": "progress",
"status": "pending",
"phone": "79001234563",
"location": "Казань, ул. Баумана, 1",
"master": [],
"master": {
"name": "Олег Макаров",
"phone": "79001234567",
"id": "23423442"
},
"notes": ""
},
{