2021-05-04 06:04:23 +00:00
|
|
|
import React, { useState, useMemo } from "react";
|
2021-03-10 07:08:20 +00:00
|
|
|
import { useDispatch } from "react-redux";
|
|
|
|
|
import { useSelector } from "store";
|
2021-05-04 06:04:23 +00:00
|
|
|
import { getUserApplicationsOrgs } from "selectors/applicationSelectors";
|
2021-03-10 07:08:20 +00:00
|
|
|
import { isPermitted, PERMISSION_TYPE } from "./permissionHelpers";
|
|
|
|
|
import { ReduxActionTypes } from "constants/ReduxActionConstants";
|
2021-05-04 06:04:23 +00:00
|
|
|
import { AppState } from "reducers";
|
2022-03-10 14:39:05 +00:00
|
|
|
import Button, { Category, Size } from "components/ads/Button";
|
|
|
|
|
import { StyledDialog, ButtonWrapper, SpinnerWrapper } from "./ForkModalStyles";
|
2021-05-04 06:04:23 +00:00
|
|
|
import { getIsFetchingApplications } from "selectors/applicationSelectors";
|
2021-03-12 07:44:16 +00:00
|
|
|
import { useLocation } from "react-router";
|
|
|
|
|
import Spinner from "components/ads/Spinner";
|
|
|
|
|
import { IconSize } from "components/ads/Icon";
|
2021-10-18 14:03:44 +00:00
|
|
|
import { matchViewerForkPath } from "constants/routes";
|
2022-03-10 14:39:05 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
|
|
|
|
import { Dropdown } from "components/ads";
|
|
|
|
|
import {
|
|
|
|
|
CANCEL,
|
|
|
|
|
createMessage,
|
|
|
|
|
FORK,
|
|
|
|
|
FORK_APP_MODAL_EMPTY_TITLE,
|
|
|
|
|
FORK_APP_MODAL_LOADING_TITLE,
|
|
|
|
|
FORK_APP_MODAL_SUCCESS_TITLE,
|
|
|
|
|
} from "@appsmith/constants/messages";
|
2021-03-10 07:08:20 +00:00
|
|
|
|
2021-05-04 06:04:23 +00:00
|
|
|
type ForkApplicationModalProps = {
|
|
|
|
|
applicationId: string;
|
|
|
|
|
// if a trigger is passed
|
|
|
|
|
// it renders that component
|
|
|
|
|
trigger?: React.ReactNode;
|
|
|
|
|
isModalOpen?: boolean;
|
|
|
|
|
setModalClose?: (isOpen: boolean) => void;
|
|
|
|
|
};
|
2021-03-12 07:44:16 +00:00
|
|
|
|
2021-05-04 06:04:23 +00:00
|
|
|
function ForkApplicationModal(props: ForkApplicationModalProps) {
|
2021-05-13 08:35:39 +00:00
|
|
|
const { isModalOpen, setModalClose } = props;
|
2022-03-10 14:39:05 +00:00
|
|
|
const [organization, selectOrganization] = useState<{
|
|
|
|
|
label: string;
|
|
|
|
|
value: string;
|
|
|
|
|
}>({ label: "", value: "" });
|
2021-03-10 07:08:20 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const userOrgs = useSelector(getUserApplicationsOrgs);
|
2021-03-12 07:44:16 +00:00
|
|
|
const forkingApplication = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.applications.forkingApplication,
|
|
|
|
|
);
|
2021-05-04 06:04:23 +00:00
|
|
|
|
|
|
|
|
const isFetchingApplications = useSelector(getIsFetchingApplications);
|
2021-03-12 07:44:16 +00:00
|
|
|
const { pathname } = useLocation();
|
2021-10-18 14:03:44 +00:00
|
|
|
|
|
|
|
|
const showBasedOnURL = matchViewerForkPath(pathname);
|
2021-03-10 07:08:20 +00:00
|
|
|
|
|
|
|
|
const forkApplication = () => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FORK_APPLICATION_INIT,
|
|
|
|
|
payload: {
|
|
|
|
|
applicationId: props.applicationId,
|
2022-03-10 14:39:05 +00:00
|
|
|
organizationId: organization?.value,
|
2021-03-10 07:08:20 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const organizationList = useMemo(() => {
|
|
|
|
|
const filteredUserOrgs = userOrgs.filter((item) => {
|
|
|
|
|
const permitted = isPermitted(
|
|
|
|
|
item.organization.userPermissions ?? [],
|
|
|
|
|
PERMISSION_TYPE.CREATE_APPLICATION,
|
|
|
|
|
);
|
|
|
|
|
return permitted;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (filteredUserOrgs.length) {
|
2022-03-10 14:39:05 +00:00
|
|
|
selectOrganization({
|
|
|
|
|
label: filteredUserOrgs[0].organization.name,
|
|
|
|
|
value: filteredUserOrgs[0].organization.id,
|
|
|
|
|
});
|
2021-03-10 07:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filteredUserOrgs.map((org) => {
|
|
|
|
|
return {
|
|
|
|
|
label: org.organization.name,
|
|
|
|
|
value: org.organization.id,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}, [userOrgs]);
|
|
|
|
|
|
2022-03-10 14:39:05 +00:00
|
|
|
const modalHeading = isFetchingApplications
|
|
|
|
|
? createMessage(FORK_APP_MODAL_LOADING_TITLE)
|
|
|
|
|
: !organizationList.length
|
|
|
|
|
? createMessage(FORK_APP_MODAL_EMPTY_TITLE)
|
|
|
|
|
: createMessage(FORK_APP_MODAL_SUCCESS_TITLE);
|
|
|
|
|
|
2021-03-10 07:08:20 +00:00
|
|
|
return (
|
|
|
|
|
<StyledDialog
|
2021-04-28 10:28:39 +00:00
|
|
|
canOutsideClickClose
|
2021-03-10 07:08:20 +00:00
|
|
|
className={"fork-modal"}
|
2022-03-10 14:39:05 +00:00
|
|
|
headerIcon={{ name: "compasses-line", bgColor: Colors.GEYSER_LIGHT }}
|
2021-05-04 06:04:23 +00:00
|
|
|
isOpen={isModalOpen || showBasedOnURL}
|
|
|
|
|
setModalClose={setModalClose}
|
2022-03-10 14:39:05 +00:00
|
|
|
title={modalHeading}
|
2021-05-04 06:04:23 +00:00
|
|
|
trigger={props.trigger}
|
2021-03-10 07:08:20 +00:00
|
|
|
>
|
2022-03-10 14:39:05 +00:00
|
|
|
{isFetchingApplications ? (
|
2021-03-12 07:44:16 +00:00
|
|
|
<SpinnerWrapper>
|
|
|
|
|
<Spinner size={IconSize.XXXL} />
|
|
|
|
|
</SpinnerWrapper>
|
2022-03-10 14:39:05 +00:00
|
|
|
) : (
|
|
|
|
|
!!organizationList.length && (
|
|
|
|
|
<>
|
|
|
|
|
<Dropdown
|
|
|
|
|
dropdownMaxHeight={"200px"}
|
|
|
|
|
fillOptions
|
|
|
|
|
onSelect={(_, dropdownOption) =>
|
|
|
|
|
selectOrganization(dropdownOption)
|
|
|
|
|
}
|
|
|
|
|
options={organizationList}
|
|
|
|
|
selected={organization}
|
|
|
|
|
showLabelOnly
|
|
|
|
|
width={"100%"}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<ButtonWrapper>
|
|
|
|
|
<Button
|
|
|
|
|
category={Category.tertiary}
|
|
|
|
|
disabled={forkingApplication}
|
|
|
|
|
onClick={() => setModalClose && setModalClose(false)}
|
|
|
|
|
size={Size.large}
|
|
|
|
|
text={createMessage(CANCEL)}
|
|
|
|
|
type="button"
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
className="t--fork-app-to-org-button"
|
|
|
|
|
isLoading={forkingApplication}
|
|
|
|
|
onClick={forkApplication}
|
|
|
|
|
size={Size.large}
|
|
|
|
|
text={createMessage(FORK)}
|
|
|
|
|
type="button"
|
|
|
|
|
/>
|
|
|
|
|
</ButtonWrapper>
|
|
|
|
|
</>
|
|
|
|
|
)
|
2021-03-12 07:44:16 +00:00
|
|
|
)}
|
2021-03-10 07:08:20 +00:00
|
|
|
</StyledDialog>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2021-03-10 07:08:20 +00:00
|
|
|
|
|
|
|
|
export default ForkApplicationModal;
|