import { AxiosResponse } from "axios"; import { useHTTP } from "../http"; import { pond } from "protobuf-ts/pond"; import { useGlobalState } from "../StateContainer"; import React, { createContext, PropsWithChildren, useContext } from "react"; import { or } from "utils"; import { pondURL } from "./pond"; export interface IObjectHeaterAPIContext { addObjectHeater: ( name: string, heater: pond.ObjectHeaterSettings, otherTeam?: string ) => Promise>; getObjectHeater: (key: string, otherTeam?: string) => Promise>; listObjectHeaters: ( limit: number, offset: number, order?: "asc" | "desc", orderBy?: string, search?: string, asRoot?: boolean, otherTeam?: string, keys?: string[], types?: string[], numerical?: boolean ) => Promise>; removeObjectHeater: (key: string, otherTeam?: string) => Promise>; updateObjectHeater: ( key: string, name: string, settings: pond.ObjectHeaterSettings, otherTeam?: string ) => Promise>; updateLink: ( parentKey: string, parentType: string, objectID: string, objectType: string, permissions: string[], otherTeam?: string ) => Promise; listObjectHeatersPageData: ( limit: number, offset: number, order?: "asc" | "desc", orderBy?: string, search?: string, otherTeam?: string, asRoot?: boolean ) => Promise>; } export const ObjectHeaterAPIContext = createContext( {} as IObjectHeaterAPIContext ); interface Props {} export default function ObjectHeaterProvider(props: PropsWithChildren) { const { children } = props; const { get, del, post, put } = useHTTP(); const [{ as }] = useGlobalState(); const addObjectHeater = (name: string, settings: pond.ObjectHeaterSettings, otherTeam?: string) => { const view = otherTeam ? otherTeam : as if (view) return post( pondURL("/objectHeaters?name=" + name + "&as=" + view), settings ); return post(pondURL("/objectHeaters?name=" + name), settings); }; const getObjectHeater = (key: string, otherTeam?: string) => { const view = otherTeam ? otherTeam : as if (view) { return get(pondURL("/objectHeaters/" + key + "?as=" + view)); } return get(pondURL("/objectHeaters/" + key)); }; const removeObjectHeater = (key: string, otherTeam?: string) => { const view = otherTeam ? otherTeam : as if (view) return del(pondURL("/objectHeaters/" + key + "?as=" + view)); return del(pondURL("/objectHeaters/" + key)); }; const listObjectHeaters = ( limit: number, offset: number, order?: "asc" | "desc", orderBy?: string, search?: string, asRoot?: boolean, otherTeam?: string, keys?: string[], types?: string[], numerical?: boolean ) => { const view = otherTeam ? otherTeam : as return get( pondURL( "/objectHeaters" + "?limit=" + limit + "&offset=" + offset + ("&order=" + (order ? order : "asc")) + ("&by=" + (orderBy ? orderBy : "key")) + (search ? "&search=" + search : "") + (numerical ? "&numerical=true" : "") + (asRoot ? "&asRoot=" + asRoot.toString() : "") + (view ? "&as=" + view : "") + (keys ? "&keys=" + keys.toString() : "") + (types ? "&types=" + types.toString() : "") ) ); }; const updateObjectHeater = (key: string, name: string, settings: pond.ObjectHeaterSettings, otherTeam?: string) => { const view = otherTeam ? otherTeam : as if (view) { return put( pondURL("/objectHeaters/" + key + "?as=" + view + "&name=" + name), settings ); } return put( pondURL("/objectHeaters/" + key + "?name=" + name), settings ); }; const updateLink = ( parentID: string, parentType: string, objectID: string, objectType: string, permissions: string[], otherTeam?: string ) => { const view = otherTeam ? otherTeam : as if (view) return post(pondURL(`/objectHeaters/` + parentID + `/link?as=${view}`), { Key: objectID, Type: objectType, Parent: parentID, ParentType: parentType, Permissions: permissions }); return post(pondURL(`/objectHeaters/` + parentID + `/link`), { Key: objectID, Type: objectType, Parent: parentID, ParentType: parentType, Permissions: permissions }); }; const listObjectHeatersPageData = ( limit: number, offset: number, order?: "asc" | "desc", orderBy?: string, search?: string, otherTeam?: string, asRoot?: boolean ) => { const view = otherTeam ? otherTeam : as return get( pondURL( "/objectHeatersWithData" + "?limit=" + limit + "&offset=" + offset + ("&order=" + or(order, "asc")) + ("&by=" + or(orderBy, "key")) + (view ? "&as=" + view : "") + (asRoot ? "&asRoot=" + asRoot.toString() : "") + (search ? "&search=" + search : "") ) ); }; return ( {children} ); } export const useObjectHeaterAPI = () => useContext(ObjectHeaterAPIContext);