Skip to content

Commit

Permalink
Merge pull request #100 from bas-kirill/feature/openapi-edit-instrument
Browse files Browse the repository at this point in the history
feat(edit-instrument): use openapi for server side
  • Loading branch information
bas-kirill authored Aug 22, 2024
2 parents fa13361 + bc8edf6 commit 4eb820b
Show file tree
Hide file tree
Showing 19 changed files with 582 additions and 51 deletions.
2 changes: 2 additions & 0 deletions client/src/generated/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ api/add-favorite-api.ts
api/basic-login-api.ts
api/create-instrument-api.ts
api/delete-instrument-by-id-api.ts
api/edit-instrument-api.ts
api/get-countries-api.ts
api/get-instrument-basic-materials-api.ts
api/get-instrument-by-id-api.ts
Expand All @@ -25,6 +26,7 @@ model/add-favorite-request-body.ts
model/client-error.ts
model/country.ts
model/create-instrument-request-body.ts
model/edit-instrument-request-body.ts
model/get-countries-response.ts
model/get-instrument-basic-materials-response.ts
model/get-instrument-by-criteria-page-response.ts
Expand Down
1 change: 1 addition & 0 deletions client/src/generated/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from "./api/add-favorite-api";
export * from "./api/basic-login-api";
export * from "./api/create-instrument-api";
export * from "./api/delete-instrument-by-id-api";
export * from "./api/edit-instrument-api";
export * from "./api/get-countries-api";
export * from "./api/get-instrument-basic-materials-api";
export * from "./api/get-instrument-by-id-api";
Expand Down
205 changes: 205 additions & 0 deletions client/src/generated/api/edit-instrument-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/* tslint:disable */
/* eslint-disable */
/**
* Client Error
* Client Error
*
* The version of the OpenAPI document: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import type { Configuration } from "../configuration";
import type { AxiosPromise, AxiosInstance, RawAxiosRequestConfig } from "axios";
import globalAxios from "axios";
// Some imports not used depending on template conditions
// @ts-ignore
import {
DUMMY_BASE_URL,
assertParamExists,
setApiKeyToObject,
setBasicAuthToObject,
setBearerAuthToObject,
setOAuthToObject,
setSearchParams,
serializeDataIfNeeded,
toPathString,
createRequestFunction,
} from "../common";
// @ts-ignore
import {
BASE_PATH,
COLLECTION_FORMATS,
type RequestArgs,
BaseAPI,
RequiredError,
operationServerMap,
} from "../base";
// @ts-ignore
import type { ClientError } from "../model";
// @ts-ignore
import type { EditInstrumentRequestBody } from "../model";
// @ts-ignore
import type { ServerError } from "../model";
/**
* EditInstrumentApi - axios parameter creator
* @export
*/
export const EditInstrumentApiAxiosParamCreator = function (
configuration?: Configuration,
) {
return {
/**
* Edit Instrument
* @summary Edit Instrument
* @param {EditInstrumentRequestBody} editInstrumentRequestBody
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
editInstrument: async (
editInstrumentRequestBody: EditInstrumentRequestBody,
options: RawAxiosRequestConfig = {},
): Promise<RequestArgs> => {
// verify required parameter 'editInstrumentRequestBody' is not null or undefined
assertParamExists(
"editInstrument",
"editInstrumentRequestBody",
editInstrumentRequestBody,
);
const localVarPath = `/instrument/edit`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = {
method: "POST",
...baseOptions,
...options,
};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

localVarHeaderParameter["Content-Type"] = "application/json";

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions =
baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {
...localVarHeaderParameter,
...headersFromBaseOptions,
...options.headers,
};
localVarRequestOptions.data = serializeDataIfNeeded(
editInstrumentRequestBody,
localVarRequestOptions,
configuration,
);

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
};
};

/**
* EditInstrumentApi - functional programming interface
* @export
*/
export const EditInstrumentApiFp = function (configuration?: Configuration) {
const localVarAxiosParamCreator =
EditInstrumentApiAxiosParamCreator(configuration);
return {
/**
* Edit Instrument
* @summary Edit Instrument
* @param {EditInstrumentRequestBody} editInstrumentRequestBody
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async editInstrument(
editInstrumentRequestBody: EditInstrumentRequestBody,
options?: RawAxiosRequestConfig,
): Promise<
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<object>
> {
const localVarAxiosArgs = await localVarAxiosParamCreator.editInstrument(
editInstrumentRequestBody,
options,
);
const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
const localVarOperationServerBasePath =
operationServerMap["EditInstrumentApi.editInstrument"]?.[
localVarOperationServerIndex
]?.url;
return (axios, basePath) =>
createRequestFunction(
localVarAxiosArgs,
globalAxios,
BASE_PATH,
configuration,
)(axios, localVarOperationServerBasePath || basePath);
},
};
};

/**
* EditInstrumentApi - factory interface
* @export
*/
export const EditInstrumentApiFactory = function (
configuration?: Configuration,
basePath?: string,
axios?: AxiosInstance,
) {
const localVarFp = EditInstrumentApiFp(configuration);
return {
/**
* Edit Instrument
* @summary Edit Instrument
* @param {EditInstrumentRequestBody} editInstrumentRequestBody
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
editInstrument(
editInstrumentRequestBody: EditInstrumentRequestBody,
options?: RawAxiosRequestConfig,
): AxiosPromise<object> {
return localVarFp
.editInstrument(editInstrumentRequestBody, options)
.then((request) => request(axios, basePath));
},
};
};

/**
* EditInstrumentApi - object-oriented interface
* @export
* @class EditInstrumentApi
* @extends {BaseAPI}
*/
export class EditInstrumentApi extends BaseAPI {
/**
* Edit Instrument
* @summary Edit Instrument
* @param {EditInstrumentRequestBody} editInstrumentRequestBody
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof EditInstrumentApi
*/
public editInstrument(
editInstrumentRequestBody: EditInstrumentRequestBody,
options?: RawAxiosRequestConfig,
) {
return EditInstrumentApiFp(this.configuration)
.editInstrument(editInstrumentRequestBody, options)
.then((request) => request(this.axios, this.basePath));
}
}
69 changes: 69 additions & 0 deletions client/src/generated/model/edit-instrument-request-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* tslint:disable */
/* eslint-disable */
/**
* Client Error
* Client Error
*
* The version of the OpenAPI document: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

/**
*
* @export
* @interface EditInstrumentRequestBody
*/
export interface EditInstrumentRequestBody {
/**
*
* @type {number}
* @memberof EditInstrumentRequestBody
*/
instrument_id?: number;
/**
*
* @type {string}
* @memberof EditInstrumentRequestBody
*/
instrument_name: string;
/**
*
* @type {string}
* @memberof EditInstrumentRequestBody
*/
instrument_type: string;
/**
*
* @type {string}
* @memberof EditInstrumentRequestBody
*/
manufacturer_name: string;
/**
*
* @type {string}
* @memberof EditInstrumentRequestBody
*/
manufacturer_date: string;
/**
*
* @type {string}
* @memberof EditInstrumentRequestBody
*/
release_date: string;
/**
*
* @type {string}
* @memberof EditInstrumentRequestBody
*/
country: string;
/**
*
* @type {Array<string>}
* @memberof EditInstrumentRequestBody
*/
materials: Array<string>;
}
1 change: 1 addition & 0 deletions client/src/generated/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./add-favorite-request-body";
export * from "./client-error";
export * from "./country";
export * from "./create-instrument-request-body";
export * from "./edit-instrument-request-body";
export * from "./get-countries-response";
export * from "./get-instrument-basic-materials-response";
export * from "./get-instrument-by-criteria-page-response";
Expand Down
32 changes: 16 additions & 16 deletions client/src/pages/edit-instrument/api/action.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ActionFunction } from "react-router-dom";
import axios from "axios";
import { SERVER_URL } from "shared/config";
import Jwt from "domain/model/jwt";
import { parseInstrumentDetails } from "shared/model/parseInstrumentDetails";
import { EditInstrumentApi } from "generated/api/edit-instrument-api";

export interface EditInstrumentAction {
errors: string[] | null;
errors: string[];
}

const editInstrument = new EditInstrumentApi();

export const action: ActionFunction = async ({
request,
}): Promise<EditInstrumentAction> => {
Expand All @@ -29,28 +30,27 @@ export const action: ActionFunction = async ({
};
}

const { status } = await axios.post(
`${SERVER_URL}/api/instrument/edit`,
const response = await editInstrument.editInstrument(
{
instrumentId: instrumentId.toNumberValue(),
instrumentName: instrumentName,
instrumentType: instrumentType,
manufacturerName: manufacturerName,
manufactureDate: manufactureDate,
releaseDate: releaseDate,
country: country,
materials: materials,
instrument_id: instrumentId.toNumberValue(),
instrument_name: instrumentName,
instrument_type: instrumentType.instrument_type,
manufacturer_name: manufacturerName,
manufacturer_date: manufactureDate,
release_date: releaseDate,
country: country.country,
materials: materials.map((material) => material.basic_material),
},
{
headers: {
Authorization: `Bearer ${Jwt.extractFromLocalStorage()?.toStringValue()}`,
},
}, // https://stackoverflow.com/questions/39153080/how-can-i-get-the-status-code-from-an-http-error-in-axios
},
);

if (status === 200) {
if (response.status === 200) {
return {
errors: null,
errors: [],
};
}

Expand Down
11 changes: 7 additions & 4 deletions client/src/pages/edit-instrument/ui/EditInstrument.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ export const EditInstrument = () => {
defaultValue={loader.instrumentForEdit.name}
required
>
{loader.manufacturers.map((manufacturerName) => (
<option key={manufacturerName} value={manufacturerName}>
{manufacturerName}
{loader.manufacturers.map((manufacturer) => (
<option
key={manufacturer.manufacturer}
value={manufacturer.manufacturer}
>
{manufacturer.manufacturer}
</option>
))}
</select>
Expand Down Expand Up @@ -149,7 +152,7 @@ export const EditInstrument = () => {

<input type="submit" value="Edit" />
</Form>
{actionData?.errors &&
{actionData.errors.length > 0 &&
actionData?.errors.map((error) => <div key={error}>{error}</div>)}
<Footer />
</>
Expand Down
3 changes: 0 additions & 3 deletions client/src/shared/config/backend.ts

This file was deleted.

Loading

0 comments on commit 4eb820b

Please sign in to comment.