Skip to content

Commit

Permalink
feat: remove samples from workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
allanhvam committed May 30, 2024
1 parent df5cb28 commit f8d121c
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 19 deletions.
8 changes: 6 additions & 2 deletions app/src/app/analysis/data-table/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ type DataTableProps<T extends NotEmpty> = {
renderCellControl: (
rowId: string,
columnId: string,
value: string
value: string,
columnIndex: number,
original: T
) => JSX.Element;
};

Expand Down Expand Up @@ -494,7 +496,9 @@ function DataTable<T extends NotEmpty>(props: DataTableProps<T>) {
{renderCellControl(
rowId,
columnId,
rows[rowIndex - 1].original[columnId]
rows[rowIndex - 1].original[columnId],
columnIndex,
rows[rowIndex - 1].original
)}
</Flex>
</div>
Expand Down
70 changes: 53 additions & 17 deletions app/src/app/workspaces/workspace.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from "react";
import { Box, Heading, Spinner } from "@chakra-ui/react";
import React, { useCallback } from "react";
import { Box, Heading, IconButton, Spinner } from "@chakra-ui/react";
import { useTranslation } from "react-i18next";
import HalfHolyGrailLayout from "layouts/half-holy-grail";
import { useRequest } from "redux-query-react";
import { getWorkspace } from "./workspaces-query-configs";
import { useMutation, useRequest } from "redux-query-react";
import {
getWorkspace,
removeWorkspaceSample,
} from "./workspaces-query-configs";
import { useSelector } from "react-redux";
import { RootState } from "app/root-reducer";
import {
Expand All @@ -20,6 +23,7 @@ import { Column } from "react-table";
import { UserDefinedViewInternal } from "models/user-defined-view-internal";
import { Loading } from "loading";
import { AnalysisViewSelector } from "app/analysis/view-selector/analysis-view-selector";
import { DeleteIcon } from "@chakra-ui/icons";

type Props = {
id: string;
Expand Down Expand Up @@ -64,22 +68,40 @@ export function Workspace(props: Props) {
[columnConfigs, t]
);

const deleteWorkspaceSampleCallback = useMutation((sampleId: string) =>
removeWorkspaceSample({ sampleId, workspaceId: id })
)[1];

const onRemoveSample = useCallback(
(sampleId) => {
const ok = confirm(
t("Are you sure you want to remove the sequence from the workspace?")
);
if (ok) {
deleteWorkspaceSampleCallback(sampleId);
}
},
[deleteWorkspaceSampleCallback, t]
);

const renderCellControl = React.useCallback(
(rowId: string, columnId: string, value) => {
(
rowId: string,
columnId: string,
value,
columnIndex: number,
original: AnalysisResult
) => {
if (
value !== 0 &&
value !== false &&
!value &&
!columnConfigs[columnId].editable
value === undefined ||
value === null ||
String(value) === "Invalid Date"
) {
return <div />;
value = "";
}
let v = `${value}`;
if (v === "Invalid Date") {
return <div />;
}
// any other dates
else if (value instanceof Date) {

if (value instanceof Date) {
// Fancy libraries could be used, but this will do the trick just fine
v = value.toISOString().split("T")[0];
} else if (
Expand Down Expand Up @@ -119,9 +141,23 @@ export function Workspace(props: Props) {
}
}

return <div style={{ backgroundColor: "white" }}>{`${v}`}</div>;
return (
<div style={{ backgroundColor: "white" }}>
{columnIndex === 0 ? (
<IconButton
size="1em"
variant="unstyled"
onClick={() => onRemoveSample(original.id)}
aria-label="Remove"
icon={<DeleteIcon marginTop="-0.5em" />}
ml="1"
/>
) : null}
{`${v}`}
</div>
);
},
[columnConfigs]
[onRemoveSample]
);

if (!columnLoadState.isFinished) {
Expand Down
17 changes: 17 additions & 0 deletions app/src/app/workspaces/workspaces-query-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
createWorkspace as createWorkspaceApi,
getWorkspace as getWorkspaceApi,
postWorkspace as postWorkspaceApi,
deleteWorkspaceSample as deleteWorkspaceSampleApi,
DeleteWorkspaceRequest,
PostWorkspaceRequest,
DeleteWorkspaceSampleRequest,
} from "sap-client";
import { CreateWorkspace, WorkspaceInfo } from "sap-client/models";
import { getUrl } from "service";
Expand Down Expand Up @@ -66,6 +68,21 @@ export const deleteWorkspace = (params: DeleteWorkspaceRequest) => {
return base;
};

export const removeWorkspaceSample = (params: DeleteWorkspaceSampleRequest) => {
const base = deleteWorkspaceSampleApi(params);
base.url = getUrl(base.url);
base.update = {
workspace: (oldValue) => {
return {
...oldValue,
samples: oldValue.samples.filter((s) => s.id !== params.sampleId),
};
},
};
base.force = true;
return base;
};

export const createWorkspace = (params: CreateWorkspace) => {
const base = createWorkspaceApi({ createWorkspace: params });
base.url = getUrl(base.url);
Expand Down
55 changes: 55 additions & 0 deletions app/src/sap-client/apis/WorkspacesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export interface DeleteWorkspaceRequest {
workspaceId: string;
}

export interface DeleteWorkspaceSampleRequest {
workspaceId: string;
sampleId: string;
}

export interface GetWorkspaceRequest {
workspaceId: string;
}
Expand Down Expand Up @@ -135,6 +140,56 @@ export function deleteWorkspace<T>(requestParameters: DeleteWorkspaceRequest, re
return deleteWorkspaceRaw(requestParameters, requestConfig);
}

/**
* Delete sample from workspace
*/
function deleteWorkspaceSampleRaw<T>(requestParameters: DeleteWorkspaceSampleRequest, requestConfig: runtime.TypedQueryConfig<T, void> = {}): QueryConfig<T> {
if (requestParameters.workspaceId === null || requestParameters.workspaceId === undefined) {
throw new runtime.RequiredError('workspaceId','Required parameter requestParameters.workspaceId was null or undefined when calling deleteWorkspaceSample.');
}

if (requestParameters.sampleId === null || requestParameters.sampleId === undefined) {
throw new runtime.RequiredError('sampleId','Required parameter requestParameters.sampleId was null or undefined when calling deleteWorkspaceSample.');
}

let queryParameters = null;


const headerParameters : runtime.HttpHeaders = {};


const { meta = {} } = requestConfig;

meta.authType = ['bearer'];
const config: QueryConfig<T> = {
url: `${runtime.Configuration.basePath}/workspace/{workspace_id}/{sample_id}`.replace(`{${"workspace_id"}}`, encodeURIComponent(String(requestParameters.workspaceId))).replace(`{${"sample_id"}}`, encodeURIComponent(String(requestParameters.sampleId))),
meta,
update: requestConfig.update,
queryKey: requestConfig.queryKey,
optimisticUpdate: requestConfig.optimisticUpdate,
force: requestConfig.force,
rollback: requestConfig.rollback,
options: {
method: 'DELETE',
headers: headerParameters,
},
body: queryParameters,
};

const { transform: requestTransform } = requestConfig;
if (requestTransform) {
}

return config;
}

/**
* Delete sample from workspace
*/
export function deleteWorkspaceSample<T>(requestParameters: DeleteWorkspaceSampleRequest, requestConfig?: runtime.TypedQueryConfig<T, void>): QueryConfig<T> {
return deleteWorkspaceSampleRaw(requestParameters, requestConfig);
}

/**
* Get an existing workspace
*/
Expand Down
24 changes: 24 additions & 0 deletions openapi_specs/SOFI/SOFI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ paths:
tags:
- workspaces
x-openapi-router-controller: web.src.SAP.generated.controllers.workspaces_controller
/workspace/{workspace_id}/{sample_id}:
delete:
description: Delete sample from workspace
operationId: delete_workspace_sample
tags:
- workspaces
x-openapi-router-controller: web.src.SAP.generated.controllers.workspaces_controller
parameters:
- name: workspace_id
in: path
required: true
schema:
type: string
description: Id of workspace to modify
- name: sample_id
in: path
required: true
schema:
type: string
description: Id of the sample to remove
responses:
'204':
description: Sample removed from workspace successfully.

/workspaces/{workspace_id}:
get:
description: Get an existing workspace
Expand Down
24 changes: 24 additions & 0 deletions web/openapi_specs/SOFI/SOFI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ paths:
tags:
- workspaces
x-openapi-router-controller: web.src.SAP.generated.controllers.workspaces_controller
/workspace/{workspace_id}/{sample_id}:
delete:
description: Delete sample from workspace
operationId: delete_workspace_sample
tags:
- workspaces
x-openapi-router-controller: web.src.SAP.generated.controllers.workspaces_controller
parameters:
- name: workspace_id
in: path
required: true
schema:
type: string
description: Id of workspace to modify
- name: sample_id
in: path
required: true
schema:
type: string
description: Id of the sample to remove
responses:
'204':
description: Sample removed from workspace successfully.

/workspaces/{workspace_id}:
get:
description: Get an existing workspace
Expand Down
14 changes: 14 additions & 0 deletions web/src/SAP/generated/controllers/workspaces_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ def delete_workspace(user, token_info, workspace_id): # noqa: E501
"""
return WorkspacesController.delete_workspace(user, token_info, workspace_id)

def delete_workspace_sample(user, token_info, workspace_id, sample_id): # noqa: E501
"""delete_workspace_sample
Delete sample from workspace # noqa: E501
:param workspace_id: Id of workspace to modify
:type workspace_id: str
:param sample_id: Id of the sample to remove
:type sample_id: str
:rtype: None
"""
return WorkspacesController.delete_workspace_sample(user, token_info, workspace_id, sample_id)

def get_workspace(user, token_info, workspace_id): # noqa: E501
"""get_workspace
Expand Down
27 changes: 27 additions & 0 deletions web/src/SAP/generated/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,33 @@ paths:
tags:
- user
x-openapi-router-controller: web.src.SAP.generated.controllers.user_controller
/workspace/{workspace_id}/{sample_id}:
delete:
description: Delete sample from workspace
operationId: delete_workspace_sample
parameters:
- description: Id of workspace to modify
explode: false
in: path
name: workspace_id
required: true
schema:
type: string
style: simple
- description: Id of the sample to remove
explode: false
in: path
name: sample_id
required: true
schema:
type: string
style: simple
responses:
"204":
description: Sample removed from workspace successfully.
tags:
- workspaces
x-openapi-router-controller: web.src.SAP.generated.controllers.workspaces_controller
/workspaces:
get:
description: Gets workspaces
Expand Down
15 changes: 15 additions & 0 deletions web/src/SAP/generated/test/test_workspaces_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ def test_delete_workspace(self):
self.assert200(response,
'Response body is : ' + response.data.decode('utf-8'))

def test_delete_workspace_sample(self):
"""Test case for delete_workspace_sample
"""
headers = {
'Authorization': 'Bearer special-key',
}
response = self.client.open(
'/api/workspace/{workspace_id}/{sample_id}'.format(workspace_id='workspace_id_example')sample_id='sample_id_example'),
method='DELETE',
headers=headers)
self.assert200(response,
'Response body is : ' + response.data.decode('utf-8'))

def test_get_workspace(self):
"""Test case for get_workspace
Expand Down
5 changes: 5 additions & 0 deletions web/src/SAP/src/controllers/WorkspacesController.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask.json import jsonify
from ..repositories.workspaces import get_workspaces as get_workspaces_db
from ..repositories.workspaces import delete_workspace as delete_workspace_db
from ..repositories.workspaces import delete_workspace_sample as delete_workspace_sample_db
from ..repositories.workspaces import create_workspace as create_workspace_db
from ..repositories.workspaces import update_workspace as update_workspace_db
from ..repositories.workspaces import get_workspace as get_workspace_db
Expand All @@ -29,3 +30,7 @@ def post_workspace(user, token_info, workspace_id: str, body):

def get_workspace(user, token_info, workspace_id: str):
return jsonify(get_workspace_db(user, workspace_id))

def delete_workspace_sample(user, token_info, workspace_id, sample_id):
res = delete_workspace_sample_db(user, workspace_id, sample_id)
return None if res.modified_count > 0 else abort(404)
10 changes: 10 additions & 0 deletions web/src/SAP/src/repositories/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def delete_workspace(user: str, workspace_id: str):
{"_id": ObjectId(workspace_id), "created_by": user}
)

def delete_workspace_sample(user: str, workspace_id: str, sample_id: str):
conn = get_connection()
mydb = conn[DB_NAME]
workspaces = mydb[WORKSPACES_COL_NAME]

filter = {'created_by': user, '_id': ObjectId(workspace_id)}
update = {"$pull": {"samples": sample_id}}
return workspaces.update_one(filter, update, upsert=True)


def create_workspace(user: str, workspace: CreateWorkspace):
conn = get_connection()
db = conn[DB_NAME]
Expand Down

0 comments on commit f8d121c

Please sign in to comment.