import { AxiosResponse } from "axios"; import { useHTTP } from "../http"; import { pond } from "protobuf-ts/pond"; import { createContext, PropsWithChildren, useContext } from "react"; //import { or } from "utils"; import { pondURL } from "./pond"; export interface ICNHiProxyAPIContext { //add new organization addAccount: ( teamKey: string, userID: string, cnhiCode: string, cnhiUsername: string ) => Promise>; //list organizations listAccounts: ( limit: number, offset: number, as?: string, keys?: string[], types?: string[] ) => Promise>; updateAccount: ( key: string, options: pond.DataOption[], as?: string ) => Promise>; listFields: ( limit: number, offset: number, // order?: "asc" | "desc", // orderBy?: string, // search?: string, as?: string, asRoot?: boolean ) => Promise>; } export const CNHiProxyAPIContext = createContext({} as ICNHiProxyAPIContext); interface Props {} export default function CNHiProvider(props: PropsWithChildren) { const { children } = props; const { post, get, put } = useHTTP(); const addAccount = (teamKey: string, userID: string, cnhiCode: string, cnhiUsername: string) => { return post( pondURL( "/cnhiAccounts?team=" + teamKey + "&user=" + userID + "&code=" + cnhiCode + "&cnhiUsername=" + cnhiUsername ) ); }; const listAccounts = ( limit: number, offset: number, as?: string, keys?: string[], types?: string[] ) => { return get( pondURL( "/cnhiAccounts?limit=" + limit + "&offset=" + offset + (as ? "&as=" + as : "") + (keys ? "&keys=" + keys.join(",") : "") + (types ? "&types=" + types.join(",") : "") ) ); }; const listFields = ( limit: number, offset: number, // order?: "asc" | "desc", // orderBy?: string, // search?: string, as?: string, asRoot?: boolean ) => { return get( pondURL( "/cnhiFields" + "?limit=" + limit + "&offset=" + offset + (as ? "&as=" + as : "") + (asRoot ? "&asRoot=" + asRoot.toString() : "") ) ); }; const updateAccount = (key: string, options: pond.DataOption[], as?: string) => { return put( pondURL("/cnhiAccounts/" + key + "?options=" + options.toString() + (as ? "&as=" + as : "")) ); }; return ( {children} ); } export const useCNHiProxyAPI = () => useContext(CNHiProxyAPIContext);