feat: add a component for changing days (#48)
All checks were successful
it-academy/dry-wash-pl/pipeline/head This commit looks good
it-academy/dry-wash-pl/pipeline/pr-main This commit looks good

This commit is contained in:
Ильназ 2024-12-14 22:27:07 +03:00
parent e2c65fd39c
commit 8cc8391a09
7 changed files with 62 additions and 7 deletions

View File

@ -77,5 +77,6 @@
"dry-wash.notFound.button.back": "Back to Home", "dry-wash.notFound.button.back": "Back to Home",
"dry-wash.errorBoundary.title": "Something went wrong", "dry-wash.errorBoundary.title": "Something went wrong",
"dry-wash.errorBoundary.description": "We are already working on fixing the issue", "dry-wash.errorBoundary.description": "We are already working on fixing the issue",
"dry-wash.errorBoundary.button.reload": "Reload Page" "dry-wash.errorBoundary.button.reload": "Reload Page",
"dry-wash.washTime.timeSlot": "{{start}} - {{end}}"
} }

View File

@ -8,8 +8,14 @@ enum ArmEndpoints {
const armService = () => { const armService = () => {
const endpoint = getConfigValue('dry-wash.api'); const endpoint = getConfigValue('dry-wash.api');
const fetchOrders = async () => { const fetchOrders = async ({ date }: { date: Date }) => {
const response = await fetch(`${endpoint}${ArmEndpoints.ORDERS}`); const response = await fetch(`${endpoint}${ArmEndpoints.ORDERS}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ date }),
});
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch orders: ${response.status}`); throw new Error(`Failed to fetch orders: ${response.status}`);

View File

@ -0,0 +1,31 @@
import React from 'react';
import { Box, Button, Text } from '@chakra-ui/react';
import { ArrowBackIcon, ArrowForwardIcon } from '@chakra-ui/icons';
interface DateNavigatorProps {
currentDate: Date;
onPreviousDate: () => void;
onNextDate: () => void;
}
const DateNavigator = ({
currentDate,
onPreviousDate,
onNextDate,
}: DateNavigatorProps) => {
return (
<Box display='flex' alignItems='center' justifyContent='flex-start' mb='5'>
<Button onClick={onPreviousDate}>
<ArrowBackIcon />
</Button>
<Text mx='4' fontSize='lg' fontWeight='bold'>
{currentDate.toLocaleDateString()}
</Text>
<Button onClick={onNextDate}>
<ArrowForwardIcon />
</Button>
</Box>
);
};
export default DateNavigator;

View File

@ -0,0 +1 @@
export { default } from './DateNavigator';

View File

@ -13,10 +13,12 @@ import {
useToast, useToast,
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
import OrderItem from '../OrderItem'; import OrderItem from '../OrderItem';
import { OrderProps } from '../OrderItem/OrderItem'; import { OrderProps } from '../OrderItem/OrderItem';
import { armService } from '../../api/arm'; import { armService } from '../../api/arm';
import DateNavigator from '../DateNavigator';
const TABLE_HEADERS = [ const TABLE_HEADERS = [
'carNumber' as const, 'carNumber' as const,
@ -39,13 +41,14 @@ const Orders = () => {
const [orders, setOrders] = useState<OrderProps[]>([]); const [orders, setOrders] = useState<OrderProps[]>([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [currentDate, setCurrentDate] = useState(new Date());
useEffect(() => { useEffect(() => {
const loadOrders = async () => { const loadOrders = async () => {
setLoading(true); setLoading(true);
try { try {
const data = await fetchOrders(); const data = await fetchOrders({ date: currentDate });
setOrders(data.body); setOrders(data.body);
} catch (err) { } catch (err) {
setError(err.message); setError(err.message);
@ -62,13 +65,26 @@ const Orders = () => {
}; };
loadOrders(); loadOrders();
}, [toast, t]); }, [toast, t, currentDate]);
return ( return (
<Box p='8'> <Box p='8'>
<Heading size='lg' mb='5'> <Heading size='lg' mb='5'>
{t('title')} {t('title')}
</Heading> </Heading>
<DateNavigator
currentDate={currentDate}
onPreviousDate={() =>
setCurrentDate((prevDate) =>
dayjs(prevDate).subtract(1, 'day').toDate(),
)
}
onNextDate={() =>
setCurrentDate((prevDate) => dayjs(prevDate).add(1, 'day').toDate())
}
/>
<Table variant='simple' colorScheme='blackAlpha'> <Table variant='simple' colorScheme='blackAlpha'>
<Thead> <Thead>
<Tr> <Tr>

View File

@ -2,9 +2,9 @@ import React from 'react';
import { Text, Button, Center, VStack, Heading } from '@chakra-ui/react'; import { Text, Button, Center, VStack, Heading } from '@chakra-ui/react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { Player } from '@lottiefiles/react-lottie-player'; import { Player } from '@lottiefiles/react-lottie-player';
import { useTranslation } from 'react-i18next';
import animate from '../../assets/animation/notFound.json'; import animate from '../../assets/animation/notFound.json';
import { useTranslation } from 'react-i18next';
const NotFound = () => { const NotFound = () => {
const { t } = useTranslation('~', { const { t } = useTranslation('~', {

View File

@ -23,7 +23,7 @@ router.get('/arm/masters', (req, res) => {
); );
}); });
router.get('/arm/orders', (req, res) => { router.post('/arm/orders', (req, res) => {
res res
.status(/error/.test(STUBS.orders) ? 500 : 200) .status(/error/.test(STUBS.orders) ? 500 : 200)
.send( .send(