From 58a45c96c080f7164d23b3f583436864bef6fabf Mon Sep 17 00:00:00 2001 From: guru_sainath Date: Thu, 21 Mar 2024 20:46:18 +0530 Subject: [PATCH] fix: rendering default state in the issue create update modal whenever we switch between the projects in the issue modal (#4024) --- web/components/dropdowns/state.tsx | 54 ++++++++++++++++++++---------- web/store/state.store.ts | 2 +- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/web/components/dropdowns/state.tsx b/web/components/dropdowns/state.tsx index 7ba69d1ce..7775f3946 100644 --- a/web/components/dropdowns/state.tsx +++ b/web/components/dropdowns/state.tsx @@ -4,7 +4,7 @@ import { usePopper } from "react-popper"; import { Check, ChevronDown, Search } from "lucide-react"; import { Combobox } from "@headlessui/react"; // hooks -import { StateGroupIcon } from "@plane/ui"; +import { Spinner, StateGroupIcon } from "@plane/ui"; import { cn } from "@/helpers/common.helper"; import { useApplication, useProjectState } from "@/hooks/store"; import { useDropdownKeyDown } from "@/hooks/use-dropdown-key-down"; @@ -50,6 +50,7 @@ export const StateDropdown: React.FC = observer((props) => { // states const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); + const [stateLoader, setStateLoader] = useState(false); // refs const dropdownRef = useRef(null); const inputRef = useRef(null); @@ -74,6 +75,8 @@ export const StateDropdown: React.FC = observer((props) => { } = useApplication(); const { fetchProjectStates, getProjectStates, getStateById } = useProjectState(); const statesList = getProjectStates(projectId); + const defaultStateList = statesList?.find((state) => state.default); + const stateValue = value ? value : defaultStateList?.id; const options = statesList?.map((state) => ({ value: state.id, @@ -89,11 +92,19 @@ export const StateDropdown: React.FC = observer((props) => { const filteredOptions = query === "" ? options : options?.filter((o) => o.query.toLowerCase().includes(query.toLowerCase())); - const selectedState = getStateById(value); + const selectedState = stateValue ? getStateById(stateValue) : undefined; - const onOpen = () => { - if (!statesList && workspaceSlug) fetchProjectStates(workspaceSlug, projectId); + const onOpen = async () => { + if (!statesList && workspaceSlug) { + setStateLoader(true); + await fetchProjectStates(workspaceSlug, projectId); + setStateLoader(false); + } }; + useEffect(() => { + if (projectId) onOpen(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [projectId]); const handleClose = () => { if (!isOpen) return; @@ -141,7 +152,7 @@ export const StateDropdown: React.FC = observer((props) => { ref={dropdownRef} tabIndex={tabIndex} className={cn("h-full", className)} - value={value} + value={stateValue} onChange={dropdownOnChange} disabled={disabled} onKeyDown={handleKeyDown} @@ -178,18 +189,27 @@ export const StateDropdown: React.FC = observer((props) => { showTooltip={showTooltip} variant={buttonVariant} > - {!hideIcon && ( - - )} - {BUTTON_VARIANTS_WITH_TEXT.includes(buttonVariant) && ( - {selectedState?.name ?? "State"} - )} - {dropdownArrow && ( -