From 5bb6904ffdf7ed7115ea1e9eb0ec74ccd8e91370 Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Sat, 21 Jan 2023 22:22:21 +0530 Subject: [PATCH] feat: add LocationSelect component (#6571) * feat: add LocationSelect component Signed-off-by: Udit Takkar * fix: type error Signed-off-by: Udit Takkar * chore: type error Signed-off-by: Udit Takkar Signed-off-by: Udit Takkar --- .../components/dialog/EditLocationDialog.tsx | 47 ++------------ .../components/eventtype/EventSetupTab.tsx | 33 +++++++--- .../web/components/ui/form/LocationSelect.tsx | 64 +++++++++++++++++++ packages/app-store/utils.ts | 39 +++-------- packages/lib/getEventTypeById.ts | 4 +- 5 files changed, 103 insertions(+), 84 deletions(-) create mode 100644 apps/web/components/ui/form/LocationSelect.tsx diff --git a/apps/web/components/dialog/EditLocationDialog.tsx b/apps/web/components/dialog/EditLocationDialog.tsx index 87e946c801..aeac9ef05d 100644 --- a/apps/web/components/dialog/EditLocationDialog.tsx +++ b/apps/web/components/dialog/EditLocationDialog.tsx @@ -3,7 +3,6 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { isValidPhoneNumber } from "libphonenumber-js"; import { useEffect } from "react"; import { Controller, useForm, useWatch } from "react-hook-form"; -import { components } from "react-select"; import { z } from "zod"; import { @@ -14,32 +13,25 @@ import { LocationObject, LocationType, } from "@calcom/app-store/locations"; -import { classNames } from "@calcom/lib"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { RouterOutputs, trpc } from "@calcom/trpc/react"; -import { Button, Dialog, DialogClose, DialogContent, DialogFooter, Form, Icon, PhoneInput } from "@calcom/ui"; +import { Button, Dialog, DialogContent, DialogFooter, Form, Icon, PhoneInput } from "@calcom/ui"; import { QueryCell } from "@lib/QueryCell"; import CheckboxField from "@components/ui/form/CheckboxField"; -import Select from "@components/ui/form/Select"; +import LocationSelect, { LocationOption } from "@components/ui/form/LocationSelect"; type BookingItem = RouterOutputs["viewer"]["bookings"]["get"]["bookings"][number]; -type OptionTypeBase = { - label: string; - value: EventLocationType["type"]; - disabled?: boolean; -}; - interface ISetLocationDialog { saveLocation: (newLocationType: EventLocationType["type"], details: { [key: string]: string }) => void; - selection?: OptionTypeBase; + selection?: LocationOption; booking?: BookingItem; defaultValues?: LocationObject[]; setShowLocationModal: React.Dispatch>; isOpenDialog: boolean; - setSelectedLocation?: (param: OptionTypeBase | undefined) => void; + setSelectedLocation?: (param: LocationOption | undefined) => void; setEditingLocationType?: (param: string) => void; } @@ -211,7 +203,7 @@ export const EditLocationDialog = (props: ISetLocationDialog) => { })(); return ( - + setShowLocationModal(open)}>
@@ -296,38 +288,11 @@ export const EditLocationDialog = (props: ISetLocationDialog) => { name="locationType" control={locationFormMethods.control} render={() => ( - + ( - -
- {props.data.icon && ( - cover - )} - - {props.data.label} - -
-
- ), - }} - formatOptionLabel={(e) => ( -
- {e.icon && app-icon} - {e.label} -
- )} - formatGroupLabel={(e) => ( -

{e.label}

- )} isSearchable className="my-4 block w-full min-w-0 flex-1 rounded-sm border border-gray-300 text-sm" onChange={(val) => { diff --git a/apps/web/components/eventtype/EventSetupTab.tsx b/apps/web/components/eventtype/EventSetupTab.tsx index 82a605fb15..8892ffda17 100644 --- a/apps/web/components/eventtype/EventSetupTab.tsx +++ b/apps/web/components/eventtype/EventSetupTab.tsx @@ -17,11 +17,21 @@ import { Button, Icon, Label, Select, SettingsToggle, Skeleton, TextField } from import { slugify } from "@lib/slugify"; import { EditLocationDialog } from "@components/dialog/EditLocationDialog"; +import LocationSelect, { + SingleValueLocationOption, + LocationOption, +} from "@components/ui/form/LocationSelect"; -type OptionTypeBase = { - label: string; - value: EventLocationType["type"]; - disabled?: boolean; +const getLocationFromType = ( + type: EventLocationType["type"], + locationOptions: Pick["locationOptions"] +) => { + for (const locationOption of locationOptions) { + const option = locationOption.options.find((option) => option.value === type); + if (option) { + return option; + } + } }; export const EventSetupTab = ( @@ -32,7 +42,7 @@ export const EventSetupTab = ( const { eventType, locationOptions, team } = props; const [showLocationModal, setShowLocationModal] = useState(false); const [editingLocationType, setEditingLocationType] = useState(""); - const [selectedLocation, setSelectedLocation] = useState(undefined); + const [selectedLocation, setSelectedLocation] = useState(undefined); const [multipleDuration, setMultipleDuration] = useState(eventType.metadata.multipleDuration); const multipleDurationOptions = [5, 10, 15, 20, 25, 30, 45, 50, 60, 75, 80, 90, 120, 180].map((mins) => ({ @@ -51,7 +61,8 @@ export const EventSetupTab = ( ); const openLocationModal = (type: EventLocationType["type"]) => { - setSelectedLocation(locationOptions.find((option) => option.value === type)); + const option = getLocationFromType(type, locationOptions); + setSelectedLocation(option); setShowLocationModal(true); }; @@ -131,14 +142,14 @@ export const EventSetupTab = (
{validLocations.length === 0 && (
-