update to mine api for as to be passed in to the functions

This commit is contained in:
csawatzky 2025-04-21 13:24:26 -06:00
parent 022837de87
commit 9b9f4cdf63
9 changed files with 50 additions and 34 deletions

View file

@ -13,6 +13,7 @@ import { LibraryAdd } from "@mui/icons-material";
import ResponsiveTable, { Column } from "common/ResponsiveTable"; import ResponsiveTable, { Column } from "common/ResponsiveTable";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { or } from "utils"; import { or } from "utils";
import { useGlobalState } from "providers";
interface MineRow { interface MineRow {
mine: pond.Mine; mine: pond.Mine;
@ -44,11 +45,12 @@ export default function Mines() {
const [pageSize, setPageSize] = useState(10); const [pageSize, setPageSize] = useState(10);
const [mineDialog, setMineDialog] = useState(false); const [mineDialog, setMineDialog] = useState(false);
const [searchValue, setSearchValue] = useState(""); const [searchValue, setSearchValue] = useState("");
const [{as}] = useGlobalState();
const load = useCallback(() => { const load = useCallback(() => {
setIsLoading(true); setIsLoading(true);
mineAPI mineAPI
.listMines(pageSize, pageSize * page, "desc", undefined, searchValue, false) .listMines(pageSize, pageSize * page, "desc", undefined, searchValue, false, as)
.then(resp => { .then(resp => {
let mineData: MineRow[] = []; let mineData: MineRow[] = [];
resp.data.mines.forEach(mine => { resp.data.mines.forEach(mine => {
@ -62,7 +64,7 @@ export default function Mines() {
.finally(() => { .finally(() => {
setIsLoading(false); setIsLoading(false);
}); });
}, [pageSize, page, mineAPI, setIsLoading, searchValue]); }, [pageSize, page, mineAPI, setIsLoading, searchValue, as]);
useEffect(() => { useEffect(() => {
load(); load();

View file

@ -13,6 +13,7 @@ import { useMineAPI } from "hooks";
import { Sensor } from "ventilation/drawable/Sensor"; import { Sensor } from "ventilation/drawable/Sensor";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { makeStyles } from "@mui/styles"; import { makeStyles } from "@mui/styles";
import { useGlobalState } from "providers";
const useStyles = makeStyles((theme: Theme) => { const useStyles = makeStyles((theme: Theme) => {
return ({ return ({
@ -78,6 +79,7 @@ export default function Ventilation() {
>(new Map()); >(new Map());
const [sensors, setSensors] = useState<Sensor[]>([]); const [sensors, setSensors] = useState<Sensor[]>([]);
const [componentDevices, setComponentDevices] = useState<Map<string, number>>(new Map()); const [componentDevices, setComponentDevices] = useState<Map<string, number>>(new Map());
const [{as}] = useGlobalState();
const handleResize = (node: any) => { const handleResize = (node: any) => {
setHeight(node.getBoundingClientRect().height); setHeight(node.getBoundingClientRect().height);
@ -96,7 +98,7 @@ export default function Ventilation() {
if (!mineKey) return; if (!mineKey) return;
setLoading(true); setLoading(true);
mineAPI mineAPI
.getMine(mineKey) .getMine(mineKey, as)
.then((resp: { data: pond.IGetMineResponse | undefined; }) => { .then((resp: { data: pond.IGetMineResponse | undefined; }) => {
if (resp.data && resp.data.mine && resp.data.mine.settings) { if (resp.data && resp.data.mine && resp.data.mine.settings) {
let data = pond.GetMineResponse.create(resp.data); let data = pond.GetMineResponse.create(resp.data);

View file

@ -7,14 +7,15 @@ import { createContext, PropsWithChildren, useContext } from "react";
import { pondURL } from "./pond"; import { pondURL } from "./pond";
export interface IMineAPIContext { export interface IMineAPIContext {
addMine: (mine: pond.MineSettings) => Promise<AxiosResponse<pond.AddMineResponse>>; addMine: (mine: pond.MineSettings, as?: string) => Promise<AxiosResponse<pond.AddMineResponse>>;
getMine: (key: string) => Promise<AxiosResponse<pond.GetMineResponse>>; getMine: (key: string, as?: string) => Promise<AxiosResponse<pond.GetMineResponse>>;
addDevice: (key: string, deviceID: string, permissions: pond.Permission[]) => Promise<any>; addDevice: (key: string, deviceID: string, permissions: pond.Permission[], as?: string) => Promise<any>;
addComponent: ( addComponent: (
key: string, key: string,
deviceID: string, deviceID: string,
componentKey: string, componentKey: string,
permissions: pond.Permission[] permissions: pond.Permission[],
as?: string
) => Promise<any>; ) => Promise<any>;
listMines: ( listMines: (
limit: number, limit: number,
@ -22,7 +23,8 @@ export interface IMineAPIContext {
order?: "asc" | "desc", order?: "asc" | "desc",
orderBy?: string, orderBy?: string,
search?: string, search?: string,
asRoot?: boolean asRoot?: boolean,
as?: string
) => Promise<AxiosResponse<pond.ListMinesResponse>>; ) => Promise<AxiosResponse<pond.ListMinesResponse>>;
listMinesSimple: ( listMinesSimple: (
limit: number, limit: number,
@ -30,9 +32,10 @@ export interface IMineAPIContext {
order?: "asc" | "desc", order?: "asc" | "desc",
orderBy?: string, orderBy?: string,
search?: string, search?: string,
asRoot?: boolean asRoot?: boolean,
as?: string
) => Promise<AxiosResponse<pond.ListMinesSimpleResponse>>; ) => Promise<AxiosResponse<pond.ListMinesSimpleResponse>>;
removeMine: (key: string) => Promise<AxiosResponse<pond.RemoveMineResponse>>; removeMine: (key: string, as?: string) => Promise<AxiosResponse<pond.RemoveMineResponse>>;
updateMine: ( updateMine: (
mine: pond.MineSettings, mine: pond.MineSettings,
componentPreferences: Map<string, pond.MineComponentPreferences> componentPreferences: Map<string, pond.MineComponentPreferences>
@ -46,13 +49,13 @@ interface Props {}
export default function MineProvider(props: PropsWithChildren<Props>) { export default function MineProvider(props: PropsWithChildren<Props>) {
const { children } = props; const { children } = props;
const { get, del, post, put } = useHTTP(); 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<pond.AddMineResponse>(pondURL("/mines" + (as ? "?as=" + as : "")), mine); return post<pond.AddMineResponse>(pondURL("/mines" + (as ? "?as=" + as : "")), mine);
}; };
const getMine = (key: string) => { const getMine = (key: string, as?: string) => {
return get<pond.GetMineResponse>(pondURL("/mines/" + key + (as ? "?as=" + as : ""))); return get<pond.GetMineResponse>(pondURL("/mines/" + key + (as ? "?as=" + as : "")));
}; };
@ -64,7 +67,8 @@ export default function MineProvider(props: PropsWithChildren<Props>) {
search?: string, search?: string,
asRoot?: boolean, asRoot?: boolean,
keys?: string, keys?: string,
types?: string types?: string,
as?: string
) => { ) => {
const url = pondURL( const url = pondURL(
"/mines" + "/mines" +
@ -89,7 +93,8 @@ export default function MineProvider(props: PropsWithChildren<Props>) {
order?: "asc" | "desc", order?: "asc" | "desc",
orderBy?: string, orderBy?: string,
search?: string, search?: string,
asRoot?: boolean asRoot?: boolean,
as?: string
) => { ) => {
const url = pondURL( const url = pondURL(
"/minesSimple" + "/minesSimple" +
@ -106,7 +111,7 @@ export default function MineProvider(props: PropsWithChildren<Props>) {
return get<pond.ListMinesSimpleResponse>(url); return get<pond.ListMinesSimpleResponse>(url);
}; };
const removeMine = (key: string) => { const removeMine = (key: string, as?: string) => {
return del<pond.RemoveMineResponse>(pondURL("/mines/" + key + (as ? "?as=" + as : ""))); return del<pond.RemoveMineResponse>(pondURL("/mines/" + key + (as ? "?as=" + as : "")));
}; };
@ -127,7 +132,7 @@ export default function MineProvider(props: PropsWithChildren<Props>) {
return put<pond.AddMineResponse>(pondURL("/mines"), request); return put<pond.AddMineResponse>(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) if (as)
return post(pondURL(`/mines/` + key + `/addDevice/` + deviceID + `?as=${as}`), { return post(pondURL(`/mines/` + key + `/addDevice/` + deviceID + `?as=${as}`), {
permissions: permissions.map(permission => permissionToString(permission)) permissions: permissions.map(permission => permissionToString(permission))
@ -141,7 +146,8 @@ export default function MineProvider(props: PropsWithChildren<Props>) {
key: string, key: string,
deviceID: string, deviceID: string,
componentKey: string, componentKey: string,
permissions: pond.Permission[] permissions: pond.Permission[],
as?: string
) => { ) => {
if (as) if (as)
return post( return post(
@ -163,8 +169,8 @@ export default function MineProvider(props: PropsWithChildren<Props>) {
addDevice, addDevice,
addComponent, addComponent,
listMines, listMines,
listMinesSimple, listMinesSimple,//
removeMine, removeMine,//
updateMine updateMine
}}> }}>
{children} {children}

View file

@ -8,6 +8,7 @@ import {
import ResponsiveDialog from "common/ResponsiveDialog"; import ResponsiveDialog from "common/ResponsiveDialog";
import { useMineAPI } from "hooks"; import { useMineAPI } from "hooks";
import { pond } from "protobuf-ts/pond"; import { pond } from "protobuf-ts/pond";
import { useGlobalState } from "providers";
import { useState } from "react"; import { useState } from "react";
interface Props { interface Props {
@ -20,11 +21,12 @@ export default function AddMine(props: Props) {
const { open, closeCallback, refreshCallback } = props; const { open, closeCallback, refreshCallback } = props;
const [name, setName] = useState(""); const [name, setName] = useState("");
const mineAPI = useMineAPI(); const mineAPI = useMineAPI();
const [{as}] = useGlobalState();
const addMine = () => { const addMine = () => {
let newMine = pond.MineSettings.create(); let newMine = pond.MineSettings.create();
newMine.name = name; newMine.name = name;
mineAPI.addMine(newMine).finally(() => { mineAPI.addMine(newMine, as).finally(() => {
closeCallback(); closeCallback();
refreshCallback(); refreshCallback();
}); });

View file

@ -111,14 +111,14 @@ export default function Editor(props: Props) {
undefined | Map<string, UnitMeasurement[]> undefined | Map<string, UnitMeasurement[]>
>(undefined); >(undefined);
const [{ user }] = useGlobalState(); const [{ user, as }] = useGlobalState();
const save = () => { const save = () => {
let settings = pond.MineSettings.create(); let settings = pond.MineSettings.create();
props.devices.forEach(device => { props.devices.forEach(device => {
if (device.settings) settings.devices.push(device.settings?.deviceId); 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); console.log(resp);
}); });
}; };

View file

@ -14,7 +14,7 @@ import CursorIcon from "assets/editor/cursor.png";
import DeleteIcon from "assets/editor/delete.png"; import DeleteIcon from "assets/editor/delete.png";
import { ImgIcon } from "common/ImgIcon"; import { ImgIcon } from "common/ImgIcon";
import { pond } from "protobuf-ts/pond"; 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 { Save, CloudDownload } from "@mui/icons-material";
import { Placeable } from "./drawable/Placeable"; import { Placeable } from "./drawable/Placeable";
import { Component } from "models"; import { Component } from "models";
@ -107,6 +107,7 @@ export default function EditorHeader(props: Props) {
const snackbar = useSnackbar(); const snackbar = useSnackbar();
const theme = useTheme(); const theme = useTheme();
const [loadDialog, setLoadDialog] = useState(false); const [loadDialog, setLoadDialog] = useState(false);
const [{as}] = useGlobalState();
const save = () => { const save = () => {
let settings = pond.MineSettings.create(); let settings = pond.MineSettings.create();
@ -141,7 +142,7 @@ export default function EditorHeader(props: Props) {
}); });
} else { } else {
mineAPI mineAPI
.addMine(settings) .addMine(settings, as)
.then(resp => { .then(resp => {
if (resp.status === 200) { if (resp.status === 200) {
snackbar.success("New mine successfully saved!"); snackbar.success("New mine successfully saved!");

View file

@ -13,7 +13,7 @@ import {
Typography Typography
} from "@mui/material"; } from "@mui/material";
import { pond } from "protobuf-ts/pond"; import { pond } from "protobuf-ts/pond";
import { useMineAPI, useSnackbar } from "providers"; import { useGlobalState, useMineAPI, useSnackbar } from "providers";
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { or } from "utils"; import { or } from "utils";
import { loadPlaceable, Placeable } from "./drawable/Placeable"; import { loadPlaceable, Placeable } from "./drawable/Placeable";
@ -80,11 +80,12 @@ export default function LoadMine(props: Props) {
const [offset, setOffset] = useState(0); const [offset, setOffset] = useState(0);
const classes = useStyles(); const classes = useStyles();
const isMobile = useMobile(); const isMobile = useMobile();
const [{as}] = useGlobalState();
const load = (key: string) => { const load = (key: string) => {
setLoading(true); setLoading(true);
mineAPI mineAPI
.getMine(key) .getMine(key, as)
.then(resp => { .then(resp => {
let newBlocks: Placeable[] = []; let newBlocks: Placeable[] = [];
if (resp.data.mine?.settings?.placeables) if (resp.data.mine?.settings?.placeables)
@ -117,13 +118,13 @@ export default function LoadMine(props: Props) {
useEffect(() => { useEffect(() => {
if (open) { if (open) {
setLoading(true); setLoading(true);
mineAPI.listMinesSimple(limit, offset).then(resp => { mineAPI.listMinesSimple(limit, offset, undefined, undefined, undefined, false, as).then(resp => {
setMines(resp.data.mines); setMines(resp.data.mines);
setLoading(false); setLoading(false);
setTotal(resp.data.total); setTotal(resp.data.total);
}); });
} }
}, [mineAPI, open, limit, offset]); }, [mineAPI, open, limit, offset, as]);
const back = () => { const back = () => {
if (offset - limit < 0) { if (offset - limit < 0) {

View file

@ -84,7 +84,7 @@ export default function ComponentCards(props: Props) {
const [firstLoad, setFirstLoad] = useState(true); const [firstLoad, setFirstLoad] = useState(true);
const themeType = useThemeType(); const themeType = useThemeType();
const classes = useStyles(); const classes = useStyles();
const [{ user }] = useGlobalState(); const [{ user, as }] = useGlobalState();
const theme = useTheme(); const theme = useTheme();
const navigate = useNavigate() const navigate = useNavigate()
@ -96,7 +96,7 @@ export default function ComponentCards(props: Props) {
types.forEach((type, index) => { types.forEach((type, index) => {
if (type === "mine") mine = keys[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) => { const addComponent = (device: string, component: Component) => {
@ -110,7 +110,7 @@ export default function ComponentCards(props: Props) {
pond.Permission.PERMISSION_SHARE, pond.Permission.PERMISSION_SHARE,
pond.Permission.PERMISSION_WRITE, pond.Permission.PERMISSION_WRITE,
pond.Permission.PERMISSION_USERS pond.Permission.PERMISSION_USERS
]) ], as)
.then(() => { .then(() => {
let prefs = getPreferences(component); let prefs = getPreferences(component);
mineComponentPreferences.set(component.key(), prefs); mineComponentPreferences.set(component.key(), prefs);

View file

@ -20,6 +20,7 @@ import { useDeviceAPI, useMineAPI } from "hooks";
import LeftIcon from "@mui/icons-material/KeyboardArrowLeft"; import LeftIcon from "@mui/icons-material/KeyboardArrowLeft";
import RightIcon from "@mui/icons-material/KeyboardArrowRight"; import RightIcon from "@mui/icons-material/KeyboardArrowRight";
import { makeStyles } from "@mui/styles"; import { makeStyles } from "@mui/styles";
import { useGlobalState } from "providers";
const useStyles = makeStyles((theme: Theme) => { const useStyles = makeStyles((theme: Theme) => {
return ({ return ({
@ -111,6 +112,7 @@ export default function DeviceDrawer(props: Props) {
const deviceAPI = useDeviceAPI(); const deviceAPI = useDeviceAPI();
const mineAPI = useMineAPI(); const mineAPI = useMineAPI();
const [tab, setTab] = React.useState(0); const [tab, setTab] = React.useState(0);
const [{as}] = useGlobalState();
useEffect(() => { useEffect(() => {
let ids: number[] = []; let ids: number[] = [];
@ -153,7 +155,7 @@ export default function DeviceDrawer(props: Props) {
const index = selectedIDs.indexOf(id); const index = selectedIDs.indexOf(id);
if (index > -1) { if (index > -1) {
if (device.settings && mine.name !== "") { 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); selectedIDs.splice(index, 1);
let d = selectedDevices; let d = selectedDevices;
let deviceIndex = 0; let deviceIndex = 0;
@ -175,7 +177,7 @@ export default function DeviceDrawer(props: Props) {
pond.Permission.PERMISSION_SHARE, pond.Permission.PERMISSION_SHARE,
pond.Permission.PERMISSION_WRITE, pond.Permission.PERMISSION_WRITE,
pond.Permission.PERMISSION_USERS pond.Permission.PERMISSION_USERS
]) ], as)
.then(resp => { .then(resp => {
selectedIDs.push(id); selectedIDs.push(id);
let d = selectedDevices; let d = selectedDevices;