front #8

Merged
mfnefd merged 14 commits from front into dev 2024-12-25 23:49:21 +04:00
6 changed files with 2130 additions and 886 deletions
Showing only changes of commit 294bf384b4 - Show all commits

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,7 @@
"react-dropzone": "^14.3.5", "react-dropzone": "^14.3.5",
"react-router-dom": "^6.27.0", "react-router-dom": "^6.27.0",
"react-scripts": "^5.0.1", "react-scripts": "^5.0.1",
"swagger-typescript-api": "^13.0.23",
"typescript": "^4.9.5", "typescript": "^4.9.5",
"web-vitals": "^2.1.4" "web-vitals": "^2.1.4"
}, },
@ -25,7 +26,8 @@
"start": "react-scripts start", "start": "react-scripts start",
"build": "react-scripts build", "build": "react-scripts build",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject" "eject": "react-scripts eject",
"gen-api": "swagger-typescript-api -r -o ./src/core/api/ --modular -p "
}, },
"eslintConfig": { "eslintConfig": {
"extends": [ "extends": [

View File

@ -0,0 +1,297 @@
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/
import {
Farm,
FarmRequest,
Greenhouse,
GreenhouseInfo,
GreenhouseRequest,
LoginRequest,
RegisterRequest,
ValveRequest,
} from "./data-contracts";
import { ContentType, HttpClient, RequestParams } from "./http-client";
export class Api<SecurityDataType = unknown> extends HttpClient<SecurityDataType> {
/**
* No description
*
* @tags Auth
* @name AuthRegisterCreate
* @request POST:/api/Auth/register
* @secure
* @response `200` `void` Success
*/
authRegisterCreate = (data: RegisterRequest, params: RequestParams = {}) =>
this.request<void, any>({
path: `/api/Auth/register`,
method: "POST",
body: data,
secure: true,
type: ContentType.Json,
...params,
});
/**
* No description
*
* @tags Auth
* @name AuthLoginCreate
* @request POST:/api/Auth/login
* @secure
* @response `200` `void` Success
*/
authLoginCreate = (data: LoginRequest, params: RequestParams = {}) =>
this.request<void, any>({
path: `/api/Auth/login`,
method: "POST",
body: data,
secure: true,
type: ContentType.Json,
...params,
});
/**
* No description
*
* @tags Auth
* @name AuthUserList
* @request GET:/api/Auth/user
* @secure
* @response `200` `void` Success
*/
authUserList = (params: RequestParams = {}) =>
this.request<void, any>({
path: `/api/Auth/user`,
method: "GET",
secure: true,
...params,
});
/**
* No description
*
* @tags Farm
* @name UserFarmDetail
* @request GET:/api/user/{userId}/farm
* @secure
* @response `200` `(Farm)[]` Success
*/
userFarmDetail = (userId: number, params: RequestParams = {}) =>
this.request<Farm[], any>({
path: `/api/user/${userId}/farm`,
method: "GET",
secure: true,
format: "json",
...params,
});
/**
* No description
*
* @tags Farm
* @name UserFarmCreate
* @request POST:/api/user/{userId}/farm
* @secure
* @response `200` `Farm` Success
*/
userFarmCreate = (userId: number, data: FarmRequest, params: RequestParams = {}) =>
this.request<Farm, any>({
path: `/api/user/${userId}/farm`,
method: "POST",
body: data,
secure: true,
type: ContentType.Json,
format: "json",
...params,
});
/**
* No description
*
* @tags Farm
* @name UserFarmDetail2
* @request GET:/api/user/{userId}/farm/{farmId}
* @originalName userFarmDetail
* @duplicate
* @secure
* @response `200` `Farm` Success
*/
userFarmDetail2 = (userId: number, farmId: number, params: RequestParams = {}) =>
this.request<Farm, any>({
path: `/api/user/${userId}/farm/${farmId}`,
method: "GET",
secure: true,
format: "json",
...params,
});
/**
* No description
*
* @tags Farm
* @name UserFarmUpdate
* @request PUT:/api/user/{userId}/farm/{farmId}
* @secure
* @response `200` `Farm` Success
*/
userFarmUpdate = (userId: number, farmId: number, data: FarmRequest, params: RequestParams = {}) =>
this.request<Farm, any>({
path: `/api/user/${userId}/farm/${farmId}`,
method: "PUT",
body: data,
secure: true,
type: ContentType.Json,
format: "json",
...params,
});
/**
* No description
*
* @tags Farm
* @name UserFarmDelete
* @request DELETE:/api/user/{userId}/farm/{farmId}
* @secure
* @response `200` `void` Success
*/
userFarmDelete = (userId: number, farmId: number, params: RequestParams = {}) =>
this.request<void, any>({
path: `/api/user/${userId}/farm/${farmId}`,
method: "DELETE",
secure: true,
...params,
});
/**
* No description
*
* @tags Greenhouse
* @name FarmGreenhouseDetail
* @request GET:/api/farm/{farmId}/greenhouse
* @secure
* @response `200` `(GreenhouseInfo)[]` Success
*/
farmGreenhouseDetail = (farmId: number, params: RequestParams = {}) =>
this.request<GreenhouseInfo[], any>({
path: `/api/farm/${farmId}/greenhouse`,
method: "GET",
secure: true,
format: "json",
...params,
});
/**
* No description
*
* @tags Greenhouse
* @name FarmGreenhouseCreate
* @request POST:/api/farm/{farmId}/greenhouse
* @secure
* @response `200` `Greenhouse` Success
*/
farmGreenhouseCreate = (farmId: number, data: GreenhouseRequest, params: RequestParams = {}) =>
this.request<Greenhouse, any>({
path: `/api/farm/${farmId}/greenhouse`,
method: "POST",
body: data,
secure: true,
type: ContentType.Json,
format: "json",
...params,
});
/**
* No description
*
* @tags Greenhouse
* @name FarmGreenhouseDetail2
* @request GET:/api/farm/{farmId}/greenhouse/{greenhouseId}
* @originalName farmGreenhouseDetail
* @duplicate
* @secure
* @response `200` `GreenhouseInfo` Success
*/
farmGreenhouseDetail2 = (farmId: number, greenhouseId: number, params: RequestParams = {}) =>
this.request<GreenhouseInfo, any>({
path: `/api/farm/${farmId}/greenhouse/${greenhouseId}`,
method: "GET",
secure: true,
format: "json",
...params,
});
/**
* No description
*
* @tags Greenhouse
* @name FarmGreenhouseDelete
* @request DELETE:/api/farm/{farmId}/greenhouse/{greenhouseId}
* @secure
* @response `200` `void` Success
*/
farmGreenhouseDelete = (farmId: number, greenhouseId: number, params: RequestParams = {}) =>
this.request<void, any>({
path: `/api/farm/${farmId}/greenhouse/${greenhouseId}`,
method: "DELETE",
secure: true,
...params,
});
/**
* No description
*
* @tags Greenhouse
* @name FarmGreenhouseSettingsDetail
* @request GET:/api/farm/{farmId}/greenhouse/{greenhouseId}/settings
* @secure
* @response `200` `Greenhouse` Success
*/
farmGreenhouseSettingsDetail = (farmId: number, greenhouseId: number, params: RequestParams = {}) =>
this.request<Greenhouse, any>({
path: `/api/farm/${farmId}/greenhouse/${greenhouseId}/settings`,
method: "GET",
secure: true,
format: "json",
...params,
});
/**
* No description
*
* @tags Greenhouse
* @name FarmGreenhouseSettingsUpdate
* @request PUT:/api/farm/{farmId}/greenhouse/{greenhouseId}/settings
* @secure
* @response `200` `Greenhouse` Success
*/
farmGreenhouseSettingsUpdate = (
farmId: number,
greenhouseId: number,
data: GreenhouseRequest,
params: RequestParams = {},
) =>
this.request<Greenhouse, any>({
path: `/api/farm/${farmId}/greenhouse/${greenhouseId}/settings`,
method: "PUT",
body: data,
secure: true,
type: ContentType.Json,
format: "json",
...params,
});
/**
* No description
*
* @tags Valve
* @name FarmGreenhouseWateringCreate
* @request POST:/api/farm/{farmId}/greenhouse/{ghId}/watering
* @secure
* @response `200` `void` Success
*/
farmGreenhouseWateringCreate = (farmId: number, ghId: number, data: ValveRequest, params: RequestParams = {}) =>
this.request<void, any>({
path: `/api/farm/${farmId}/greenhouse/${ghId}/watering`,
method: "POST",
body: data,
secure: true,
type: ContentType.Json,
...params,
});
}

View File

@ -0,0 +1,92 @@
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/
export interface Farm {
/** @format int32 */
id?: number;
name?: string | null;
/** @format int32 */
userId?: number;
user?: User;
raspberryIP?: string | null;
}
export interface FarmRequest {
name?: string | null;
raspberryIP?: string | null;
}
export interface Greenhouse {
/** @format int32 */
id?: number;
/** @format int32 */
recomendedTemperature?: number;
wateringMode?: WateringMode;
heatingMode?: HeatingMode;
/** @format int32 */
farmId?: number;
farm?: Farm;
}
export interface GreenhouseInfo {
/** @format int32 */
id?: number;
/** @format int32 */
percentWater?: number;
/** @format int32 */
soilTemperature?: number;
pumpStatus?: boolean;
heatingStatus?: boolean;
autoWateringStatus?: boolean;
}
export interface GreenhouseRequest {
/** @format int32 */
recomendedTemperature?: number;
wateringMode?: WateringMode;
heatingMode?: HeatingMode;
}
/** @format int32 */
export enum HeatingMode {
Value0 = 0,
Value1 = 1,
}
export interface LoginRequest {
email?: string | null;
password?: string | null;
}
export interface RegisterRequest {
name?: string | null;
email?: string | null;
password?: string | null;
}
export interface User {
/** @format int32 */
id?: number;
name?: string | null;
email?: string | null;
password?: string | null;
farms?: Farm[] | null;
}
export interface ValveRequest {
action?: string | null;
}
/** @format int32 */
export enum WateringMode {
Value0 = 0,
Value1 = 1,
}

View File

@ -0,0 +1,220 @@
/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/
export type QueryParamsType = Record<string | number, any>;
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
export interface FullRequestParams extends Omit<RequestInit, "body"> {
/** set parameter to `true` for call `securityWorker` for this request */
secure?: boolean;
/** request path */
path: string;
/** content type of request body */
type?: ContentType;
/** query params */
query?: QueryParamsType;
/** format of response (i.e. response.json() -> format: "json") */
format?: ResponseFormat;
/** request body */
body?: unknown;
/** base url */
baseUrl?: string;
/** request cancellation token */
cancelToken?: CancelToken;
}
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
export interface ApiConfig<SecurityDataType = unknown> {
baseUrl?: string;
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
customFetch?: typeof fetch;
}
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
data: D;
error: E;
}
type CancelToken = Symbol | string | number;
export enum ContentType {
Json = "application/json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
}
export class HttpClient<SecurityDataType = unknown> {
public baseUrl: string = "http://172.18.167.3:5124";
private securityData: SecurityDataType | null = null;
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
private abortControllers = new Map<CancelToken, AbortController>();
private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams);
private baseApiParams: RequestParams = {
credentials: "same-origin",
headers: {},
redirect: "follow",
referrerPolicy: "no-referrer",
};
constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
Object.assign(this, apiConfig);
}
public setSecurityData = (data: SecurityDataType | null) => {
this.securityData = data;
};
protected encodeQueryParam(key: string, value: any) {
const encodedKey = encodeURIComponent(key);
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
}
protected addQueryParam(query: QueryParamsType, key: string) {
return this.encodeQueryParam(key, query[key]);
}
protected addArrayQueryParam(query: QueryParamsType, key: string) {
const value = query[key];
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
}
protected toQueryString(rawQuery?: QueryParamsType): string {
const query = rawQuery || {};
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
return keys
.map((key) => (Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key)))
.join("&");
}
protected addQueryParams(rawQuery?: QueryParamsType): string {
const queryString = this.toQueryString(rawQuery);
return queryString ? `?${queryString}` : "";
}
private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (input: any) =>
input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.Text]: (input: any) => (input !== null && typeof input !== "string" ? JSON.stringify(input) : input),
[ContentType.FormData]: (input: any) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
key,
property instanceof Blob
? property
: typeof property === "object" && property !== null
? JSON.stringify(property)
: `${property}`,
);
return formData;
}, new FormData()),
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
};
protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
return {
...this.baseApiParams,
...params1,
...(params2 || {}),
headers: {
...(this.baseApiParams.headers || {}),
...(params1.headers || {}),
...((params2 && params2.headers) || {}),
},
};
}
protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
if (this.abortControllers.has(cancelToken)) {
const abortController = this.abortControllers.get(cancelToken);
if (abortController) {
return abortController.signal;
}
return void 0;
}
const abortController = new AbortController();
this.abortControllers.set(cancelToken, abortController);
return abortController.signal;
};
public abortRequest = (cancelToken: CancelToken) => {
const abortController = this.abortControllers.get(cancelToken);
if (abortController) {
abortController.abort();
this.abortControllers.delete(cancelToken);
}
};
public request = async <T = any, E = any>({
body,
secure,
path,
type,
query,
format,
baseUrl,
cancelToken,
...params
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
const secureParams =
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
this.securityWorker &&
(await this.securityWorker(this.securityData))) ||
{};
const requestParams = this.mergeRequestParams(params, secureParams);
const queryString = query && this.toQueryString(query);
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
const responseFormat = format || requestParams.format;
return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, {
...requestParams,
headers: {
...(requestParams.headers || {}),
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
},
signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null,
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
}).then(async (response) => {
const r = response.clone() as HttpResponse<T, E>;
r.data = null as unknown as T;
r.error = null as unknown as E;
const data = !responseFormat
? r
: await response[responseFormat]()
.then((data) => {
if (r.ok) {
r.data = data;
} else {
r.error = data;
}
return r;
})
.catch((e) => {
r.error = e;
return r;
});
if (cancelToken) {
this.abortControllers.delete(cancelToken);
}
if (!response.ok) throw data;
return data;
});
};
}

View File

@ -1,6 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "ES2022",
"lib": [ "lib": [
"dom", "dom",
"dom.iterable", "dom.iterable",