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";
|
|
|
|
|
import { Size } from "components/ads/Button";
|
|
|
|
|
import {
|
|
|
|
|
StyledDialog,
|
|
|
|
|
StyledRadioComponent,
|
|
|
|
|
ForkButton,
|
|
|
|
|
OrganizationList,
|
|
|
|
|
ButtonWrapper,
|
|
|
|
|
SpinnerWrapper,
|
|
|
|
|
} from "./ForkModalStyles";
|
|
|
|
|
import Divider from "components/editorComponents/Divider";
|
|
|
|
|
import { getIsFetchingApplications } from "selectors/applicationSelectors";
|
2021-03-12 07:44:16 +00:00
|
|
|
import { useLocation } from "react-router";
|
|
|
|
|
import { getApplicationViewerPageURL } from "constants/routes";
|
|
|
|
|
import { getCurrentPageId } from "selectors/editorSelectors";
|
|
|
|
|
import Spinner from "components/ads/Spinner";
|
|
|
|
|
import { IconSize } from "components/ads/Icon";
|
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;
|
2021-03-10 07:08:20 +00:00
|
|
|
const [organizationId, selectOrganizationId] = useState("");
|
|
|
|
|
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);
|
|
|
|
|
const currentPageId = useSelector(getCurrentPageId);
|
2021-03-12 07:44:16 +00:00
|
|
|
const { pathname } = useLocation();
|
2021-05-04 06:04:23 +00:00
|
|
|
const showBasedOnURL =
|
|
|
|
|
pathname ===
|
|
|
|
|
`${getApplicationViewerPageURL(props.applicationId, currentPageId)}/fork`;
|
2021-03-10 07:08:20 +00:00
|
|
|
|
|
|
|
|
const forkApplication = () => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.FORK_APPLICATION_INIT,
|
|
|
|
|
payload: {
|
|
|
|
|
applicationId: props.applicationId,
|
|
|
|
|
organizationId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const organizationList = useMemo(() => {
|
|
|
|
|
const filteredUserOrgs = userOrgs.filter((item) => {
|
|
|
|
|
const permitted = isPermitted(
|
|
|
|
|
item.organization.userPermissions ?? [],
|
|
|
|
|
PERMISSION_TYPE.CREATE_APPLICATION,
|
|
|
|
|
);
|
|
|
|
|
return permitted;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (filteredUserOrgs.length) {
|
|
|
|
|
selectOrganizationId(filteredUserOrgs[0].organization.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filteredUserOrgs.map((org) => {
|
|
|
|
|
return {
|
|
|
|
|
label: org.organization.name,
|
|
|
|
|
value: org.organization.id,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}, [userOrgs]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<StyledDialog
|
2021-04-28 10:28:39 +00:00
|
|
|
canOutsideClickClose
|
2021-03-10 07:08:20 +00:00
|
|
|
className={"fork-modal"}
|
2021-05-04 06:04:23 +00:00
|
|
|
isOpen={isModalOpen || showBasedOnURL}
|
2021-04-28 10:28:39 +00:00
|
|
|
maxHeight={"540px"}
|
2021-05-04 06:04:23 +00:00
|
|
|
setModalClose={setModalClose}
|
2021-04-28 10:28:39 +00:00
|
|
|
title={"Choose where to fork the app"}
|
2021-05-04 06:04:23 +00:00
|
|
|
trigger={props.trigger}
|
2021-03-10 07:08:20 +00:00
|
|
|
>
|
|
|
|
|
<Divider />
|
2021-03-12 07:44:16 +00:00
|
|
|
{isFetchingApplications && (
|
|
|
|
|
<SpinnerWrapper>
|
|
|
|
|
<Spinner size={IconSize.XXXL} />
|
|
|
|
|
</SpinnerWrapper>
|
|
|
|
|
)}
|
|
|
|
|
{!isFetchingApplications && organizationList.length && (
|
2021-03-10 07:08:20 +00:00
|
|
|
<OrganizationList>
|
|
|
|
|
<StyledRadioComponent
|
|
|
|
|
className={"radio-group"}
|
|
|
|
|
columns={1}
|
|
|
|
|
defaultValue={organizationList[0].value}
|
|
|
|
|
onSelect={(value) => selectOrganizationId(value)}
|
2021-04-28 10:28:39 +00:00
|
|
|
options={organizationList}
|
2021-03-10 07:08:20 +00:00
|
|
|
/>
|
|
|
|
|
</OrganizationList>
|
|
|
|
|
)}
|
2021-03-12 07:44:16 +00:00
|
|
|
<ButtonWrapper>
|
|
|
|
|
<ForkButton
|
2021-05-04 06:04:23 +00:00
|
|
|
cypressSelector={"t--fork-app-to-org-button"}
|
2021-03-12 07:44:16 +00:00
|
|
|
disabled={!organizationId}
|
2021-04-28 10:28:39 +00:00
|
|
|
isLoading={forkingApplication}
|
2021-03-12 07:44:16 +00:00
|
|
|
onClick={forkApplication}
|
|
|
|
|
size={Size.large}
|
2021-04-28 10:28:39 +00:00
|
|
|
text={"FORK"}
|
2021-03-12 07:44:16 +00:00
|
|
|
/>
|
|
|
|
|
</ButtonWrapper>
|
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;
|