import {ComponentMeta} from "../../ComponentsMeta" import {iconWithClassName} from "../../IconWithClassName" import {CardRenderer, CardRendererContext} from "../../CardRenderers"; import {usingTestableOptions} from "../CardHelpers" import {ComponentCategory} from "../../components"; import {FieldType} from "../../fields"; import React from "react"; import {__, CouponsPlus} from "../../../globals"; import {WeeklyScheduleFields} from "../WeeklyScheduleFields"; import {getCarbonWeekdayLabel, getHour12Format, TimeSlot} from "../TimeSlotField"; import {isAllDay, renderHoursAndMinutes} from "../time"; export type WeeklyScheduleOptions = { /* a string of json objects with the following structure: * { * "days": ["tuesday", "wednesday", "friday"], 'monday'...'sunday' (can skip days) * "time": { * "type": "hours" | "range", * "value": [0, 10, 18] // 0-23 hours if type is "hours" | ['00:00', '10:00'] if type is "range" * } * } */ timeSlots: TimeSlot[], } const type = CouponsPlus?.components?.conditions?.WeeklySchedule?.type || 'WeeklySchedule' export const WeeklyScheduleMeta: ComponentMeta = { id: type, type: 'conditions', category: ComponentCategory.Time, icon: iconWithClassName(({className, context}) => { if (context === 'card') { // solid return } // outline return }), fieldsMap: [ [ { id: 'timeSlots', renderType: FieldType.Custom, fieldProps: (options: WeeklyScheduleOptions, onUpdate, {componentRuntimeData, field}) => { return { component: onUpdate( componentRuntimeData, field, [ ...options.timeSlots, { days: [], time: { type: 'range', value: ['00:00', '23:59'], __local: { // defaults to avoid data corruption when switching contexts. value_hours: [], value_range: ['00:00', '23:59'], open_days_popup_on_mount: true } } } as TimeSlot ], // @ts-ignore {action: 'update'} // cannot import the enum because of a circular dependency )} onTimeSlotChange={(index, timeSlot) => onUpdate( componentRuntimeData, field, options.timeSlots.map((ts, i) => i === index ? timeSlot : ts), // @ts-ignore {action: 'update'} // cannot import the enum because of a circular dependency )} onRemoveTimeSlot={(index) => onUpdate( componentRuntimeData, field, options.timeSlots.filter((_, i) => i !== index), // @ts-ignore {action: 'update'} // cannot import the enum because of a circular dependency )} /> } } } ] ] } export const WeeklyScheduleRenderer: CardRenderer = { type, icon: WeeklyScheduleMeta.icon!, prefix: '*', example: __('Monday - Friday, 9:00 - 17:00, Saturday 10-12PM & 14-16PM'), AboveComputedValue: usingTestableOptions(({}, {context}) => { if ((['tier-base', 'tier-subchild'] as CardRendererContext[]).includes(context)) { return false } return undefined }), ComputedValue: usingTestableOptions(({timeSlots}, {context}) => { const metaData = CouponsPlus.components.conditions[WeeklyScheduleMeta.id] return
{timeSlots.map(({days, time}) =>
{days.map(day =>

{getCarbonWeekdayLabel(day) || day}

)}
{time.type === 'hours' ? (
    {time.value.map((hour, index, hours) => (
  • {getHour12Format(parseInt(hour)).hour} {getHour12Format(parseInt(hour)).label} {index < hours.length - 1 && ','}
  • ))}
) : (

{isAllDay(time.value as [string, string]) ? __('All day') : time.value.map(renderHoursAndMinutes).join(' - ')}

)}
)}
; }), }