From 974d2885ae13e08f8e490ea74356edfa87af3379 Mon Sep 17 00:00:00 2001 From: Thomas L Fagermyr Date: Tue, 10 Dec 2024 10:16:16 +0100 Subject: [PATCH 1/2] chore: Generated files --- src/api/generated/core/request.ts | 103 +++++++++--------- .../UpdateVariogramResultCommandBody.ts | 1 + .../models/UpdateVariogramResultDto.ts | 3 +- src/api/generated/services/ResultsService.ts | 8 +- 4 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/api/generated/core/request.ts b/src/api/generated/core/request.ts index 1142d432..b018a07c 100644 --- a/src/api/generated/core/request.ts +++ b/src/api/generated/core/request.ts @@ -2,10 +2,6 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -import axios from 'axios'; -import type { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios'; -import FormData from 'form-data'; - import { ApiError } from './ApiError'; import type { ApiRequestOptions } from './ApiRequestOptions'; import type { ApiResult } from './ApiResult'; @@ -42,10 +38,6 @@ export const isFormData = (value: any): value is FormData => { return value instanceof FormData; }; -export const isSuccess = (status: number): boolean => { - return status >= 200 && status < 300; -}; - export const base64 = (str: string): string => { try { return btoa(str); @@ -144,24 +136,22 @@ export const resolve = async (options: ApiRequestOptions, resolver?: T | Reso return resolver; }; -export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions, formData?: FormData): Promise> => { +export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise => { const token = await resolve(options, config.TOKEN); const username = await resolve(options, config.USERNAME); const password = await resolve(options, config.PASSWORD); const additionalHeaders = await resolve(options, config.HEADERS); - const formHeaders = typeof formData?.getHeaders === 'function' && formData?.getHeaders() || {} const headers = Object.entries({ Accept: 'application/json', ...additionalHeaders, ...options.headers, - ...formHeaders, }) - .filter(([_, value]) => isDefined(value)) - .reduce((headers, [key, value]) => ({ - ...headers, - [key]: String(value), - }), {} as Record); + .filter(([_, value]) => isDefined(value)) + .reduce((headers, [key, value]) => ({ + ...headers, + [key]: String(value), + }), {} as Record); if (isStringWithValue(token)) { headers['Authorization'] = `Bearer ${token}`; @@ -184,53 +174,52 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio } } - return headers; + return new Headers(headers); }; export const getRequestBody = (options: ApiRequestOptions): any => { - if (options.body) { - return options.body; + if (options.body !== undefined) { + if (options.mediaType?.includes('/json')) { + return JSON.stringify(options.body) + } else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) { + return options.body; + } else { + return JSON.stringify(options.body); + } } return undefined; }; -export const sendRequest = async ( +export const sendRequest = async ( config: OpenAPIConfig, options: ApiRequestOptions, url: string, body: any, formData: FormData | undefined, - headers: Record, - onCancel: OnCancel, - axiosClient: AxiosInstance -): Promise> => { - const source = axios.CancelToken.source(); - - const requestConfig: AxiosRequestConfig = { - url, + headers: Headers, + onCancel: OnCancel +): Promise => { + const controller = new AbortController(); + + const request: RequestInit = { headers, - data: body ?? formData, + body: body ?? formData, method: options.method, - withCredentials: config.WITH_CREDENTIALS, - cancelToken: source.token, + signal: controller.signal, }; - onCancel(() => source.cancel('The user aborted a request.')); - - try { - return await axiosClient.request(requestConfig); - } catch (error) { - const axiosError = error as AxiosError; - if (axiosError.response) { - return axiosError.response; - } - throw error; + if (config.WITH_CREDENTIALS) { + request.credentials = config.CREDENTIALS; } + + onCancel(() => controller.abort()); + + return await fetch(url, request); }; -export const getResponseHeader = (response: AxiosResponse, responseHeader?: string): string | undefined => { +export const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => { if (responseHeader) { - const content = response.headers[responseHeader]; + const content = response.headers.get(responseHeader); if (isString(content)) { return content; } @@ -238,9 +227,22 @@ export const getResponseHeader = (response: AxiosResponse, responseHeader?: return undefined; }; -export const getResponseBody = (response: AxiosResponse): any => { +export const getResponseBody = async (response: Response): Promise => { if (response.status !== 204) { - return response.data; + try { + const contentType = response.headers.get('Content-Type'); + if (contentType) { + const jsonTypes = ['application/json', 'application/problem+json'] + const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); + if (isJSON) { + return await response.json(); + } else { + return await response.text(); + } + } + } catch (error) { + console.error(error); + } } return undefined; }; @@ -283,26 +285,25 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): * Request method * @param config The OpenAPI configuration object * @param options The request options from the service - * @param axiosClient The axios client instance to use * @returns CancelablePromise * @throws ApiError */ -export const request = (config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise => { +export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise => { return new CancelablePromise(async (resolve, reject, onCancel) => { try { const url = getUrl(config, options); const formData = getFormData(options); const body = getRequestBody(options); - const headers = await getHeaders(config, options, formData); + const headers = await getHeaders(config, options); if (!onCancel.isCancelled) { - const response = await sendRequest(config, options, url, body, formData, headers, onCancel, axiosClient); - const responseBody = getResponseBody(response); + const response = await sendRequest(config, options, url, body, formData, headers, onCancel); + const responseBody = await getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); const result: ApiResult = { url, - ok: isSuccess(response.status), + ok: response.ok, status: response.status, statusText: response.statusText, body: responseHeader ?? responseBody, diff --git a/src/api/generated/models/UpdateVariogramResultCommandBody.ts b/src/api/generated/models/UpdateVariogramResultCommandBody.ts index 76625b25..534ff0aa 100644 --- a/src/api/generated/models/UpdateVariogramResultCommandBody.ts +++ b/src/api/generated/models/UpdateVariogramResultCommandBody.ts @@ -5,5 +5,6 @@ export type UpdateVariogramResultCommandBody = { status: string; + identifier: number; }; diff --git a/src/api/generated/models/UpdateVariogramResultDto.ts b/src/api/generated/models/UpdateVariogramResultDto.ts index fb815c56..f3997341 100644 --- a/src/api/generated/models/UpdateVariogramResultDto.ts +++ b/src/api/generated/models/UpdateVariogramResultDto.ts @@ -7,7 +7,8 @@ import type { ResultStatus } from './ResultStatus'; export type UpdateVariogramResultDto = { analogueModelId: string; - variogramResultId: string; + computeCaseId: string; status: ResultStatus; + identifier: number; }; diff --git a/src/api/generated/services/ResultsService.ts b/src/api/generated/services/ResultsService.ts index 1c708bd6..0827f3aa 100644 --- a/src/api/generated/services/ResultsService.ts +++ b/src/api/generated/services/ResultsService.ts @@ -90,22 +90,22 @@ export class ResultsService { /** * @param id - * @param variogramId + * @param computeCaseId * @param requestBody * @returns UpdateVariogramResultCommandResponse Success * @throws ApiError */ public static putApiAnalogueModelsResultsVariogram( id: string, - variogramId: string, + computeCaseId: string, requestBody?: UpdateVariogramResultCommandBody, ): CancelablePromise { return __request(OpenAPI, { method: 'PUT', - url: '/api/analogue-models/{id}/results/variogram/{variogramId}', + url: '/api/analogue-models/{id}/results/variogram/{computeCaseId}', path: { 'id': id, - 'variogramId': variogramId, + 'computeCaseId': computeCaseId, }, body: requestBody, mediaType: 'application/json-patch+json', From d18e1b4fb6fab5b10e5b9c1149cb30f0b7f55138 Mon Sep 17 00:00:00 2001 From: Thomas L Fagermyr Date: Tue, 10 Dec 2024 14:10:57 +0100 Subject: [PATCH 2/2] feat: Added status switch for variogram result --- src/api/generated/services/ResultsService.ts | 4 +- .../TanStackTable/TanStackTable.tsx | 79 ++++++++++++++++++- src/hooks/useMutateResults.ts | 8 +- 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/src/api/generated/services/ResultsService.ts b/src/api/generated/services/ResultsService.ts index 0827f3aa..c24df81a 100644 --- a/src/api/generated/services/ResultsService.ts +++ b/src/api/generated/services/ResultsService.ts @@ -95,14 +95,14 @@ export class ResultsService { * @returns UpdateVariogramResultCommandResponse Success * @throws ApiError */ - public static putApiAnalogueModelsResultsVariogram( + public static putApiAnalogueModelsComputecasesResults( id: string, computeCaseId: string, requestBody?: UpdateVariogramResultCommandBody, ): CancelablePromise { return __request(OpenAPI, { method: 'PUT', - url: '/api/analogue-models/{id}/results/variogram/{computeCaseId}', + url: '/api/analogue-models/{id}/computecases/{computeCaseId}/results', path: { 'id': id, 'computeCaseId': computeCaseId, diff --git a/src/features/Results/CaseResult/CaseResultView/VariogramCaseResult/VariogramResultTable/TanStackTable/TanStackTable.tsx b/src/features/Results/CaseResult/CaseResultView/VariogramCaseResult/VariogramResultTable/TanStackTable/TanStackTable.tsx index def237b2..0951e8e5 100644 --- a/src/features/Results/CaseResult/CaseResultView/VariogramCaseResult/VariogramResultTable/TanStackTable/TanStackTable.tsx +++ b/src/features/Results/CaseResult/CaseResultView/VariogramCaseResult/VariogramResultTable/TanStackTable/TanStackTable.tsx @@ -1,9 +1,9 @@ /* eslint-disable max-lines */ /* eslint-disable sort-imports */ /* eslint-disable max-lines-per-function */ -import { Fragment } from 'react'; +import { ChangeEvent, Fragment } from 'react'; -import { Button, Icon } from '@equinor/eds-core-react'; +import { Button, Icon, Switch } from '@equinor/eds-core-react'; import { chevron_down as DOWN, chevron_right as RIGHT, @@ -19,11 +19,17 @@ import { import { GetVariogramResultsDto, GetVariogramResultsVariogramResultFileDto, + ResultStatus, + UpdateVariogramResultCommandBody, + // UpdateVariogramResultCommandBody, } from '../../../../../../../api/generated'; import { usePepmContextStore } from '../../../../../../../hooks/GlobalState'; import { roundResultString } from '../../../../../../../utils/RoundResultString'; import { SubRowResult } from '../SubRowResult/SubRowResult'; import * as Styled from './TanStackTable.styled'; +import { useIsOwnerOrAdmin } from '../../../../../../../hooks/useIsOwnerOrAdmin'; +import { useMutateVariogramResult } from '../../../../../../../hooks/useMutateResults'; +// import { useMutateVariogramResult } from '../../../../../../../hooks/useMutateResults'; export interface ResultObjectType { variogramResultId: string; @@ -44,6 +50,7 @@ export interface ResultObjectType { modelArea: string; variogramModel: string; identifier: number; + status: ResultStatus; subRows?: ResultObjectType[]; } @@ -131,7 +138,53 @@ export const TanStackTable = ({ }: { resultList: GetVariogramResultsDto[]; }) => { - const { computeCases } = usePepmContextStore(); + const { + computeCases, + analogueModel, + variogramResults, + updateVariogramResult, + } = usePepmContextStore(); + const isOwner = useIsOwnerOrAdmin(); + const mutateVariogramResult = useMutateVariogramResult(); + + const putUpdateVariogramResult = async ( + status: ResultStatus, + variogram: ResultObjectType, + ) => { + const requestBody: UpdateVariogramResultCommandBody = { + status: status, + identifier: variogram.identifier, + }; + + const variogramUpdate = await mutateVariogramResult.mutateAsync({ + id: analogueModel.analogueModelId, + computeCaseId: variogram.computeCaseId, + requestBody: requestBody, + }); + + if (variogramUpdate.success) { + variogramResults.forEach((elem) => { + if ( + elem.computeCaseId === variogram.computeCaseId && + elem.identifier === variogram.identifier + ) + updateVariogramResult({ ...elem, status: status }); + }); + } + }; + + const updateStatus = (checked: boolean, variogram: ResultObjectType) => { + if (checked) { + putUpdateVariogramResult(ResultStatus.PUBLISH, variogram); + } else { + putUpdateVariogramResult(ResultStatus.DRAFT, variogram); + } + }; + + const checkedStatus = (variogram: ResultObjectType) => { + if (variogram.status === ResultStatus.PUBLISH) return true; + return false; + }; const getSubRows = (computeCaseId: string, identifier: number) => { const subRowArray: ResultObjectType[] = []; @@ -178,6 +231,7 @@ export const TanStackTable = ({ variogramModel: e.family ? e.family : '', quality: roundResultString(e.quality), identifier: e.identifier, + status: e.status, }; subRowArray.push(element); @@ -230,6 +284,24 @@ export const TanStackTable = ({ header: () =>
Model Area
, id: 'modelArea', }, + { + accessorKey: 'status', + header: () =>
Published
, + cell: ({ row }) => { + return ( + row.getCanExpand() && ( + ) => { + updateStatus(e.target.checked, row.original); + }} + checked={checkedStatus(row.original)} + disabled={!isOwner} + > + ) + ); + }, + id: 'status', + }, ]; const getRows = () => { @@ -279,6 +351,7 @@ export const TanStackTable = ({ variogramModel: e.family ? e.family : '', quality: roundResultString(e.quality), identifier: e.identifier, + status: e.status, }; rowArray.push(element); }); diff --git a/src/hooks/useMutateResults.ts b/src/hooks/useMutateResults.ts index 478d3de7..565a55f3 100644 --- a/src/hooks/useMutateResults.ts +++ b/src/hooks/useMutateResults.ts @@ -35,16 +35,16 @@ export const useMutateVariogramResult = () => { const putVariogramResult = useMutation({ mutationFn: ({ id, - variogramId, + computeCaseId, requestBody, }: { id: string; - variogramId: string; + computeCaseId: string; requestBody: UpdateVariogramResultCommandBody; }) => { - return ResultsService.putApiAnalogueModelsResultsVariogram( + return ResultsService.putApiAnalogueModelsComputecasesResults( id, - variogramId, + computeCaseId, requestBody, ); },