add handle error on requests - global variables (#1808)

📝 (index.tsx): Remove unused import and update dependencies in
InputGlobalComponent
📝 (index.tsx): Add async/await to handleDelete function in
InputGlobalComponent
📝 (index.tsx): Add try/catch block to registerGlobalVariable function in
API controller
📝 (index.tsx): Add try/catch block to deleteGlobalVariable function in
API controller
📝 (index.tsx): Add try/catch block to updateGlobalVariable function in
API controller
📝 (index.tsx): Remove unused import and add deleteGlobalVariable import
in GlobalVariablesPage
📝 (index.tsx): Add setErrorData and getVariableId hooks to
GlobalVariablesPage
📝 (index.tsx): Add async/await to removeVariables function in
GlobalVariablesPage
📝 (globalVariables.ts): Remove unused import in globalVariables store
📝 (globalVariables.ts): Remove async/await from removeGlobalVariable
function in globalVariables store
This commit is contained in:
Cristhian Zanforlin Lousa 2024-04-30 16:34:38 -03:00 • committed by GitHub
commit e65ae37175
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 71 additions and 36 deletions

View file

@ -1,8 +1,8 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { deleteGlobalVariable } from "../../controllers/API";
import DeleteConfirmationModal from "../../modals/DeleteConfirmationModal"; import DeleteConfirmationModal from "../../modals/DeleteConfirmationModal";
import useAlertStore from "../../stores/alertStore"; import useAlertStore from "../../stores/alertStore";
import { useGlobalVariablesStore } from "../../stores/globalVariables"; import { useGlobalVariablesStore } from "../../stores/globalVariables";
import { ResponseErrorDetailAPI } from "../../types/api";
import { InputGlobalComponentType } from "../../types/components"; import { InputGlobalComponentType } from "../../types/components";
import { cn } from "../../utils/utils"; import { cn } from "../../utils/utils";
import AddNewVariableButton from "../addNewVariableButtonComponent/addNewVariableButton"; import AddNewVariableButton from "../addNewVariableButtonComponent/addNewVariableButton";
@ -23,7 +23,9 @@ export default function InputGlobalComponent({
); );
const getVariableId = useGlobalVariablesStore((state) => state.getVariableId); const getVariableId = useGlobalVariablesStore((state) => state.getVariableId);
const unavaliableFields = useGlobalVariablesStore((state) => state.unavaliableFields); const unavaliableFields = useGlobalVariablesStore(
(state) => state.unavaliableFields
);
const removeGlobalVariable = useGlobalVariablesStore( const removeGlobalVariable = useGlobalVariablesStore(
(state) => state.removeGlobalVariable (state) => state.removeGlobalVariable
); );
@ -41,19 +43,23 @@ export default function InputGlobalComponent({
}, [globalVariablesEntries]); }, [globalVariablesEntries]);
useEffect(() => { useEffect(() => {
if (!data.node?.template[name].value && data.node?.template[name].display_name) { if (
if(unavaliableFields[data.node?.template[name].display_name!]){ !data.node?.template[name].value &&
data.node?.template[name].display_name
) {
if (unavaliableFields[data.node?.template[name].display_name!]) {
setDb(true); setDb(true);
onChange(unavaliableFields[data.node?.template[name].display_name!]); onChange(unavaliableFields[data.node?.template[name].display_name!]);
} }
} }
},[unavaliableFields]); }, [unavaliableFields]);
function handleDelete(key: string) { async function handleDelete(key: string) {
const id = getVariableId(key); const id = getVariableId(key);
if (id !== undefined) { if (id !== undefined) {
removeGlobalVariable(key) await deleteGlobalVariable(id)
.then((_) => { .then(() => {
removeGlobalVariable(key);
if ( if (
data?.node?.template[name].value === key && data?.node?.template[name].value === key &&
data?.node?.template[name].load_from_db data?.node?.template[name].load_from_db
@ -62,11 +68,10 @@ export default function InputGlobalComponent({
setDb(false); setDb(false);
} }
}) })
.catch((error) => { .catch(() => {
let responseError = error as ResponseErrorDetailAPI;
setErrorData({ setErrorData({
title: "Error deleting variable", title: "Error deleting variable",
list: [responseError.response.data.detail ?? "Unknown error"], list: [cn("ID not found for variable: ", key)],
}); });
}); });
} else { } else {

View file

@ -884,16 +884,26 @@ export async function registerGlobalVariable({
type?: string; type?: string;
default_fields?: string[]; default_fields?: string[];
}): Promise<AxiosResponse<{ name: string; id: string; type: string }>> { }): Promise<AxiosResponse<{ name: string; id: string; type: string }>> {
return await api.post(`${BASE_URL_API}variables/`, { try {
name, const response = await api.post(`${BASE_URL_API}variables/`, {
value, name,
type, value,
default_fields: default_fields, type,
}); default_fields: default_fields,
});
return response;
} catch (error) {
throw error;
}
} }
export async function deleteGlobalVariable(id: string) { export async function deleteGlobalVariable(id: string) {
api.delete(`${BASE_URL_API}variables/${id}`); try {
const response = await api.delete(`${BASE_URL_API}variables/${id}`);
return response;
} catch (error) {
throw error;
}
} }
export async function updateGlobalVariable( export async function updateGlobalVariable(
@ -901,10 +911,16 @@ export async function updateGlobalVariable(
value: string, value: string,
id: string id: string
) { ) {
api.patch(`${BASE_URL_API}variables/${id}`, { try {
name, const response = api.patch(`${BASE_URL_API}variables/${id}`, {
value, name,
}); value,
});
return response;
} catch (error) {
throw error;
}
} }
export async function getVerticesOrder( export async function getVerticesOrder(

View file

@ -8,6 +8,8 @@ import Dropdown from "../../../../components/dropdownComponent";
import ForwardedIconComponent from "../../../../components/genericIconComponent"; import ForwardedIconComponent from "../../../../components/genericIconComponent";
import TableComponent from "../../../../components/tableComponent"; import TableComponent from "../../../../components/tableComponent";
import { Badge } from "../../../../components/ui/badge"; import { Badge } from "../../../../components/ui/badge";
import { deleteGlobalVariable } from "../../../../controllers/API";
import useAlertStore from "../../../../stores/alertStore";
import { useGlobalVariablesStore } from "../../../../stores/globalVariables"; import { useGlobalVariablesStore } from "../../../../stores/globalVariables";
import { cn } from "../../../../utils/utils"; import { cn } from "../../../../utils/utils";
@ -21,6 +23,8 @@ export default function GlobalVariablesPage() {
const globalVariables = useGlobalVariablesStore( const globalVariables = useGlobalVariablesStore(
(state) => state.globalVariables (state) => state.globalVariables
); );
const setErrorData = useAlertStore((state) => state.setErrorData);
const getVariableId = useGlobalVariablesStore((state) => state.getVariableId);
const BadgeRenderer = (props) => { const BadgeRenderer = (props) => {
return props.value !== "" ? ( return props.value !== "" ? (
@ -34,15 +38,14 @@ export default function GlobalVariablesPage() {
); );
}; };
const [rowData, setRowData] = const [rowData, setRowData] = useState<
useState< {
{ type: string | undefined;
type: string | undefined; id: string;
id: string; name: string;
name: string; default_fields: string | undefined;
default_fields: string | undefined; }[]
}[] >();
>();
useEffect(() => { useEffect(() => {
const rows: Array<{ const rows: Array<{
@ -106,10 +109,23 @@ export default function GlobalVariablesPage() {
const [selectedRows, setSelectedRows] = useState<string[]>([]); const [selectedRows, setSelectedRows] = useState<string[]>([]);
function removeVariables() { async function removeVariables() {
selectedRows.forEach((row) => { const deleteGlobalVariablesPromise = selectedRows.map(async (row) => {
removeGlobalVariable(row); const id = getVariableId(row);
const deleteGlobalVariables = deleteGlobalVariable(id!);
await deleteGlobalVariables;
}); });
Promise.all(deleteGlobalVariablesPromise)
.then(() => {
selectedRows.forEach((row) => {
removeGlobalVariable(row);
});
})
.catch(() => {
setErrorData({
title: `Error deleting global variables.`,
});
});
} }
return ( return (

View file

@ -1,5 +1,4 @@
import { create } from "zustand"; import { create } from "zustand";
import { deleteGlobalVariable } from "../controllers/API";
import { GlobalVariablesStore } from "../types/zustand/globalVariables"; import { GlobalVariablesStore } from "../types/zustand/globalVariables";
import { getUnavailableFields } from "../utils/utils"; import { getUnavailableFields } from "../utils/utils";
@ -35,7 +34,6 @@ export const useGlobalVariablesStore = create<GlobalVariablesStore>(
removeGlobalVariable: async (name) => { removeGlobalVariable: async (name) => {
const id = get().globalVariables[name]?.id; const id = get().globalVariables[name]?.id;
if (id === undefined) return; if (id === undefined) return;
await deleteGlobalVariable(id);
const newVariables = { ...get().globalVariables }; const newVariables = { ...get().globalVariables };
delete newVariables[name]; delete newVariables[name];
set({ set({