Refaktor, rozdělení api, zpřehlednění kódu
This commit is contained in:
57
client/src/api/Api.ts
Normal file
57
client/src/api/Api.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { toast } from "react-toastify";
|
||||
import { getBaseUrl, getToken } from "../Utils";
|
||||
|
||||
/**
|
||||
* Wrapper pro volání API, u kterých chceme automaticky zobrazit toaster s chybou ze serveru.
|
||||
*
|
||||
* @param apiFunction volaná API funkce
|
||||
*/
|
||||
export function errorHandler<T>(apiFunction: () => Promise<T>): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
apiFunction().then((result) => {
|
||||
resolve(result);
|
||||
}).catch(e => {
|
||||
toast.error(e.message, { theme: "colored" });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function request<TResponse>(
|
||||
url: string,
|
||||
config: RequestInit = {}
|
||||
): Promise<TResponse> {
|
||||
config.headers = config?.headers ? new Headers(config.headers) : new Headers();
|
||||
config.headers.set("Authorization", `Bearer ${getToken()}`);
|
||||
try {
|
||||
const response = await fetch(getBaseUrl() + url, config);
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
// Vyhodíme samotnou hlášku z odpovědi, odchytí si jí errorHandler
|
||||
throw new Error(json.error);
|
||||
}
|
||||
return response.json() as TResponse;
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get: <TResponse>(url: string) => request<TResponse>(url),
|
||||
post: <TBody extends BodyInit, TResponse>(url: string, body: TBody) => request<TResponse>(url, { method: 'POST', body, headers: { 'Content-Type': 'application/json' } }),
|
||||
}
|
||||
|
||||
export const getQrUrl = (login: string) => {
|
||||
return `${getBaseUrl()}/api/qr?login=${login}`;
|
||||
}
|
||||
|
||||
export const getData = async (dayIndex?: number) => {
|
||||
let url = '/api/data';
|
||||
if (dayIndex != null) {
|
||||
url += '?dayIndex=' + dayIndex;
|
||||
}
|
||||
return await api.get<any>(url);
|
||||
}
|
||||
|
||||
export const login = async (login?: string) => {
|
||||
return await api.post<any, any>('/api/login', JSON.stringify({ login }));
|
||||
}
|
||||
19
client/src/api/FoodApi.ts
Normal file
19
client/src/api/FoodApi.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { api } from "./Api";
|
||||
|
||||
const FOOD_API_PREFIX = '/api/food';
|
||||
|
||||
export const addChoice = async (locationIndex: number, foodIndex?: number, dayIndex?: number) => {
|
||||
return await api.post<any, any>(`${FOOD_API_PREFIX}/addChoice`, JSON.stringify({ locationIndex, foodIndex, dayIndex }));
|
||||
}
|
||||
|
||||
export const removeChoices = async (locationIndex: number, dayIndex?: number) => {
|
||||
return await api.post<any, any>(`${FOOD_API_PREFIX}/removeChoices`, JSON.stringify({ locationIndex, dayIndex }));
|
||||
}
|
||||
|
||||
export const removeChoice = async (locationIndex: number, foodIndex: number, dayIndex?: number) => {
|
||||
return await api.post<any, any>(`${FOOD_API_PREFIX}/removeChoice`, JSON.stringify({ locationIndex, foodIndex, dayIndex }));
|
||||
}
|
||||
|
||||
export const changeDepartureTime = async (time: string, dayIndex?: number) => {
|
||||
return await api.post<any, any>(`${FOOD_API_PREFIX}/changeDepartureTime`, JSON.stringify({ time, dayIndex }));
|
||||
}
|
||||
44
client/src/api/PizzaDayApi.ts
Normal file
44
client/src/api/PizzaDayApi.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { PizzaOrder } from "../types";
|
||||
import { api } from "./Api";
|
||||
|
||||
const PIZZADAY_API_PREFIX = '/api/pizzaDay';
|
||||
|
||||
export const createPizzaDay = async () => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/create`, undefined);
|
||||
}
|
||||
|
||||
export const deletePizzaDay = async () => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/delete`, undefined);
|
||||
}
|
||||
|
||||
export const lockPizzaDay = async () => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/lock`, undefined);
|
||||
}
|
||||
|
||||
export const unlockPizzaDay = async () => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/unlock`, undefined);
|
||||
}
|
||||
|
||||
export const finishOrder = async () => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/finishOrder`, undefined);
|
||||
}
|
||||
|
||||
export const finishDelivery = async (bankAccount?: string, bankAccountHolder?: string) => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/finishDelivery`, JSON.stringify({ bankAccount, bankAccountHolder }));
|
||||
}
|
||||
|
||||
export const addPizza = async (pizzaIndex: number, pizzaSizeIndex: number) => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/add`, JSON.stringify({ pizzaIndex, pizzaSizeIndex }));
|
||||
}
|
||||
|
||||
export const removePizza = async (pizzaOrder: PizzaOrder) => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/remove`, JSON.stringify({ pizzaOrder }));
|
||||
}
|
||||
|
||||
export const updatePizzaDayNote = async (note?: string) => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/updatePizzaDayNote`, JSON.stringify({ note }));
|
||||
}
|
||||
|
||||
export const updatePizzaFee = async (login: string, text?: string, price?: number) => {
|
||||
return await api.post<any, any>(`${PIZZADAY_API_PREFIX}/updatePizzaFee`, JSON.stringify({ login, text, price }));
|
||||
}
|
||||
12
client/src/api/VotingApi.ts
Normal file
12
client/src/api/VotingApi.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { FeatureRequest } from "../types";
|
||||
import { api } from "./Api";
|
||||
|
||||
const VOTING_API_PREFIX = '/api/voting';
|
||||
|
||||
export const getFeatureVotes = async () => {
|
||||
return await api.get<any>(`${VOTING_API_PREFIX}/getVotes`);
|
||||
}
|
||||
|
||||
export const updateFeatureVote = async (option: FeatureRequest, active: boolean) => {
|
||||
return await api.post<any, any>(`${VOTING_API_PREFIX}/updateVote`, JSON.stringify({ option, active }));
|
||||
}
|
||||
Reference in New Issue
Block a user