Skip to content

Commit

Permalink
Merge pull request #86 from ssi-dk/feat/send-to-new-workspace
Browse files Browse the repository at this point in the history
feat: send to new workspace
  • Loading branch information
allanhvam authored Jul 4, 2024
2 parents ae37e0f + 62f6676 commit 062fb6e
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/src/app/workspaces/send-to-microreact-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const SendToMicroreactModal = (props: Props) => {
localStorage.getItem(localStorageKey) ?? ""
);
const [isSending, setIsSending] = useState<boolean>(false);
const [treeMethod, setTreeMethod] = useState<string>();
const [treeMethod, setTreeMethod] = useState<string>(TreeMethod.single);

const workspaceInfo = useSelector<RootState>(
(s) => s.entities.workspace ?? {}
Expand Down Expand Up @@ -90,7 +90,7 @@ export const SendToMicroreactModal = (props: Props) => {
</div>
<div>
{t("Tree method")}:
<TreeMethodSelect onChange={setTreeMethod} />
<TreeMethodSelect value={treeMethod} onChange={setTreeMethod} />
</div>
</div>
) : null}
Expand Down
47 changes: 37 additions & 10 deletions app/src/app/workspaces/send-to-workspace-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useState } from "react";
import { Spinner, useToast } from "@chakra-ui/react";
import { AnalysisResult } from "sap-client";
import { AnalysisResult, Workspace } from "sap-client";
import {
Button,
Modal,
Expand All @@ -17,6 +17,8 @@ import { WorkspaceSelect } from "./workspace-select";
import { useMutation } from "redux-query-react";
import { useHistory } from "react-router";
import { createWorkspace, updateWorkspace } from "./workspaces-query-configs";
import { useSelector } from "react-redux";
import { RootState } from "app/root-reducer";

type Props = {
selection: DataTableSelection<AnalysisResult>;
Expand All @@ -30,19 +32,44 @@ export const SendToWorkspaceModal = (props: Props) => {
const [workspace, setWorkspace] = React.useState<string>();
const history = useHistory();
const toast = useToast();
const workspaces = useSelector<RootState>((s) =>
Object.values(s.entities.workspaces ?? {})
) as Array<Workspace>;

const [{ isPending, status }, sendToWorkspace] = useMutation(() => {
const samples = Object.values(selection).map((s) => s.original.id);
return updateWorkspace({
workspaceId: workspace,
updateWorkspace: { samples },
});
});
const [{ isPending, status }, sendToWorkspace] = useMutation(
(name: string) => {
const samples = Object.values(selection).map((s) => s.original.id);
if (workspace === "-- New workspace") {
setWorkspace(name);
return createWorkspace({
name,
samples,
});
}
return updateWorkspace({
workspaceId: workspace,
updateWorkspace: { samples },
});
}
);

const onSend = useCallback(async () => {
if (workspace === "-- New workspace") {
const name = prompt(t("Workspace name"));
if (name) {
const exists = workspaces.find((w) => w.name === name);
if (exists) {
alert(t("Workspace already exists."));
return;
}
setIsSending(true);
sendToWorkspace(name);
}
return;
}
setIsSending(true);
sendToWorkspace();
}, [setIsSending, sendToWorkspace]);
sendToWorkspace(workspace);
}, [setIsSending, sendToWorkspace, workspaces, workspace, t]);

const onWorkspaceChange = useCallback((id: string) => {
setWorkspace(id);
Expand Down
8 changes: 6 additions & 2 deletions app/src/app/workspaces/tree-method-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { TreeMethod } from "sap-client";

type Props = {
onChange: (id: string) => void;
value: string;
};

export const TreeMethodSelect = (props: Props) => {
const { onChange } = props;
const { onChange, value } = props;

const [treeMethod, setTreeMethod] = useState<OptionTypeBase>();
const [treeMethod, setTreeMethod] = useState<OptionTypeBase>({
value,
label: value,
});

const treeMethods = useMemo(() => {
const methods = new Array<string>();
Expand Down
40 changes: 33 additions & 7 deletions app/src/app/workspaces/workspace-select.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { RootState } from "app/root-reducer";
import React, { useCallback, useState } from "react";
import React, { useCallback, useMemo, useState } from "react";
import { useSelector } from "react-redux";
import Select, { OptionTypeBase } from "react-select";
import { useRequest } from "redux-query-react";
import { Workspace } from "sap-client/models";
import { fetchWorkspaces } from "./workspaces-query-configs";
import { useTranslation } from "react-i18next";

type Props = {
onChange: (id: string) => void;
Expand All @@ -13,6 +14,7 @@ type Props = {
export const WorkspaceSelect = (props: Props) => {
const { onChange } = props;
const [workspacesQueryState] = useRequest(fetchWorkspaces());
const { t } = useTranslation();

const workspaces = useSelector<RootState>((s) =>
Object.values(s.entities.workspaces ?? {})
Expand All @@ -28,14 +30,38 @@ export const WorkspaceSelect = (props: Props) => {
[setWorkspace, onChange]
);

const options = useMemo(() => {
const newOptions = new Array<
| { label: string; value: string }
| {
label: string;
options: Array<{
label: string;
value: string;
}>;
}
>({
label: `-- ${t("New workspace")}`,
value: "-- New workspace",
});

const workspaceOptions = workspaces.map((w) => {
return {
value: w.id,
label: w.name,
};
});
newOptions.push({
label: t("Existing workspaces"),
options: workspaceOptions,
});

return newOptions;
}, [workspaces, t]);

return (
<Select
options={workspaces.map((w) => {
return {
value: w.id,
label: w.name,
};
})}
options={options}
defaultValue={workspace}
value={workspace}
isLoading={workspacesQueryState.isPending}
Expand Down
8 changes: 8 additions & 0 deletions app/src/sap-client/models/CreateWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ export interface CreateWorkspace {
* @memberof CreateWorkspace
*/
name: string;
/**
*
* @type {Array<string>}
* @memberof CreateWorkspace
*/
samples?: Array<string>;
}

export function CreateWorkspaceFromJSON(json: any): CreateWorkspace {
return {
'name': json['name'],
'samples': !exists(json, 'samples') ? undefined : json['samples'],
};
}

Expand All @@ -38,6 +45,7 @@ export function CreateWorkspaceToJSON(value?: CreateWorkspace): any {
}
return {
'name': value.name,
'samples': value.samples,
};
}

Expand Down
5 changes: 5 additions & 0 deletions openapi_specs/SOFI/SOFI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,11 @@ components:
name:
type: string
title: Name
samples:
type: array
items:
type: string
title: Samples
type: object
required:
- name
Expand Down
5 changes: 5 additions & 0 deletions web/openapi_specs/SOFI/SOFI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,11 @@ components:
name:
type: string
title: Name
samples:
type: array
items:
type: string
title: Samples
type: object
required:
- name
Expand Down
28 changes: 27 additions & 1 deletion web/src/SAP/generated/models/create_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@ class CreateWorkspace(Model):
Do not edit the class manually.
"""

def __init__(self, name=None): # noqa: E501
def __init__(self, name=None, samples=None): # noqa: E501
"""CreateWorkspace - a model defined in OpenAPI
:param name: The name of this CreateWorkspace. # noqa: E501
:type name: str
:param samples: The samples of this CreateWorkspace. # noqa: E501
:type samples: List[str]
"""
self.openapi_types = {
'name': str,
'samples': List[str],
}

self.attribute_map = {
'name': 'name',
'samples': 'samples',
}

self._name = name
self._samples = samples

@classmethod
def from_dict(cls, dikt):
Expand Down Expand Up @@ -66,3 +71,24 @@ def name(self, name):
raise ValueError("Invalid value for `name`, must not be `None`") # noqa: E501

self._name = name

@property
def samples(self):
"""Gets the samples of this CreateWorkspace.
:return: The samples of this CreateWorkspace.
:rtype: List[str]
"""
return self._samples

@samples.setter
def samples(self, samples):
"""Sets the samples of this CreateWorkspace.
:param samples: The samples of this CreateWorkspace.
:type samples: List[str]
"""

self._samples = samples
8 changes: 8 additions & 0 deletions web/src/SAP/generated/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1540,10 +1540,18 @@ components:
CreateWorkspace:
example:
name: name
samples:
- samples
- samples
properties:
name:
title: Name
type: string
samples:
items:
type: string
title: Samples
type: array
required:
- name
type: object
Expand Down
3 changes: 2 additions & 1 deletion web/src/SAP/generated/test/test_workspaces_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def test_create_workspace(self):
"""
create_workspace = {
"name" : "name"
"name" : "name",
"samples" : [ "samples", "samples" ]
}
headers = {
'Content-Type': 'application/json',
Expand Down
11 changes: 9 additions & 2 deletions web/src/SAP/src/repositories/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ def get_workspace(user: str, workspace_id: str):
conn = get_connection()
db = conn[DB_NAME]
workspaces = db[WORKSPACES_COL_NAME]
workspace = trim(workspaces.find_one({"created_by": user, "_id": ObjectId(workspace_id)}))
workspace["samples"] = list(map(get_sequence, workspace["samples"]))
if ObjectId.is_valid(workspace_id):
workspace = trim(workspaces.find_one({"created_by": user, "_id": ObjectId(workspace_id)}))
else:
workspace = trim(workspaces.find_one({"created_by": user, "name": workspace_id}))

if workspace["samples"] is None:
workspace["samples"] = []
else:
workspace["samples"] = list(map(get_sequence, workspace["samples"]))

return workspace

Expand Down

0 comments on commit 062fb6e

Please sign in to comment.