From 0ddcaa5d5228b6c123130504416161f43adf89ee Mon Sep 17 00:00:00 2001 From: ilnaz <237x237@gmail.com> Date: Sat, 25 Jan 2025 17:12:02 +0300 Subject: [PATCH 1/3] feat: add update status and masters (#67) --- locales/en.json | 3 +- locales/ru.json | 1 + src/api/arm.ts | 37 +++++++++++++++++++++++- src/components/OrderItem/OrderItem.tsx | 39 ++++++++++++++++++++++---- src/components/Orders/Orders.tsx | 1 - stubs/api/index.js | 18 ++++++++---- stubs/json/arm-masters/success.json | 7 ++++- stubs/json/arm-orders/success.json | 8 ++++-- 8 files changed, 97 insertions(+), 17 deletions(-) diff --git a/locales/en.json b/locales/en.json index 22ad871..05600cb 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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", diff --git a/locales/ru.json b/locales/ru.json index 6ff912a..c389cd8 100644 --- a/locales/ru.json +++ b/locales/ru.json @@ -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": "Дата заказа", diff --git a/src/api/arm.ts b/src/api/arm.ts index d1f2573..98db777 100644 --- a/src/api/arm.ts +++ b/src/api/arm.ts @@ -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 }; diff --git a/src/components/OrderItem/OrderItem.tsx b/src/components/OrderItem/OrderItem.tsx index 5621d15..5e677a2 100644 --- a/src/components/OrderItem/OrderItem.tsx +++ b/src/components/OrderItem/OrderItem.tsx @@ -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) => { + 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) => { + const status = e.target.value; + updateOrders({ id, status }); + setStatus(e.target.value as OrderProps['status']); + }; + return ( {carNumber} - {getTimeSlot(startWashTime, endWashTime)} - {dayjs(orderDate).format('DD.MM.YYYY')} + + {dayjs(orderDate).format('DD.MM.YYYY')}
+ {getTimeSlot(startWashTime, endWashTime)} + setMaster(e.target.value as OrderProps['status'])} - placeholder={t(`status.placeholder`)} + onChange={handelChangeMasters} + placeholder={t(`master.placeholder`)} > {allMasters.map((item) => (