From ec65cd633356ab3f233feb01ceb80ef46870d4d0 Mon Sep 17 00:00:00 2001 From: Sahil Lather Date: Mon, 15 Jul 2024 15:05:48 +0530 Subject: [PATCH] fix: icon visibility and add up and down icons (#15543) * fixed the icons visibility and added up and down icon * wip: TimeUnitAddonSuffix * chore: Quick pass for rerender-optimisation --------- Co-authored-by: Keith Williams Co-authored-by: Alex van Andel Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> --- .../components/TimeTimeUnitInput.tsx | 87 ++++++++++++------- .../features/ee/workflows/lib/getOptions.ts | 7 -- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/packages/features/ee/workflows/components/TimeTimeUnitInput.tsx b/packages/features/ee/workflows/components/TimeTimeUnitInput.tsx index 1087adc73b..da4f8e6fe7 100644 --- a/packages/features/ee/workflows/components/TimeTimeUnitInput.tsx +++ b/packages/features/ee/workflows/components/TimeTimeUnitInput.tsx @@ -2,6 +2,7 @@ import { useState } from "react"; import type { UseFormReturn } from "react-hook-form"; import { useLocale } from "@calcom/lib/hooks/useLocale"; +import { TimeUnit } from "@calcom/prisma/enums"; import { Dropdown, DropdownItem, @@ -12,21 +13,50 @@ import { TextField, } from "@calcom/ui"; -import { getWorkflowTimeUnitOptions } from "../lib/getOptions"; import type { FormValues } from "../pages/workflow"; +const TIME_UNITS = [TimeUnit.DAY, TimeUnit.HOUR, TimeUnit.MINUTE] as const; + type Props = { form: UseFormReturn; disabled: boolean; }; +const TimeUnitAddonSuffix = ({ + DropdownItems, + timeUnitOptions, + form, +}: { + form: UseFormReturn; + DropdownItems: JSX.Element; + timeUnitOptions: { [x: string]: string }; +}) => { + // because isDropdownOpen already triggers a render cycle we can use getValues() + // instead of watch() function + const timeUnit = form.getValues("timeUnit"); + const [isDropdownOpen, setIsDropdownOpen] = useState(false); + return ( + + + + + {DropdownItems} + + ); +}; + export const TimeTimeUnitInput = (props: Props) => { const { form } = props; const { t } = useLocale(); - const timeUnitOptions = getWorkflowTimeUnitOptions(t); - - const [timeUnit, setTimeUnit] = useState(form.getValues("timeUnit")); - + const timeUnitOptions = TIME_UNITS.reduce((acc, option) => { + acc[option] = t(`${option.toLowerCase()}_timeUnit`); + return acc; + }, {} as { [x: string]: string }); return (
@@ -39,33 +69,26 @@ export const TimeTimeUnitInput = (props: Props) => { className="-mt-2 rounded-r-none text-sm focus:ring-0" {...form.register("time", { valueAsNumber: true })} addOnSuffix={ - - - - - - {timeUnitOptions.map((option, index) => ( - - { - setTimeUnit(option.value); - form.setValue("timeUnit", option.value); - }}> - {option.label} - - - ))} - - + + {TIME_UNITS.map((timeUnit, index) => ( + + { + form.setValue("timeUnit", timeUnit); + }}> + {timeUnitOptions[timeUnit]} + + + ))} + + } + /> } />
diff --git a/packages/features/ee/workflows/lib/getOptions.ts b/packages/features/ee/workflows/lib/getOptions.ts index cc5c92099b..f8d74275d6 100644 --- a/packages/features/ee/workflows/lib/getOptions.ts +++ b/packages/features/ee/workflows/lib/getOptions.ts @@ -4,7 +4,6 @@ import type { WorkflowActions } from "@calcom/prisma/enums"; import { isSMSOrWhatsappAction, isWhatsappAction, isEmailToAttendeeAction } from "./actionHelperFunctions"; import { - TIME_UNIT, WHATSAPP_WORKFLOW_TEMPLATES, WORKFLOW_ACTIONS, BASIC_WORKFLOW_TEMPLATES, @@ -32,12 +31,6 @@ export function getWorkflowTriggerOptions(t: TFunction) { }); } -export function getWorkflowTimeUnitOptions(t: TFunction) { - return TIME_UNIT.map((timeUnit) => { - return { label: t(`${timeUnit.toLowerCase()}_timeUnit`), value: timeUnit }; - }); -} - export function getWorkflowTemplateOptions(t: TFunction, action: WorkflowActions | undefined) { const TEMPLATES = action && isWhatsappAction(action)