1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-03 23:52:19 +01:00

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-09-01 16:08:37 +02:00
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Order } from '../../models/order';
import FormatLib from '../../lib/format';
import { FabButton } from '../base/fab-button';
2022-09-07 15:35:46 +02:00
import { User } from '../../models/user';
import { FabStateLabel } from '../base/fab-state-label';
2022-09-01 16:08:37 +02:00
interface OrderItemProps {
2022-09-07 15:35:46 +02:00
order?: Order,
currentUser?: User
2022-09-01 16:08:37 +02:00
}
/**
* List item for an order
*/
2022-09-07 15:35:46 +02:00
export const OrderItem: React.FC<OrderItemProps> = ({ order, currentUser }) => {
const { t } = useTranslation('shared');
2022-09-01 16:08:37 +02:00
/**
* Go to order page
*/
const showOrder = (order: Order) => {
2022-09-07 15:35:46 +02:00
isPrivileged()
? window.location.href = `/#!/admin/store/orders/${order.id}`
: window.location.href = `/#!/dashboard/orders/${order.id}`;
2022-09-07 15:35:46 +02:00
};
/**
* Check if the current operator has administrative rights or is a normal member
*/
const isPrivileged = (): boolean => {
return (currentUser?.role === 'admin' || currentUser?.role === 'manager');
};
/**
* Returns a className according to the status
*/
const statusColor = (status: string) => {
switch (status) {
case 'error':
return 'error';
case 'canceled':
return 'canceled';
case 'in_progress':
2022-09-07 15:35:46 +02:00
return 'pending';
default:
return 'normal';
}
2022-09-01 16:08:37 +02:00
};
return (
<div className='order-item'>
<p className="ref">{order.reference}</p>
2022-09-07 15:35:46 +02:00
<div>
<FabStateLabel status={statusColor(order.state)} background>
{t(`app.shared.store.order_item.state.${order.state}`)}
2022-09-07 15:35:46 +02:00
</FabStateLabel>
2022-09-01 16:08:37 +02:00
</div>
2022-09-07 15:35:46 +02:00
{isPrivileged() &&
<div className='client'>
<span>{t('app.shared.store.order_item.client')}</span>
<p>{order?.user?.name || ''}</p>
2022-09-07 15:35:46 +02:00
</div>
}
<p className="date">{FormatLib.date(order.created_at)}</p>
2022-09-01 16:08:37 +02:00
<div className='price'>
2022-09-07 15:35:46 +02:00
<span>{t('app.shared.store.order_item.total')}</span>
2022-09-13 13:03:56 +02:00
<p>{FormatLib.price(order?.paid_total)}</p>
2022-09-01 16:08:37 +02:00
</div>
<FabButton onClick={() => showOrder(order)} icon={<i className="fas fa-eye" />} className="is-black" />
2022-09-01 16:08:37 +02:00
</div>
);
};