JSFiddle - React, Tailwind, and code Playground
by Alexander Lashchevsky
JavaScript
const DATA = [
{ status: 'created', action: 'unconfirm', label: ['Оформить', 'Отменить подтверждение'] },
{ status: 'confirmed', action: 'confirm', label: ['Подтвердить', 'Отменить приём'] },
{ status: 'reception_courier_assigned', action: 'assign-reception-courier', label: 'Назначить курьера' },
{ status: 'courier_received', action: 'receive-courier', label: 'Принять' },
{ status: 'received', action: 'receive', label: 'Принять в СЦ' },
{ status: 'sent', action: 'send', label: 'Передать в Post' },
{ status: 'created_delivery', action: 'unconfirm-arrive', label: ['Оформить доставку', 'Отменить приём к доставке'] },
{ status: 'arrived', action: 'arrive', label: ['Принять к доставке', 'Отменить выдачу'] },
{ status: 'delivery_courier_assigned', action: 'assign-delivery-courier', label: 'Назначить курьера' },
{ status: 'courier_delivered', action: 'delivery-courier', label: 'Выдать' },
{ status: 'delivered', action: 'delivery', label: 'Выдать (и отметить в Post)' },
];
const EXTRA = [
{ from: 'reception_courier_assigned', action: 'confirm', label: 'Отменить курьера' },
{ from: 'delivery_courier_assigned', action: 'arrive', label: 'Отменить курьера' },
];
const foo = (current, action) => {
const to = DATA.find((i) => i.action === action) || null;
if (!to) {
return null;
}
const extraRule = EXTRA.filter((i) => i.from === current && i.action === action)[0] || null;
if (extraRule) {
return extraRule.label;
}
if (typeof to.label === 'object') {
return DATA.findIndex((i) => i.status === to.status) < DATA.findIndex((i) => i.status === current)
? (to.label[1] || to.label[0])
:...