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 IJohnDeereProxyAPIContext { //add new organization addAccount: ( teamKey: string, userID: string, jdCode: string, jdUserName: 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>; //update organizations listFields: ( limit: number, offset: number, // order?: "asc" | "desc", // orderBy?: string, // search?: string, as?: string, asRoot?: boolean ) => Promise>; } export const JohnDeereProxyAPIContext = createContext( {} as IJohnDeereProxyAPIContext ); interface Props {} export default function JohnDeereProvider(props: PropsWithChildren) { const { children } = props; const { post, get, put } = useHTTP(); const addAccount = (teamKey: string, userID: string, jdCode: string, jdUserName: string) => { return post( pondURL( "/jdAccounts?team=" + teamKey + "&user=" + userID + "&code=" + jdCode + "&jdUserName=" + jdUserName ) ); }; const listAccounts = ( limit: number, offset: number, as?: string, keys?: string[], types?: string[] ) => { return get( pondURL( "/jdAccounts?limit=" + limit + "&offset=" + offset + (as ? "&as=" + as : "") + (keys ? "&keys=" + keys.join(",") : "") + (types ? "&types=" + types.join(",") : "") ) ); }; const updateAccount = (key: string, options: pond.DataOption[], as?: string) => { return put( pondURL("/jdAccounts/" + key + "?options=" + options.toString() + (as ? "&as=" + as : "")) ); }; const listFields = ( limit: number, offset: number, // order?: "asc" | "desc", // orderBy?: string, // search?: string, as?: string, asRoot?: boolean ) => { return get( pondURL( "/jdFields" + "?limit=" + limit + "&offset=" + offset + (as ? "&as=" + as : "") + (asRoot ? "&asRoot=" + asRoot.toString() : "") ) ); }; return ( {children} ); } export const useJohnDeereProxyAPI = () => useContext(JohnDeereProxyAPIContext);