diff --git a/src/pages/Gate.tsx b/src/pages/Gate.tsx index c0de27f..c32cb84 100644 --- a/src/pages/Gate.tsx +++ b/src/pages/Gate.tsx @@ -3,7 +3,7 @@ import { Gate as IGate } from "models/Gate"; //import { Redirect, useHistory, useRouteMatch } from "react-router"; import React, { useCallback, useEffect, useState } from "react"; import PageContainer from "./PageContainer"; -import { useGlobalState, useGateAPI, useUserAPI } from "providers"; +import { useGlobalState, useGateAPI } from "providers"; import { Box, ButtonBase, @@ -20,7 +20,7 @@ import { } from "@mui/material"; import { pond } from "protobuf-ts/pond"; import DeviceLinkDrawer from "common/DeviceLinkDrawer"; -import { Component, Device, Scope } from "models"; +import { Component, Device } from "models"; import GateActions from "gate/GateActions"; import GateDevice from "gate/GateDevice"; import ObjectControls from "common/ObjectControls"; @@ -28,7 +28,7 @@ import { Link } from "@mui/icons-material"; import Chat from "chat/Chat"; import PlaneIcon from "products/AviationIcons/PlaneIcon"; import NotesIcon from "@mui/icons-material/Notes"; -import { useMobile, useSnackbar, useThemeType } from "hooks"; +import { useMobile, usePermissionAPI, useSnackbar, useThemeType } from "hooks"; import { clone } from "lodash"; import { useNavigate, useParams } from "react-router-dom"; import { makeStyles } from "@mui/styles"; @@ -90,7 +90,7 @@ export default function Gate(props: Props) { const gateID = gateKey ?? useParams<{ gateKey: string }>()?.gateKey ?? ""; const classes = useStyles(); const gateAPI = useGateAPI(); - const userAPI = useUserAPI(); + const permissionAPI = usePermissionAPI(); const [gate, setGate] = useState(IGate.create()); const [devices, setDevices] = useState>( new Map() @@ -115,16 +115,10 @@ export default function Gate(props: Props) { }; useEffect(() => { - let key = gateID; - let kind = "gate"; - if (as) { - key = as; - kind = "team"; - } - userAPI.getUser(user.id(), { key: key, kind: kind } as Scope).then(resp => { - setPermissions(resp.permissions); - }); - }, [as, gateID, userAPI, user]); + permissionAPI.getPermissions(user.id(), [gateID], ["gate"]).then(resp => { + setPermissions(pond.EvaluatePermissionsResponse.fromObject(resp.data).permissions) + }) + }, [as, gateID, permissionAPI, user]); const loadGate = useCallback(() => { let id = gateID; diff --git a/src/providers/pond/permissionAPI.tsx b/src/providers/pond/permissionAPI.tsx index 91b7d6a..1c75827 100644 --- a/src/providers/pond/permissionAPI.tsx +++ b/src/providers/pond/permissionAPI.tsx @@ -8,7 +8,7 @@ import { createContext, PropsWithChildren, useContext } from "react"; import { objectQueryParams, pondURL } from "./pond"; export interface IPermissionAPIContext { - getPermissions: (user: string, scope: Scope) => Promise>; + getPermissions: (user: string, keys: string[], types: string[]) => Promise>; removePermissions: (user: string, scope: Scope) => Promise>; updatePermissions: (scope: Scope, users: User[] | Team[]) => Promise>; updateRelativePermissions: ( @@ -44,9 +44,12 @@ export default function PermissionProvider(props: PropsWithChildren) { const { get, del, put, post } = useHTTP(); const [{ as }] = useGlobalState(); - const getPermissions = (user: string, scope: Scope) => { + const getPermissions = (user: string, keys: string[], types: string[]) => { return new Promise((resolve, reject) => { - get(pondURL("/users/" + user + "/permissions" + objectQueryParams(scope))).then(resp => { + get(pondURL( + "/users/" + user + "/permissions?types=" + types.toString() + "&keys=" + keys.toString() + + (as && "&as=" + as) + )).then(resp => { return resolve(resp) }).catch(err => { return reject(err) @@ -54,6 +57,8 @@ export default function PermissionProvider(props: PropsWithChildren) { }) }; + + const removePermissions = (user: string, scope: Scope) => { return new Promise((resolve, reject) => { del(pondURL("/users/" + user + "/permissions" + objectQueryParams(scope))).then(resp => {