Skip to content

Commit

Permalink
Merge pull request #807 from OpenSourceBrain/feature/762
Browse files Browse the repository at this point in the history
Feature/762 - fix - Creating new workspace from repository
  • Loading branch information
filippomc authored Nov 14, 2023
2 parents 8456aba + 1ba0e16 commit ec8e585
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default ({
dialogOpen: boolean;
handleClose: (open: boolean) => any;
}) => {
console.log(dialogOpen);

return (
<OSBDialog
closeAction={() => handleClose(false)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import DialogTitle from "@mui/material/DialogTitle";
import DialogContent from "@mui/material/DialogContent";
import DialogActions from "@mui/material/DialogActions";
import Dialog from "@mui/material/Dialog";

import { Button, Link } from "@mui/material";


const WorkspaceConfirmDialog = ({
createdWorkspaceConfirmationContent,
setChecked,
workspaceLink,
handleCloseConfirmationDialog,
}) => {
const { title, content, isSuccess, } = createdWorkspaceConfirmationContent;

return (
<Dialog
open={true}
onClose={() => handleCloseConfirmationDialog()}
>
<DialogTitle>{title}</DialogTitle>
<DialogContent>{content}</DialogContent>
<DialogActions>
<Button
color="primary"
onClick={() => {
setChecked([]);
handleCloseConfirmationDialog();
}}
>
Close
</Button>
<Button color="primary" variant="contained" disabled={!isSuccess || !workspaceLink.length}>
<Link
href={workspaceLink}
target="_blank"
color="secondary"
underline="none"
>
Go to workspace
</Link>
</Button>
</DialogActions>
</Dialog>
)
}

export default WorkspaceConfirmDialog;
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
badgeBgLight,
lightWhite,
} from "../../theme";
import ConfirmationDialog from "../dialogs/WorkspaceConfirmDialog";

export interface WorkspaceTemplate {
title: string;
Expand Down Expand Up @@ -145,19 +146,16 @@ export const NewWorkspaceItem = (props: ItemProps) => {
setNewWorkspaceOpen(true);
};

const onWorkspaceCreated = (refresh = false, ws: Workspace) => {
document.getElementById("your-all-workspaces-tab")?.click();
setNewWorkspaceOpen(false);
props.closeMainDialog(false);

const onWorkspaceCreated = (refresh = false) => {
if (refresh) {
refreshWorkspaces();
}
};

const defaultWorkspace: Workspace = WORKSPACE_TEMPLATES[template];

return (
<>
<>
<Button sx={style} onClick={handleClick} className={className}>
<Box textAlign="center">
{icon}
Expand Down Expand Up @@ -186,9 +184,12 @@ export const NewWorkspaceItem = (props: ItemProps) => {
<WorkspaceFromRepository
close={() => setNewWorkspaceOpen(false)}
workspaceCreatedCallback={onWorkspaceCreated}
closeMainDialog={(isClosed) => props.closeMainDialog(isClosed)}
/>
))}
</>);
))
}
</>
);
};

export default NewWorkspaceItem;
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ export default (props: WorkspaceEditProps) => {
(e) => console.error("Error uploading thumbnail", e)
);
} else {
console.log("else");
setLoading(true);
props.onLoadWorkspace(true, returnedWorkspace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import OSBDialog from "../common/OSBDialog";
import { fontColor, bgInputs, radius, bgLight, bgDarker } from "../../theme";
import { RootState } from "../../store/rootReducer";
import RepositoriesWorkspacesSearchField from "../common/RepositoriesWorkspacesSearchField";
import WorkspaceConfirmDialog from "../dialogs/WorkspaceConfirmDialog";

export interface WorkspaceTemplate {
title: string;
Expand Down Expand Up @@ -125,9 +126,11 @@ const useStyles = makeStyles((theme) => ({
export const WorkspaceFromRepository = ({
close,
workspaceCreatedCallback,
closeMainDialog,
}: {
close: () => any;
workspaceCreatedCallback: (refresh?: boolean, workspace?: Workspace) => void;
workspaceCreatedCallback: (refresh?: boolean, workspace?: Workspace) => void;
closeMainDialog?: (isClosed: boolean) => void;
}) => {
const [checked, setChecked] = React.useState<RepositoryResourceNode[]>([]);

Expand All @@ -141,9 +144,18 @@ export const WorkspaceFromRepository = ({
SELECT_FILES,
EDIT_WORKSPACE,
ERROR_NO_FILES,
SHOW_SUCCESS_OR_ERROR_MESSAGE,
}
const [stage, setStage] = React.useState(Stage.SELECT_REPO);

const [createdWorkspaceConfirmationContent, setCreatedWorkspaceConfirmationContent] = React.useState({
title: "",
content: "",
isSuccess: false,
showConfirmationDialog: true,
});
const [workspaceLink, setWorkspaceLink] = React.useState("");

const handleBackAction = () => {
if (stage > Stage.SELECT_REPO) {
setStage(stage - 1);
Expand All @@ -152,7 +164,7 @@ export const WorkspaceFromRepository = ({
};

const handleContinue = () => {
if (stage < Stage.ERROR_NO_FILES) setStage(stage + 1);
if (stage < Stage.SHOW_SUCCESS_OR_ERROR_MESSAGE) setStage(stage + 1);
};

const defaultWorkspace: Workspace = {
Expand All @@ -169,16 +181,30 @@ export const WorkspaceFromRepository = ({
};

const onWorkspaceCreated = (refresh = false, ws: Workspace) => {
document.getElementById("your-all-workspaces-tab").click(); // TODO replace with redux action
if (checked.length > 0) {
WorkspaceService.importResourcesToWorkspace(
ws.id,
checked.map((c) => c.resource)
).then(() => {
setChecked([]);
workspaceCreatedCallback(refresh, ws);
setWorkspaceLink(`/workspace/${ws.id}`);
setCreatedWorkspaceConfirmationContent((prevContent) => ({
...prevContent,
title: "Success!",
content: "New workspace created.",
isSuccess: true,
}));
setStage(Stage.SHOW_SUCCESS_OR_ERROR_MESSAGE);
}).catch((err) => {
setCreatedWorkspaceConfirmationContent((prevContent) => ({
...prevContent,
title: "Error!",
content: "Unexpected error submitting the workspace. Please try again later.",
isSuccess: false,
}));
setStage(Stage.SHOW_SUCCESS_OR_ERROR_MESSAGE);
});
} else {
setStage(Stage.ERROR_NO_FILES)
workspaceCreatedCallback(refresh, ws);
}
};
Expand Down Expand Up @@ -351,6 +377,18 @@ export const WorkspaceFromRepository = ({
close();
};

const handleCloseConfirmationDialog = () => {
if (createdWorkspaceConfirmationContent.isSuccess) { workspaceCreatedCallback(true, null) }
setCreatedWorkspaceConfirmationContent({
title: "",
content: "",
isSuccess: false,
showConfirmationDialog: false,
});
Stage ? setStage(Stage.SHOW_SUCCESS_OR_ERROR_MESSAGE + 1) : null
closeMainDialog(false);
}

const returnDialoged = (children: any) => {
return (
<OSBDialog
Expand Down Expand Up @@ -422,6 +460,17 @@ export const WorkspaceFromRepository = ({
</Grid>
</Grid>
);
case Stage.SHOW_SUCCESS_OR_ERROR_MESSAGE:

return (
<WorkspaceConfirmDialog
setChecked={setChecked}
createdWorkspaceConfirmationContent={createdWorkspaceConfirmationContent}
workspaceLink={workspaceLink}
handleCloseConfirmationDialog={handleCloseConfirmationDialog}
/>
)

default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const WorkspacesList = (props: WorkspacesProps) => {
}
>
<StyledContextChip
label={row.defaultApplication.name}
label={row.defaultApplication?.name}
/>
</Tooltip>
</TableCell>
Expand Down
Loading

0 comments on commit ec8e585

Please sign in to comment.