added team api

This commit is contained in:
Carter 2024-11-15 09:59:46 -06:00
parent 258b19583e
commit 17c559bdfc
6 changed files with 254 additions and 11 deletions

View file

@ -8,7 +8,6 @@ import UserWrapper from './UserWrapper'
import { Theme, ThemeProvider } from '@mui/material'
import { CreateTheme } from '../theme/theme'
import { getThemeType, setThemeType } from '../theme/themeType'
import { BrowserRouter } from 'react-router-dom'
function AuthHTTPWrapper() {
const [token, setToken] = useState<string | undefined>(undefined)
@ -43,11 +42,9 @@ function AuthHTTPWrapper() {
<ThemeProvider theme={palette}>
<AuthWrapper setToken={setToken}>
{ token ?
// <BrowserRouter>
<HTTPProvider token={token}>
<UserWrapper toggleTheme={toggleTheme}/>
</HTTPProvider>
// </BrowserRouter>
<HTTPProvider token={token}>
<UserWrapper toggleTheme={toggleTheme}/>
</HTTPProvider>
:
<LoadingScreen
message='Loading user profile'

View file

@ -1,4 +1,4 @@
// export {
export {
// useAuth,
// useBackpackAPI,
// useBilling,
@ -10,7 +10,7 @@
// useFirmwareAPI,
// useGitlab,
// useGroupAPI,
// useHTTP,
useHTTP,
// useInteractionsAPI,
// useMeasurementsWebsocket,
// useMetricAPI,
@ -22,7 +22,7 @@
// useTagAPI,
// useUsageAPI,
// useUserAPI
// } from "providers";
} from "providers";
// export * from "./useDebounce";
// export * from "./useForceUpdate";
// export * from "./useInterval";

60
src/models/Scope.ts Normal file
View file

@ -0,0 +1,60 @@
export interface Scope {
kind: string;
key: string;
}
export function newScope(kind: string, key: string): Scope {
return { kind, key };
}
export function componentScope(device: string, component: string): Scope {
return { kind: "component", key: device + ":" + component };
}
export function deviceScope(key: string): Scope {
return { kind: "device", key: key };
}
export function groupScope(key: string): Scope {
return { kind: "group", key: key };
}
export function binScope(key: string): Scope {
return { kind: "bin", key: key };
}
export function binYardScope(key: string): Scope {
return { kind: "binyard", key: key };
}
export function teamScope(key: string): Scope {
return { kind: "team", key: key };
}
export function userScope(key: string): Scope {
return { kind: "user", key: key };
}
export function noteScope(key: string): Scope {
return { kind: "note", key: key };
}
export function fieldScope(key: string): Scope {
return { kind: "field", key: key };
}
export function siteScope(key: string): Scope {
return { kind: "site", key: key };
}
export function taskScope(key: string): Scope {
return { kind: "task", key: key };
}
export function mineScope(key: string): Scope {
return { kind: "mine", key: key };
}
export function planScope(key: string): Scope {
return { kind: "harvestPlan", key: key };
}

View file

@ -6,7 +6,7 @@
// export * from "./Firmware";
// export * from "./Group";
// export * from "./Interaction";
// export * from "./Scope";
export * from "./Scope";
// export * from "./ShareableLink";
// export * from "./Site";
// export * from "./Tag";

49
src/providers/index.ts Normal file
View file

@ -0,0 +1,49 @@
// export { AuthContext, useAuth } from "./auth";
// export { BillingContext, useBilling } from "./billing";
// export { GitlabContext, useGitlab } from "./gitlab";
export { HTTPContext, useHTTP } from "./http";
export {
// useBackpackAPI,
// useBinAPI,
// useBinYardAPI,
// useNoteAPI,
// useComponentAPI,
// useComponentsWebsocket,
// useComponentWebsocket,
// useDeviceAPI,
// useDeviceWebsocket,
// useFieldAPI,
// useFieldMarkerAPI,
// useFirmwareAPI,
// useGroupAPI,
// useHarvestPlanAPI,
// useHarvestYearAPI,
// useHomeMarkerAPI,
// useInteractionsAPI,
// useMeasurementsWebsocket,
// useMetricAPI,
// usePermissionAPI,
// usePreferenceAPI,
// useSiteAPI,
// useTagAPI,
// useTeamAPI,
// useTaskAPI,
// useUsageAPI,
useUserAPI,
// useStripeAPI,
// useDataDogProxyAPI,
// useMineAPI,
// useKeyManagerAPI,
// useTerminalAPI,
// useGateAPI,
// useObjectHeaterAPI,
// useMutationAPI,
// useGrainBagAPI,
// useTransactionAPI,
// useFileControllerAPI
} from "./pond/pond";
// export { SecurityContext, useSecurity } from "./security";
// export { SnackbarContext, useSnackbar } from "./Snackbar";
export * from "./StateContainer";
// export { TagsContext, TagsProvider, useTags } from "./tags";
// export { UsersContext, UsersProvider, useUsers } from "./users";

View file

@ -0,0 +1,137 @@
import { AxiosResponse } from "axios";
import { Scope, Team } from "models";
import { useHTTP } from "hooks";
import { pond } from "protobuf-ts/pond";
import { createContext, PropsWithChildren, useContext } from "react";
import { or } from "utils/types";
import { pondURL } from "./pond";
export interface ITeamAPIContext {
addTeam: (team: pond.TeamSettings) => Promise<AxiosResponse<pond.AddTeamResponse>>;
getTeam: (teamID: string, scope?: Scope) => Promise<Team>;
updateTeam: (
key: string,
team: pond.TeamSettings,
asRoot?: boolean
) => Promise<AxiosResponse<pond.UpdateTeamResponse>>;
updatePreferences: (
key: string,
preferences: pond.TeamPreferences,
keys?: string[],
types?: string[]
) => Promise<any>;
listTeams: (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
asRoot?: boolean,
as?: string
) => Promise<AxiosResponse<pond.ListTeamsResponse>>;
listAllTeams: () => Promise<AxiosResponse<pond.ListTeamsResponse>>;
listObjectTeams: (scope: Scope, as?: string) => Promise<any>;
removeTeam: (key: string) => Promise<AxiosResponse<pond.RemoveTeamResponse>>;
}
export const TeamAPIContext = createContext<ITeamAPIContext>({} as ITeamAPIContext);
interface Props {}
export default function TeamProvider(props: PropsWithChildren<Props>) {
const { children } = props;
const { get, del, post, put } = useHTTP();
//const [{ team }] = useGlobalState();
const addTeam = (team: pond.TeamSettings) => {
return post<pond.AddTeamResponse>(pondURL("/team"), team);
};
const getTeam = (id: string, scope?: Scope): Promise<Team> => {
let partial = "/team/" + id;
if (scope) {
partial += "?kind=" + scope.kind + "&key=" + scope.key;
}
return new Promise(function(resolve, reject) {
get(pondURL(partial))
.then((response: any) => resolve(Team.any(response.data)))
.catch((error: any) => reject(error));
});
};
const removeTeam = (key: string) => {
return del<pond.RemoveTeamResponse>(pondURL("/teams/" + key));
};
const updateTeam = (key: string, team: pond.TeamSettings, asRoot?: boolean) => {
return put<pond.UpdateBinYardResponse>(
pondURL("/teams/" + key + (asRoot ? "?asRoot=" + asRoot.toString() : "")),
team
);
};
const updatePreferences = (
key: string,
preferences: pond.TeamPreferences,
keys?: string[],
types?: string[]
) => {
return put(pondURL("/teams/" + key + "/preferences"), preferences);
};
const listTeams = (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
asRoot?: boolean,
as?: string
) => {
//let asText = team ? "&as=" + team.key() : "";
return get<pond.ListTeamsResponse>(
pondURL(
"/teams" +
"?limit=" +
limit +
"&offset=" +
offset +
("&order=" + or(order, "asc")) +
("&by=" + or(orderBy, "key")) +
(search ? "&search=" + search : "") +
(asRoot ? "&asRoot=" + asRoot.toString() : "") +
(as ? "&as=" + as : "")
)
);
};
const listAllTeams = () => {
//let asText = team ? "&as=" + team.key() : "";
return get<pond.ListTeamsResponse>(pondURL("/allTeams"));
};
const listObjectTeams = (scope: Scope, as?: string) => {
if (as) {
return get(pondURL(`/${scope.kind}s/${scope.key}/teams?as=${as}`));
}
return get(pondURL("/" + scope.kind + "s/" + scope.key + "/teams"));
};
return (
<TeamAPIContext.Provider
value={{
addTeam,
getTeam,
updateTeam,
updatePreferences,
listTeams,
listObjectTeams,
removeTeam,
listAllTeams
}}>
{children}
</TeamAPIContext.Provider>
);
}
export const useTeamAPI = () => useContext(TeamAPIContext);