updated as in the field api

This commit is contained in:
csawatzky 2025-04-21 14:44:07 -06:00
parent 29902bc40b
commit ae458c3fca
3 changed files with 17 additions and 15 deletions

View file

@ -14,7 +14,7 @@ import ResponsiveDialog from "common/ResponsiveDialog";
import React from "react";
import { useState } from "react";
import { useEffect } from "react";
import { useFieldAPI } from "providers";
import { useFieldAPI, useGlobalState } from "providers";
import { pond } from "protobuf-ts/pond";
import { useSnackbar } from "hooks";
import { Field } from "models";
@ -35,6 +35,7 @@ interface Props {
export default function FieldSettings(props: Props) {
const { selectedField, onClose, removeField, updateFields, borders } = props;
const [{as}] = useGlobalState();
const [fieldName, setFieldName] = useState("");
const [acres, setAcres] = useState<number>(0);
const [cropType, setCropType] = useState<pond.Grain>(pond.Grain.GRAIN_INVALID);
@ -89,7 +90,7 @@ export default function FieldSettings(props: Props) {
settings.acres = acres;
fieldAPI
.updateField(selectedField.key(), settings)
.updateField(selectedField.key(), settings, undefined, as)
.then(resp => {
let newField = clone(selectedField);
newField.settings = settings;
@ -121,7 +122,7 @@ export default function FieldSettings(props: Props) {
}
settings.fieldGeoData = geo;
fieldAPI
.addField(settings)
.addField(settings, as)
.then(resp => {
updateFields && updateFields(resp.data.field, settings);
openSnack("New Field Created");
@ -136,7 +137,7 @@ export default function FieldSettings(props: Props) {
const deleteField = () => {
if (selectedField) {
fieldAPI
.removeField(selectedField?.key())
.removeField(selectedField?.key(), as)
.then(resp => {
openSnack("Field Mapping Deleted");
//setActiveStep(0);

View file

@ -1200,7 +1200,7 @@ import { Result } from "@mapbox/mapbox-gl-geocoder";
let newSettings = field.settings;
newSettings.fieldGeoData = geoData;
fieldAPI
.updateField(key, newSettings)
.updateField(key, newSettings, undefined, as)
.then(resp => {
let cloneGeo = clone(geoRef.current);
geoData.objectKey = key;
@ -1219,7 +1219,7 @@ import { Result } from "@mapbox/mapbox-gl-geocoder";
let field = fieldsRef.current.get(key);
if (field) {
fieldAPI
.removeField(key)
.removeField(key, as)
.then(resp => {
let cloneGeo = clone(geoRef.current);
cloneGeo.delete(key);

View file

@ -7,8 +7,8 @@ import { or } from "utils";
import { pondURL } from "./pond";
export interface IFieldAPIContext {
addField: (field: pond.FieldSettings) => Promise<any>;
getField: (fieldId: string) => Promise<any>;
addField: (field: pond.FieldSettings, as?: string) => Promise<any>;
getField: (fieldId: string, as?: string) => Promise<any>;
listFields: (
limit: number,
offset: number,
@ -18,11 +18,12 @@ export interface IFieldAPIContext {
as?: string,
asRoot?: boolean
) => Promise<AxiosResponse<pond.ListFieldsResponse>>;
removeField: (key: string) => Promise<AxiosResponse<pond.RemoveFieldResponse>>;
removeField: (key: string, as?: string) => Promise<AxiosResponse<pond.RemoveFieldResponse>>;
updateField: (
key: string,
field: pond.FieldSettings,
asRoot?: true
asRoot?: true,
as?: string
) => Promise<AxiosResponse<pond.UpdateSiteResponse>>;
}
@ -33,19 +34,19 @@ interface Props {}
export default function FieldProvider(props: PropsWithChildren<Props>) {
const { children } = props;
const { get, del, post, put } = useHTTP();
const [{ as }] = useGlobalState();
//const [{ as }] = useGlobalState();
const addField = (field: pond.FieldSettings) => {
const addField = (field: pond.FieldSettings, as?: string) => {
if (as) return post<pond.AddFieldResponse>(pondURL("/fields?as=" + as), field);
return post(pondURL("/fields"), field);
};
const getField = (fieldId: string) => {
const getField = (fieldId: string, as?: string) => {
if (as) return get(pondURL("/field/" + fieldId + "?as=" + as));
return get(pondURL("/field/" + fieldId));
};
const removeField = (key: string) => {
const removeField = (key: string, as?: string) => {
if (as) return del<pond.RemoveFieldResponse>(pondURL("/fields/" + key + "?as=" + as));
return del<pond.RemoveFieldResponse>(pondURL("/fields/" + key));
};
@ -75,7 +76,7 @@ export default function FieldProvider(props: PropsWithChildren<Props>) {
);
};
const updateField = (key: string, field: pond.FieldSettings, asRoot?: boolean) => {
const updateField = (key: string, field: pond.FieldSettings, asRoot?: boolean, as?: string) => {
if (as)
return put<pond.UpdateFieldResponse>(
pondURL("/fields/" + key + "?as=" + as + (asRoot ? "&asRoot=" + asRoot.toString() : "")),