diff --git a/ui/src/components/DestinationStack/Actions/LoadStacks.tsx b/ui/src/components/DestinationStack/Actions/LoadStacks.tsx index 53803041c..ed8c6dae7 100644 --- a/ui/src/components/DestinationStack/Actions/LoadStacks.tsx +++ b/ui/src/components/DestinationStack/Actions/LoadStacks.tsx @@ -1,5 +1,5 @@ -import { useContext, useEffect, useState } from 'react'; -import { Icon, Select, cbModal } from '@contentstack/venus-components'; +import { useContext, useState } from 'react'; +import { AsyncSelect, Icon, cbModal } from '@contentstack/venus-components'; import { AppContext } from '../../../context/app/app.context'; import { DEFAULT_DROPDOWN, IDropDown, INewMigration } from '../../../context/app/app.interface'; @@ -43,10 +43,11 @@ const LoadStacks = (props: LoadFileFormatProps) => { created_at: '' } ]; - const [allStack, setAllStack] = useState(loadingOption); + const [allStack, setAllStack] = useState([]); const [allLocales, setAllLocales] = useState([]); const [isSaving, setIsSaving] = useState(false); + const [isLoading, setisLoading] = useState(false); const { projectId = '' }: Params = useParams(); @@ -113,31 +114,31 @@ const LoadStacks = (props: LoadFileFormatProps) => { //Handle Legacy cms selection const handleDropdownChange = (name: string) => (data: IDropDown) => { const stackChanged = selectedStack?.value !== data?.value; - const stackCleared = data?.value === ''; + const stackCleared = data?.value === '' || data?.value === null || data === null; + if (name === 'stacks') { - if (stackChanged || stackCleared) { - setSelectedStack(() => ({ ...data })); + setSelectedStack(() => ({ ...data })); - const newMigrationDataObj: INewMigration = { - ...newMigrationData, - destination_stack: { - ...newMigrationData.destination_stack, - selectedStack: stackCleared ? DEFAULT_DROPDOWN : { ...data } - } - }; + const newMigrationDataObj: INewMigration = { + ...newMigrationData, + destination_stack: { + ...newMigrationData?.destination_stack, + selectedStack: stackCleared ? DEFAULT_DROPDOWN : { ...data } + } + }; - updateNewMigrationData(newMigrationDataObj); + updateNewMigrationData(newMigrationDataObj); - //call for Step Change + if (!stackCleared) { if (props?.handleStepChange) { - props.handleStepChange(props?.currentStep, true); + props?.handleStepChange(props?.currentStep, true); } - //.handleStepChange(props?.currentStep, true); } } }; const fetchData = async () => { + setisLoading(true); const stackData = await getAllStacksInOrg( newMigrationData?.destination_stack?.selectedOrg?.value ); //org id will always be there @@ -179,12 +180,13 @@ const LoadStacks = (props: LoadFileFormatProps) => { }; updateNewMigrationData(newMigrationDataObj); + setisLoading(false); }; /**** ALL USEEffects HERE ****/ - useEffect(() => { - fetchData(); - }, []); + // useEffect(() => { + // fetchData(); + // }, []); const handleCreateNewStack = () => { cbModal({ component: (props: LoadFileFormatProps) => ( @@ -209,25 +211,88 @@ const LoadStacks = (props: LoadFileFormatProps) => { } }); }; + + const loadMoreOptions: any = async ({ + search, + skip, + limit + }: { + search: string; + skip: number; + limit: number; + }) => { + const stackData = await getAllStacksInOrg( + newMigrationData?.destination_stack?.selectedOrg?.value + ); //org id will always be there + + const stackArray = validateArray(stackData?.data?.stacks) + ? stackData?.data?.stacks?.map((stack: StackResponse) => ({ + label: stack?.name, + value: stack?.api_key, + uid: stack?.api_key, + master_locale: stack?.master_locale, + locales: stack?.locales, + created_at: stack?.created_at + })) + : []; + + stackArray.sort((a: IDropDown, b: IDropDown) => { + new Date(b?.created_at)?.getTime() - new Date(a?.created_at)?.getTime(); + }); + + setAllStack(stackArray); + + //Set selected Stack + const selectedStackData = validateArray(stackArray) + ? stackArray.find( + (stack: IDropDown) => + stack?.value === newMigrationData?.destination_stack?.selectedStack?.value + ) + : DEFAULT_DROPDOWN; + + setSelectedStack(selectedStackData); + + const newMigrationDataObj: INewMigration = { + ...newMigrationData, + destination_stack: { + ...newMigrationData?.destination_stack, + selectedStack: selectedStackData + } + }; + + updateNewMigrationData(newMigrationDataObj); + + return { options: stackArray }; + }; + const onBlurDropdown = () => { + if (!isEmptyString(selectedStack?.value)) { + if (props?.handleStepChange) { + props?.handleStepChange(props?.currentStep, true); + } + } + }; + return (
-
-