From f0a36f8194f462255f6e67a30fe6a474036bd41b Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Mon, 23 May 2022 23:29:09 +0530 Subject: [PATCH] Fix infinite renders on event-type edit page (#2820) --- .github/PULL_REQUEST_TEMPLATE.md | 2 + apps/web/components/ui/form/CheckedSelect.tsx | 62 +++++-------------- apps/web/pages/event-types/[type].tsx | 23 ++++--- 3 files changed, 33 insertions(+), 54 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index e530d9ace3..7a790cf629 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -8,6 +8,8 @@ Fixes # (issue) Loom Video: https://www.loom.com/ --> +**Environment**: Staging(main branch) / Production + ## Type of change diff --git a/apps/web/components/ui/form/CheckedSelect.tsx b/apps/web/components/ui/form/CheckedSelect.tsx index 2c61aaaa7c..17a568674d 100644 --- a/apps/web/components/ui/form/CheckedSelect.tsx +++ b/apps/web/components/ui/form/CheckedSelect.tsx @@ -1,57 +1,31 @@ import { CheckIcon, XIcon } from "@heroicons/react/outline"; -import React, { useEffect, useState } from "react"; -import { MultiValue } from "react-select"; +import React from "react"; +import { Props } from "react-select"; -import { useLocale } from "@lib/hooks/useLocale"; +import { useLocale } from "@calcom/lib/hooks/useLocale"; import Avatar from "@components/ui/Avatar"; import Select from "@components/ui/form/Select"; -type CheckedSelectValue = { +type CheckedSelectOption = { avatar: string; label: string; value: string; disabled?: boolean; -}[]; - -export type CheckedSelectProps = { - defaultValue?: CheckedSelectValue; - placeholder?: string; - name?: string; - options: CheckedSelectValue; - onChange: (options: CheckedSelectValue) => void; - disabled: boolean; }; -export const CheckedSelect = (props: CheckedSelectProps) => { - const { onChange } = props; - const [selectedOptions, setSelectedOptions] = useState(props.defaultValue || []); +export const CheckedSelect = ({ + options = [], + value = [], + ...props +}: Omit, "value" | "onChange"> & { + value?: readonly CheckedSelectOption[]; + onChange: (value: readonly CheckedSelectOption[]) => void; +}) => { const { t } = useLocale(); - - useEffect(() => { - onChange(selectedOptions); - }, [onChange, selectedOptions]); - - const options = props.options.map((option) => ({ - ...option, - disabled: !!selectedOptions.find((selectedOption) => selectedOption.value === option.value), - })); - - const removeOption = (value: string) => - setSelectedOptions(selectedOptions.filter((option) => option.value !== value)); - - const changeHandler = (selections: MultiValue) => - selections.forEach((selected) => { - if (selectedOptions.find((option) => option.value === selected.value)) { - removeOption(selected.value); - return; - } - setSelectedOptions(selectedOptions.concat(selected)); - }); - return ( <> - +