mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-24 15:49:42 +02:00
feat(frontend): ✨ Create CRUD User Interface for Units and Foods
This commit is contained in:
parent
122d35ec09
commit
a1aad078da
13 changed files with 517 additions and 42 deletions
|
@ -10,39 +10,16 @@ export interface CrudAPIInterface {
|
|||
// Methods
|
||||
}
|
||||
|
||||
export const crudMixins = <T>(
|
||||
requests: ApiRequestInstance,
|
||||
baseRoute: string,
|
||||
itemRoute: (itemId: string) => string
|
||||
) => {
|
||||
async function getAll(start = 0, limit = 9999) {
|
||||
return await requests.get<T[]>(baseRoute, {
|
||||
params: { start, limit },
|
||||
});
|
||||
}
|
||||
export interface CrudAPIMethodsInterface {
|
||||
// CRUD Methods
|
||||
getAll(): any
|
||||
createOne(): any
|
||||
getOne(): any
|
||||
updateOne(): any
|
||||
patchOne(): any
|
||||
deleteOne(): any
|
||||
}
|
||||
|
||||
async function createOne(payload: T) {
|
||||
return await requests.post<T>(baseRoute, payload);
|
||||
}
|
||||
|
||||
async function getOne(itemId: string) {
|
||||
return await requests.get<T>(itemRoute(itemId));
|
||||
}
|
||||
|
||||
async function updateOne(itemId: string, payload: T) {
|
||||
return await requests.put<T>(itemRoute(itemId), payload);
|
||||
}
|
||||
|
||||
async function patchOne(itemId: string, payload: T) {
|
||||
return await requests.patch(itemRoute(itemId), payload);
|
||||
}
|
||||
|
||||
async function deleteOne(itemId: string) {
|
||||
return await requests.delete<T>(itemRoute(itemId));
|
||||
}
|
||||
|
||||
return { getAll, getOne, updateOne, patchOne, deleteOne, createOne };
|
||||
};
|
||||
|
||||
export abstract class BaseAPI {
|
||||
requests: ApiRequestInstance;
|
||||
|
@ -66,11 +43,11 @@ export abstract class BaseCRUDAPI<T, U> extends BaseAPI implements CrudAPIInterf
|
|||
return await this.requests.post<T>(this.baseRoute, payload);
|
||||
}
|
||||
|
||||
async getOne(itemId: string) {
|
||||
async getOne(itemId: string | number) {
|
||||
return await this.requests.get<T>(this.itemRoute(itemId));
|
||||
}
|
||||
|
||||
async updateOne(itemId: string, payload: T) {
|
||||
async updateOne(itemId: string | number, payload: T) {
|
||||
return await this.requests.put<T>(this.itemRoute(itemId), payload);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import { requests } from "../requests";
|
||||
import { BaseCRUDAPI } from "./_base";
|
||||
|
||||
export type EventCategory = "general" | "recipe" | "backup" | "scheduled" | "migration" | "group" | "user";
|
||||
|
@ -36,7 +35,7 @@ export class NotificationsAPI extends BaseCRUDAPI<EventNotification, CreateEvent
|
|||
itemRoute = routes.aboutEventsNotificationsId;
|
||||
/** Returns the Group Data for the Current User
|
||||
*/
|
||||
async testNotification(id: number) {
|
||||
return await requests.post(routes.aboutEventsNotificationsTest, { id });
|
||||
async testNotification(id: number | null = null, testUrl: string | null = null) {
|
||||
return await this.requests.post(routes.aboutEventsNotificationsTest, { id, testUrl });
|
||||
}
|
||||
}
|
||||
|
|
22
frontend/api/class-interfaces/recipe-foods.ts
Normal file
22
frontend/api/class-interfaces/recipe-foods.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { BaseCRUDAPI } from "./_base";
|
||||
|
||||
const prefix = "/api";
|
||||
|
||||
export interface CreateFood {
|
||||
name: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export interface Food extends CreateFood {
|
||||
id: number;
|
||||
}
|
||||
|
||||
const routes = {
|
||||
food: `${prefix}/foods`,
|
||||
foodsFood: (tag: string) => `${prefix}/foods/${tag}`,
|
||||
};
|
||||
|
||||
export class FoodAPI extends BaseCRUDAPI<Food, CreateFood> {
|
||||
baseRoute: string = routes.food;
|
||||
itemRoute = routes.foodsFood;
|
||||
}
|
23
frontend/api/class-interfaces/recipe-units.ts
Normal file
23
frontend/api/class-interfaces/recipe-units.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { BaseCRUDAPI } from "./_base";
|
||||
|
||||
const prefix = "/api";
|
||||
|
||||
export interface CreateUnit {
|
||||
name: string;
|
||||
abbreviation: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export interface Unit extends CreateUnit {
|
||||
id: number;
|
||||
}
|
||||
|
||||
const routes = {
|
||||
unit: `${prefix}/units`,
|
||||
unitsUnit: (tag: string) => `${prefix}/units/${tag}`,
|
||||
};
|
||||
|
||||
export class UnitAPI extends BaseCRUDAPI<Unit, CreateUnit> {
|
||||
baseRoute: string = routes.unit;
|
||||
itemRoute = routes.unitsUnit;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue