From 9b9f4cdf6372eff96a63b177216e51bbc6bdc8fb Mon Sep 17 00:00:00 2001 From: csawatzky Date: Mon, 21 Apr 2025 13:24:26 -0600 Subject: [PATCH] update to mine api for as to be passed in to the functions --- src/pages/Mines.tsx | 6 ++-- src/pages/VentEditor.tsx | 4 ++- src/providers/pond/mineAPI.tsx | 40 +++++++++++++--------- src/ventilation/AddMine.tsx | 4 ++- src/ventilation/Editor.tsx | 4 +-- src/ventilation/EditorHeader.tsx | 5 +-- src/ventilation/LoadMine.tsx | 9 ++--- src/ventilation/drawers/ComponentCards.tsx | 6 ++-- src/ventilation/drawers/DeviceDrawer.tsx | 6 ++-- 9 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/pages/Mines.tsx b/src/pages/Mines.tsx index 22b508a..5e73bae 100644 --- a/src/pages/Mines.tsx +++ b/src/pages/Mines.tsx @@ -13,6 +13,7 @@ import { LibraryAdd } from "@mui/icons-material"; import ResponsiveTable, { Column } from "common/ResponsiveTable"; import { useNavigate } from "react-router-dom"; import { or } from "utils"; +import { useGlobalState } from "providers"; interface MineRow { mine: pond.Mine; @@ -44,11 +45,12 @@ export default function Mines() { const [pageSize, setPageSize] = useState(10); const [mineDialog, setMineDialog] = useState(false); const [searchValue, setSearchValue] = useState(""); + const [{as}] = useGlobalState(); const load = useCallback(() => { setIsLoading(true); mineAPI - .listMines(pageSize, pageSize * page, "desc", undefined, searchValue, false) + .listMines(pageSize, pageSize * page, "desc", undefined, searchValue, false, as) .then(resp => { let mineData: MineRow[] = []; resp.data.mines.forEach(mine => { @@ -62,7 +64,7 @@ export default function Mines() { .finally(() => { setIsLoading(false); }); - }, [pageSize, page, mineAPI, setIsLoading, searchValue]); + }, [pageSize, page, mineAPI, setIsLoading, searchValue, as]); useEffect(() => { load(); diff --git a/src/pages/VentEditor.tsx b/src/pages/VentEditor.tsx index 8e46944..143c9bf 100644 --- a/src/pages/VentEditor.tsx +++ b/src/pages/VentEditor.tsx @@ -13,6 +13,7 @@ import { useMineAPI } from "hooks"; import { Sensor } from "ventilation/drawable/Sensor"; import { useParams } from "react-router-dom"; import { makeStyles } from "@mui/styles"; +import { useGlobalState } from "providers"; const useStyles = makeStyles((theme: Theme) => { return ({ @@ -78,6 +79,7 @@ export default function Ventilation() { >(new Map()); const [sensors, setSensors] = useState([]); const [componentDevices, setComponentDevices] = useState>(new Map()); + const [{as}] = useGlobalState(); const handleResize = (node: any) => { setHeight(node.getBoundingClientRect().height); @@ -96,7 +98,7 @@ export default function Ventilation() { if (!mineKey) return; setLoading(true); mineAPI - .getMine(mineKey) + .getMine(mineKey, as) .then((resp: { data: pond.IGetMineResponse | undefined; }) => { if (resp.data && resp.data.mine && resp.data.mine.settings) { let data = pond.GetMineResponse.create(resp.data); diff --git a/src/providers/pond/mineAPI.tsx b/src/providers/pond/mineAPI.tsx index 305b3ec..50dc6a0 100644 --- a/src/providers/pond/mineAPI.tsx +++ b/src/providers/pond/mineAPI.tsx @@ -7,14 +7,15 @@ import { createContext, PropsWithChildren, useContext } from "react"; import { pondURL } from "./pond"; export interface IMineAPIContext { - addMine: (mine: pond.MineSettings) => Promise>; - getMine: (key: string) => Promise>; - addDevice: (key: string, deviceID: string, permissions: pond.Permission[]) => Promise; + addMine: (mine: pond.MineSettings, as?: string) => Promise>; + getMine: (key: string, as?: string) => Promise>; + addDevice: (key: string, deviceID: string, permissions: pond.Permission[], as?: string) => Promise; addComponent: ( key: string, deviceID: string, componentKey: string, - permissions: pond.Permission[] + permissions: pond.Permission[], + as?: string ) => Promise; listMines: ( limit: number, @@ -22,7 +23,8 @@ export interface IMineAPIContext { order?: "asc" | "desc", orderBy?: string, search?: string, - asRoot?: boolean + asRoot?: boolean, + as?: string ) => Promise>; listMinesSimple: ( limit: number, @@ -30,9 +32,10 @@ export interface IMineAPIContext { order?: "asc" | "desc", orderBy?: string, search?: string, - asRoot?: boolean + asRoot?: boolean, + as?: string ) => Promise>; - removeMine: (key: string) => Promise>; + removeMine: (key: string, as?: string) => Promise>; updateMine: ( mine: pond.MineSettings, componentPreferences: Map @@ -46,13 +49,13 @@ interface Props {} export default function MineProvider(props: PropsWithChildren) { const { children } = props; const { get, del, post, put } = useHTTP(); - const [{ as }] = useGlobalState(); + //const [{ as }] = useGlobalState(); - const addMine = (mine: pond.MineSettings) => { + const addMine = (mine: pond.MineSettings, as?: string) => { return post(pondURL("/mines" + (as ? "?as=" + as : "")), mine); }; - const getMine = (key: string) => { + const getMine = (key: string, as?: string) => { return get(pondURL("/mines/" + key + (as ? "?as=" + as : ""))); }; @@ -64,7 +67,8 @@ export default function MineProvider(props: PropsWithChildren) { search?: string, asRoot?: boolean, keys?: string, - types?: string + types?: string, + as?: string ) => { const url = pondURL( "/mines" + @@ -89,7 +93,8 @@ export default function MineProvider(props: PropsWithChildren) { order?: "asc" | "desc", orderBy?: string, search?: string, - asRoot?: boolean + asRoot?: boolean, + as?: string ) => { const url = pondURL( "/minesSimple" + @@ -106,7 +111,7 @@ export default function MineProvider(props: PropsWithChildren) { return get(url); }; - const removeMine = (key: string) => { + const removeMine = (key: string, as?: string) => { return del(pondURL("/mines/" + key + (as ? "?as=" + as : ""))); }; @@ -127,7 +132,7 @@ export default function MineProvider(props: PropsWithChildren) { return put(pondURL("/mines"), request); }; - const addDevice = (key: string, deviceID: string, permissions: pond.Permission[]) => { + const addDevice = (key: string, deviceID: string, permissions: pond.Permission[], as?: string) => { if (as) return post(pondURL(`/mines/` + key + `/addDevice/` + deviceID + `?as=${as}`), { permissions: permissions.map(permission => permissionToString(permission)) @@ -141,7 +146,8 @@ export default function MineProvider(props: PropsWithChildren) { key: string, deviceID: string, componentKey: string, - permissions: pond.Permission[] + permissions: pond.Permission[], + as?: string ) => { if (as) return post( @@ -163,8 +169,8 @@ export default function MineProvider(props: PropsWithChildren) { addDevice, addComponent, listMines, - listMinesSimple, - removeMine, + listMinesSimple,// + removeMine,// updateMine }}> {children} diff --git a/src/ventilation/AddMine.tsx b/src/ventilation/AddMine.tsx index aec2c07..a2028f5 100644 --- a/src/ventilation/AddMine.tsx +++ b/src/ventilation/AddMine.tsx @@ -8,6 +8,7 @@ import { import ResponsiveDialog from "common/ResponsiveDialog"; import { useMineAPI } from "hooks"; import { pond } from "protobuf-ts/pond"; +import { useGlobalState } from "providers"; import { useState } from "react"; interface Props { @@ -20,11 +21,12 @@ export default function AddMine(props: Props) { const { open, closeCallback, refreshCallback } = props; const [name, setName] = useState(""); const mineAPI = useMineAPI(); + const [{as}] = useGlobalState(); const addMine = () => { let newMine = pond.MineSettings.create(); newMine.name = name; - mineAPI.addMine(newMine).finally(() => { + mineAPI.addMine(newMine, as).finally(() => { closeCallback(); refreshCallback(); }); diff --git a/src/ventilation/Editor.tsx b/src/ventilation/Editor.tsx index caa3e6e..ca8dd51 100644 --- a/src/ventilation/Editor.tsx +++ b/src/ventilation/Editor.tsx @@ -111,14 +111,14 @@ export default function Editor(props: Props) { undefined | Map >(undefined); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const save = () => { let settings = pond.MineSettings.create(); props.devices.forEach(device => { if (device.settings) settings.devices.push(device.settings?.deviceId); }); - mineAPI.addMine(settings).then((resp: any) => { + mineAPI.addMine(settings, as).then((resp: any) => { console.log(resp); }); }; diff --git a/src/ventilation/EditorHeader.tsx b/src/ventilation/EditorHeader.tsx index e76c742..e7e86e2 100644 --- a/src/ventilation/EditorHeader.tsx +++ b/src/ventilation/EditorHeader.tsx @@ -14,7 +14,7 @@ import CursorIcon from "assets/editor/cursor.png"; import DeleteIcon from "assets/editor/delete.png"; import { ImgIcon } from "common/ImgIcon"; import { pond } from "protobuf-ts/pond"; -import { useMineAPI, useSnackbar } from "providers"; +import { useGlobalState, useMineAPI, useSnackbar } from "providers"; import { Save, CloudDownload } from "@mui/icons-material"; import { Placeable } from "./drawable/Placeable"; import { Component } from "models"; @@ -107,6 +107,7 @@ export default function EditorHeader(props: Props) { const snackbar = useSnackbar(); const theme = useTheme(); const [loadDialog, setLoadDialog] = useState(false); + const [{as}] = useGlobalState(); const save = () => { let settings = pond.MineSettings.create(); @@ -141,7 +142,7 @@ export default function EditorHeader(props: Props) { }); } else { mineAPI - .addMine(settings) + .addMine(settings, as) .then(resp => { if (resp.status === 200) { snackbar.success("New mine successfully saved!"); diff --git a/src/ventilation/LoadMine.tsx b/src/ventilation/LoadMine.tsx index 231cff0..9d6500e 100644 --- a/src/ventilation/LoadMine.tsx +++ b/src/ventilation/LoadMine.tsx @@ -13,7 +13,7 @@ import { Typography } from "@mui/material"; import { pond } from "protobuf-ts/pond"; -import { useMineAPI, useSnackbar } from "providers"; +import { useGlobalState, useMineAPI, useSnackbar } from "providers"; import React, { useEffect, useState } from "react"; import { or } from "utils"; import { loadPlaceable, Placeable } from "./drawable/Placeable"; @@ -80,11 +80,12 @@ export default function LoadMine(props: Props) { const [offset, setOffset] = useState(0); const classes = useStyles(); const isMobile = useMobile(); + const [{as}] = useGlobalState(); const load = (key: string) => { setLoading(true); mineAPI - .getMine(key) + .getMine(key, as) .then(resp => { let newBlocks: Placeable[] = []; if (resp.data.mine?.settings?.placeables) @@ -117,13 +118,13 @@ export default function LoadMine(props: Props) { useEffect(() => { if (open) { setLoading(true); - mineAPI.listMinesSimple(limit, offset).then(resp => { + mineAPI.listMinesSimple(limit, offset, undefined, undefined, undefined, false, as).then(resp => { setMines(resp.data.mines); setLoading(false); setTotal(resp.data.total); }); } - }, [mineAPI, open, limit, offset]); + }, [mineAPI, open, limit, offset, as]); const back = () => { if (offset - limit < 0) { diff --git a/src/ventilation/drawers/ComponentCards.tsx b/src/ventilation/drawers/ComponentCards.tsx index 072f31c..e9cab7a 100644 --- a/src/ventilation/drawers/ComponentCards.tsx +++ b/src/ventilation/drawers/ComponentCards.tsx @@ -84,7 +84,7 @@ export default function ComponentCards(props: Props) { const [firstLoad, setFirstLoad] = useState(true); const themeType = useThemeType(); const classes = useStyles(); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const theme = useTheme(); const navigate = useNavigate() @@ -96,7 +96,7 @@ export default function ComponentCards(props: Props) { types.forEach((type, index) => { if (type === "mine") mine = keys[index]; }); - mineAPI.addComponent(mine, device, component.key(), [pond.Permission.PERMISSION_INVALID]); + mineAPI.addComponent(mine, device, component.key(), [pond.Permission.PERMISSION_INVALID], as); }; const addComponent = (device: string, component: Component) => { @@ -110,7 +110,7 @@ export default function ComponentCards(props: Props) { pond.Permission.PERMISSION_SHARE, pond.Permission.PERMISSION_WRITE, pond.Permission.PERMISSION_USERS - ]) + ], as) .then(() => { let prefs = getPreferences(component); mineComponentPreferences.set(component.key(), prefs); diff --git a/src/ventilation/drawers/DeviceDrawer.tsx b/src/ventilation/drawers/DeviceDrawer.tsx index 379e35f..5aa84f3 100644 --- a/src/ventilation/drawers/DeviceDrawer.tsx +++ b/src/ventilation/drawers/DeviceDrawer.tsx @@ -20,6 +20,7 @@ import { useDeviceAPI, useMineAPI } from "hooks"; import LeftIcon from "@mui/icons-material/KeyboardArrowLeft"; import RightIcon from "@mui/icons-material/KeyboardArrowRight"; import { makeStyles } from "@mui/styles"; +import { useGlobalState } from "providers"; const useStyles = makeStyles((theme: Theme) => { return ({ @@ -111,6 +112,7 @@ export default function DeviceDrawer(props: Props) { const deviceAPI = useDeviceAPI(); const mineAPI = useMineAPI(); const [tab, setTab] = React.useState(0); + const [{as}] = useGlobalState(); useEffect(() => { let ids: number[] = []; @@ -153,7 +155,7 @@ export default function DeviceDrawer(props: Props) { const index = selectedIDs.indexOf(id); if (index > -1) { if (device.settings && mine.name !== "") { - mineAPI.addDevice(mine.key, device.settings.deviceId.toString(), []).then(resp => { + mineAPI.addDevice(mine.key, device.settings.deviceId.toString(), [], as).then(resp => { selectedIDs.splice(index, 1); let d = selectedDevices; let deviceIndex = 0; @@ -175,7 +177,7 @@ export default function DeviceDrawer(props: Props) { pond.Permission.PERMISSION_SHARE, pond.Permission.PERMISSION_WRITE, pond.Permission.PERMISSION_USERS - ]) + ], as) .then(resp => { selectedIDs.push(id); let d = selectedDevices;