import {__, CouponsPlus} from "../../../globals" import {ComponentMeta} from "../../ComponentsMeta" import {iconWithClassName} from "../../IconWithClassName" import {CardRenderer, CardRendererContext} from "../../CardRenderers"; import {usingTestableOptions} from "../CardHelpers" import {ComponentCategory} from "../../components"; import {CustomFieldComputedValue} from "./CustomField"; import {FieldType} from "../../fields"; import {TabFieldProps} from "../../fields/TabField"; import {TabsProps} from "../../fields/tabs"; import {CalendarProps} from "../../fields/Calendar"; import {TimeFieldProps} from "../../fields/TimeField"; import dayjs from "dayjs"; import localizedFormat from "dayjs/plugin/localizedFormat"; dayjs.extend(localizedFormat) export type DateOptions = { 'type' : 'exact' | 'range', 'date' : { 'exact': string, // hour is ignored 'from': string, // hours are considered! 'to': string // hours are considered! }, // only used when type is 'exact', because range already supports hours 'exactHours' : { enabled: 'yes' | 'no', 'from' : string, // hours! 'to' : string // hours! } } const type = CouponsPlus?.components?.conditions?.Date?.type || 'Date' export const DateMeta: ComponentMeta = { id: type, type: 'conditions', category: ComponentCategory.Time, icon: iconWithClassName(({className, context}) => { if (context === 'card') { // solid return } // outline return }), // @ts-ignore fieldsMap: ({options: {type, exactHours}}: {options: DateOptions}) => [ [ {id: 'type', renderType: FieldType.Tab}, ], ...type === 'exact' ? ([ [ {id: 'date.exact', renderType: FieldType.Date} ], [ { id: 'exactHours.enabled', renderType: FieldType.Tab, fieldProps: { size: 'extra-small', dimUnselected: true, width: 'auto' } as TabsProps, fieldOptions: { centered: true } } ], exactHours.enabled === 'yes' && [ { id: 'exactHours.from', renderType: FieldType.Time, wrap: (fieldToRender) =>
{__('From')}
{fieldToRender}
} ], exactHours.enabled === 'yes' && [ { id: 'exactHours.to', renderType: FieldType.Time, wrap: (fieldToRender) =>
{__('To')}
{fieldToRender}
} ], ].filter(Boolean)) : [], type === 'range' && [ [ {id: 'date.from', renderType: FieldType.Date, fieldProps: {size: 'small'} as CalendarProps, title: __('Start Date')}, {id: 'date.from', renderType: FieldType.Time, fieldOptions: {isFullDate: true}, fieldProps: {narrow: true} as Partial}, ], [ {id: 'date.to', renderType: FieldType.Date, fieldProps: {size: 'small'} as CalendarProps, title: __('End Date')}, {id: 'date.to', renderType: FieldType.Time, fieldOptions: {isFullDate: true}, fieldProps: {narrow: true} as Partial}, ], ], ] } export const DateRenderer: CardRenderer = { type, icon: DateMeta.icon!, prefix: '*', example: __('May 5 (all day), From June 10 2P.M to June 12 10P.M'), AboveComputedValue: usingTestableOptions(({type}, {context}) => { if ((['tier-base', 'tier-subchild'] as CardRendererContext[]).includes(context)) { return false } if (type === 'range') { // dont show the title since we will show a title for each date return false } return undefined; }), ComputedValue: usingTestableOptions(({type, date, exactHours}, {context}) => { if ((['tier-base', 'tier-subchild'] as CardRendererContext[]).includes(context)) { return false } const parseFormat = 'YYYY-MM-DD HH:mm'; if (type === 'exact') { return `${dayjs(date.exact, parseFormat).format('ll')}${exactHours.enabled === 'yes' ? `, ${dayjs(exactHours.from, 'HH:mm').format('LT')} - ${dayjs(exactHours.to, 'HH:mm').format('LT')}` : ''}`; } else { // this is a range return
{[date.from, date.to].map((source, index) => <> {index === 0? __('Start Date') : __('End Date')}

{dayjs(source, parseFormat).format('lll')}

)}
} }), }