Skip to content

Commit

Permalink
refactor: Fixed file upload checks. Implemented endpoint for model-co…
Browse files Browse the repository at this point in the history
…nversion after file upload.
  • Loading branch information
mheggelund committed Oct 18, 2023
1 parent 62dda86 commit 97332eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
14 changes: 12 additions & 2 deletions src/features/AddModel/ModelMetadata/ModelMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
} from '@equinor/eds-core-react';
import MetadataProps, { ErrorType } from '../AddModelDialog/AddModelDialog';

import { useQuery } from '@tanstack/react-query';
import { AnalogueList, AnaloguesService } from '../../../api/generated';
import * as Styled from './ModelMetadata.styled';

export const ModelMetadata = ({
Expand All @@ -26,6 +28,13 @@ export const ModelMetadata = ({
analogue: ['Analouge1', 'Analouge2'],
};

const { isLoading, data } = useQuery({
queryKey: ['apiParameters'],
queryFn: () => AnaloguesService.getApiAnalogues(),
});

if (isLoading || !data?.success) return <p>Loading ...</p>;

const handleInput = (e: AutocompleteChanges<string>, target: string) => {
setMetadata({ ...metadata, [target]: e.selectedItems });
};
Expand Down Expand Up @@ -76,9 +85,10 @@ export const ModelMetadata = ({
<Styled.AutocompleteRow>
<Autocomplete
label="Analogue (optional)"
options={fields.analogue}
options={data.data}
optionLabel={(option) => option.name}
multiple
onOptionsChange={(e: AutocompleteChanges<string>) =>
onOptionsChange={(e: AutocompleteChanges<AnalogueList>) =>
setMetadata({ ...metadata, analogue: e.selectedItems })
}
></Autocomplete>
Expand Down
2 changes: 1 addition & 1 deletion src/features/ModelView/ModelSourceView/ModelSourceView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Table, Typography } from '@equinor/eds-core-react';
import { useQuery } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import { AnalogueModelsService, UploadList } from '../../../api/generated';
import { useQuery } from '@tanstack/react-query';

export const ModelSourceView = () => {
const { id } = useParams();
Expand Down
57 changes: 38 additions & 19 deletions src/pages/Browse/Browse.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* eslint-disable max-lines-per-function */
import { Button, Snackbar, Typography } from '@equinor/eds-core-react';
import { useState } from 'react';
import { Table } from '../../components/Table';
import * as Styled from './Browse.styled';
import { useMutation } from '@tanstack/react-query';
import { useState } from 'react';
import {
AnalogueModelsService,
ConvertAnalogueModelCommand,
CreateAnalogueModelCommand,
JobsService,
} from '../../api/generated';
import { Table } from '../../components/Table';
import { AddModelDialog } from '../../features/AddModel/AddModelDialog/AddModelDialog';
import * as Styled from './Browse.styled';

enum UploadProcess {
STARTED = 'We are uploading your new model. Please keep this browser tab open.',
Expand All @@ -20,6 +23,12 @@ type MutationContract = {
file: Blob;
};

const ModelBody: CreateAnalogueModelCommand = {
name: 'Model test',
description: 'New test of the model',
sourceType: 'ResQML',
};

export const Browse = () => {
const createModel = useMutation({
mutationFn: AnalogueModelsService.postApiAnalogueModels,
Expand All @@ -34,6 +43,12 @@ export const Browse = () => {
},
});

const convertModelFile = useMutation({
mutationFn: (modelId: ConvertAnalogueModelCommand) => {
return JobsService.postApiJobsComputeModelConversions(modelId);
},
});

const [isAddModelDialog, setAddModelDialog] = useState<boolean>(false);
const [uploadStatus, setUploadStatus] = useState<string>();

Expand All @@ -47,26 +62,30 @@ export const Browse = () => {

async function uploadModel(file: File) {
setUploadStatus(UploadProcess.STARTED);
const modelUpload = await createModel.mutateAsync(
// TODO
{
name: 'testModel',
description: 'description',
sourceType: 'Deltares',
} as CreateAnalogueModelCommand,
);

if (createModel?.isSuccess) {

const modelUpload = await createModel.mutateAsync(ModelBody);

if (createModel.error === null && modelUpload.success) {
toggleDialog();
const fileUpload = await uploadNCFile.mutateAsync({
const FileUploadBody: MutationContract = {
id: modelUpload.data.analogueModelId ?? '',
file: file,
} as MutationContract);
};
const fileUpload = await uploadNCFile.mutateAsync(FileUploadBody);

if (fileUpload && uploadNCFile.isSuccess)
if (uploadNCFile.error === null && fileUpload.success) {
setUploadStatus(UploadProcess.SUCCESS);
else if (!uploadNCFile.isSuccess) {

const id = modelUpload.data.analogueModelId;
const convert = await convertModelFile.mutateAsync({
modelId: id,
});

// eslint-disable-next-line no-console
console.log(convert);
} else if (uploadNCFile.error) {
setUploadStatus(UploadProcess.FAILED);

// TODO: show validation message
}
}
Expand All @@ -78,7 +97,7 @@ export const Browse = () => {
<Typography variant="h1">Browse all models</Typography>
<div className="btn-div">
<Button onClick={toggleDialog}>Add new model</Button>
<Button href="/model/bf2171bc-2f5e-44a1-f6e0-08dbb5ed15e2/details">
<Button href="/model/4e999a96-34a3-4121-998d-08dbb2a7609c/details">
Model view - Hardkodet
</Button>
</div>
Expand All @@ -91,7 +110,7 @@ export const Browse = () => {
/>
<Snackbar
open={!!uploadStatus}
autoHideDuration={5000}
autoHideDuration={15000}
onClose={clearStatus}
>
{uploadStatus}
Expand Down

0 comments on commit 97332eb

Please sign in to comment.