frontend/src/providers/pond/fieldAPI.tsx
2025-08-22 09:39:20 -06:00

143 lines
4.1 KiB
TypeScript

import { AxiosResponse } from "axios";
import { useHTTP } from "hooks";
import { pond } from "protobuf-ts/pond";
import { useGlobalState } from "providers";
import React, { createContext, PropsWithChildren, useContext } from "react";
import { or } from "utils";
import { pondURL } from "./pond";
export interface IFieldAPIContext {
addField: (field: pond.FieldSettings, otherTeam?: string) => Promise<any>;
getField: (fieldId: string, otherTeam?: string) => Promise<any>;
listFields: (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
otherTeam?: string,
asRoot?: boolean
) => Promise<AxiosResponse<pond.ListFieldsResponse>>;
fieldsPageData: (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
otherTeam?: string,
) => Promise<AxiosResponse<pond.ListFieldsPageDataResponse>>
removeField: (key: string, otherTeam?: string) => Promise<AxiosResponse<pond.RemoveFieldResponse>>;
updateField: (
key: string,
field: pond.FieldSettings,
asRoot?: true,
otherTeam?: string
) => Promise<AxiosResponse<pond.UpdateFieldResponse>>;
}
export const FieldAPIContext = createContext<IFieldAPIContext>({} as IFieldAPIContext);
interface Props {}
export default function FieldProvider(props: PropsWithChildren<Props>) {
const { children } = props;
const { get, del, post, put } = useHTTP();
const [{ as }] = useGlobalState();
const addField = (field: pond.FieldSettings, otherTeam?: string) => {
const view = otherTeam ? otherTeam : as
if (view) return post<pond.AddFieldResponse>(pondURL("/fields?as=" + view), field);
return post(pondURL("/fields"), field);
};
const getField = (fieldId: string, otherTeam?: string) => {
const view = otherTeam ? otherTeam : as
if (view) return get(pondURL("/field/" + fieldId + "?as=" + view));
return get(pondURL("/field/" + fieldId));
};
const removeField = (key: string, otherTeam?: string) => {
const view = otherTeam ? otherTeam : as
if (view) return del<pond.RemoveFieldResponse>(pondURL("/fields/" + key + "?as=" + view));
return del<pond.RemoveFieldResponse>(pondURL("/fields/" + key));
};
const listFields = (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
otherTeam?: string,
asRoot?: boolean
) => {
const view = otherTeam ? otherTeam : as
return get<pond.ListFieldsResponse>(
pondURL(
"/fields" +
"?limit=" +
limit +
"&offset=" +
offset +
("&order=" + or(order, "asc")) +
("&by=" + or(orderBy, "key")) +
(view ? "&as=" + view : "") +
(asRoot ? "&asRoot=" + asRoot.toString() : "") +
(search ? "&search=" + search : "")
)
);
};
const fieldsPageData = (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
otherTeam?: string,
) => {
const view = otherTeam ? otherTeam : as
return get<pond.ListFieldsPageDataResponse>(
pondURL(
"/fieldsPage" +
"?limit=" +
limit +
"&offset=" +
offset +
("&order=" + or(order, "asc")) +
("&by=" + or(orderBy, "key")) +
(view ? "&as=" + view : "") +
(search ? "&search=" + search : "")
)
);
}
const updateField = (key: string, field: pond.FieldSettings, asRoot?: boolean, otherTeam?: string) => {
const view = otherTeam ? otherTeam : as
if (view)
return put<pond.UpdateFieldResponse>(
pondURL("/fields/" + key + "?as=" + view + (asRoot ? "&asRoot=" + asRoot.toString() : "")),
field
);
return put<pond.UpdateFieldResponse>(
pondURL("/fields/" + key + (asRoot ? "?asRoot=" + asRoot.toString() : "")),
field
);
};
return (
<FieldAPIContext.Provider
value={{
addField,
getField,
listFields,
removeField,
updateField,
fieldsPageData
}}>
{children}
</FieldAPIContext.Provider>
);
}
export const useFieldAPI = () => useContext(FieldAPIContext);