Update frontend. Fixed CreateInstallation and delete Installation bug

This commit is contained in:
Noe 2025-09-11 14:28:49 +02:00
parent 73880f0737
commit 2b1d550571
11 changed files with 166 additions and 187 deletions

View File

@ -312,8 +312,7 @@ public static class SessionMethods
&& installation is not null
&& user.UserType != 0)
{
if (installation.Product is (int)ProductType.Salimax or (int)ProductType.Salidomo || installation.Product == (int)ProductType.SodiStoreMax)
{
return
Db.Delete(installation)
&& await installation.RevokeReadKey()
@ -321,12 +320,7 @@ public static class SessionMethods
&& await installation.RemoveReadRole()
&& await installation.RemoveWriteRole()
&& await installation.DeleteBucket();
}
else
{
return Db.Delete(installation);
}
}
return false;

View File

@ -22,6 +22,8 @@ import { UserContext } from '../../../contexts/userContext';
import { UserType } from '../../../interfaces/UserTypes';
import { ProductIdContext } from '../../../contexts/ProductIdContextProvider';
import { useLocation, useNavigate } from 'react-router-dom';
interface InformationProps {
values: I_Installation;
s3Credentials: I_S3Credentials;
@ -82,10 +84,28 @@ function Information(props: InformationProps) {
setOpenModalDeleteInstallation(true);
};
const location = useLocation();
const navigate = useNavigate();
const deleteInstallationModalHandle = () => {
setOpenModalDeleteInstallation(false);
deleteInstallation(formValues, props.type);
setLoading(false);
const pathSegments = location.pathname.split('/'); // e.g. ['', 'sodiohome_installations', 'list', 'installation', '123', 'information']
const base = `/${pathSegments[1]}`;
const isListView = pathSegments.includes('list');
const isTreeView = pathSegments.includes('tree');
if (isListView) {
navigate(`${base}/list/`);
} else if (isTreeView) {
navigate(`${base}/tree/`);
} else {
// fallback
navigate(`${base}/list/`);
}
};
const deleteInstallationModalHandleCancel = () => {

View File

@ -51,6 +51,7 @@ function InstallationTabs(props: InstallationTabsProps) {
if (path[path.length - 2] === 'list') {
setCurrentTab('list');
fetchAllInstallations(product, false);
} else if (path[path.length - 2] === 'tree') {
setCurrentTab('tree');
} else {

View File

@ -26,7 +26,6 @@ function installationForm(props: installationFormProps) {
const theme = useTheme();
const [open, setOpen] = useState(true);
console.log('productToInsert IS ', props.productToInsert);
const [formValues, setFormValues] = useState<Partial<I_Installation>>({
installationName: '',
name: '',
@ -64,6 +63,7 @@ function installationForm(props: installationFormProps) {
formValues.parentId = props.parentid;
formValues.product = props.productToInsert;
const responseData = await createInstallation(formValues);
console.log('awaited');
props.submit();
};
const handleCancelSubmit = (e) => {

View File

@ -35,7 +35,7 @@ function SalidomoInstallationTabs(props: InstallationTabsProps) {
const {
salidomoInstallations,
fetchAllSalidomoInstallations,
fetchAllInstallations,
socket,
openSocket,
closeSocket
@ -48,6 +48,7 @@ function SalidomoInstallationTabs(props: InstallationTabsProps) {
if (path[path.length - 2] === 'list') {
setCurrentTab('list');
fetchAllInstallations(product, false);
} else if (path[path.length - 2] === 'tree') {
setCurrentTab('tree');
} else {
@ -62,7 +63,7 @@ function SalidomoInstallationTabs(props: InstallationTabsProps) {
useEffect(() => {
if (product == props.product) {
fetchAllSalidomoInstallations();
fetchAllInstallations(product);
}
}, [product]);

View File

@ -37,7 +37,7 @@ function SodioHomeInstallationTabs(props: SodioHomeInstallationTabsProps) {
useState<boolean>(false);
const {
sodiohomeInstallations,
fetchAllSodiohomeInstallations,
fetchAllInstallations,
socket,
openSocket,
closeSocket
@ -49,6 +49,7 @@ function SodioHomeInstallationTabs(props: SodioHomeInstallationTabsProps) {
if (path[path.length - 2] === 'list') {
setCurrentTab('list');
fetchAllInstallations(product, false);
} else if (path[path.length - 2] === 'tree') {
setCurrentTab('tree');
} else {
@ -63,7 +64,7 @@ function SodioHomeInstallationTabs(props: SodioHomeInstallationTabsProps) {
useEffect(() => {
if (product == props.product) {
fetchAllSodiohomeInstallations();
fetchAllInstallations(product);
}
}, [product]);

View File

@ -46,6 +46,7 @@ function CustomTreeItem(props: CustomTreeItemProps) {
const handleSelectOneInstallation = (): void => {
let installation = props.node;
let installation_path =
installation.product == 0
? routes.installations
@ -60,7 +61,7 @@ function CustomTreeItem(props: CustomTreeItemProps) {
? routes.installations
: product == 1
? routes.salidomo_installations
: installation.product == 2
: product == 2
? routes.sodiohome_installations
: routes.sodistore_installations;

View File

@ -54,17 +54,17 @@ function TreeInformation(props: TreeInformationProps) {
updated,
setUpdated,
updateFolder,
deleteFolder
deleteFolder,
fetchAllInstallations
} = installationContext;
const [product, setProduct] = useState('Salimax');
const handleChangeInstallationChoice = (e) => {
setProduct(e.target.value); // Directly update the product state
// console.log('Selected Product:', e.target.value);
};
const ProductTypes = ['Salimax', 'Salidomo', 'Sodiohome', 'SodistoreMax'];
const ProductTypes = ['Salimax', 'Salidomo', 'SodistoreHome', 'SodistoreMax'];
const isMobile = window.innerWidth <= 1490;
@ -84,7 +84,6 @@ function TreeInformation(props: TreeInformationProps) {
const handleNewInstallationInsertion = () => {
setOpenModalInstallationChoice(true);
//setOpenModalInstallation(true);
};
const handleCancelSubmitInstallationChoice = () => {
@ -323,7 +322,7 @@ function TreeInformation(props: TreeInformationProps) {
)}
{openModalInstallation &&
(product == 'Salimax' ||
product == 'Sodiohome' ||
product == 'SodistoreHome' ||
product == 'SodistoreMax') && (
<InstallationForm
cancel={handleFormCancel}
@ -339,13 +338,6 @@ function TreeInformation(props: TreeInformationProps) {
parentid={props.folder.id}
/>
)}
{/*{openModalInstallation && product == 'Sodiohome' && (*/}
{/* <SodiohomeInstallationForm*/}
{/* cancel={handleFormCancel}*/}
{/* submit={handleInstallationFormSubmit}*/}
{/* parentid={props.folder.id}*/}
{/* />*/}
{/*)}*/}
<Container maxWidth="xl">
<Grid

View File

@ -80,7 +80,7 @@ function InstallationTree() {
key={installation.id}
path={routes.installation + installation.id + '*'}
element={
installation.product == 0 ? (
installation.product == 0 || installation.product == 3 ? (
<Installation
key={installation.id}
current_installation={installation}

View File

@ -1,4 +1,3 @@
import { AxiosError, AxiosResponse } from 'axios';
import {
createContext,
ReactNode,
@ -39,12 +38,8 @@ const InstallationsContextProvider = ({
const navigate = useNavigate();
const tokencontext = useContext(TokenContext);
const { removeToken } = tokencontext;
// const [socket, setSocket] = useState<WebSocket>(null);
const socket = useRef<WebSocket | null>(null); // Using useRef instead of useState
//const [currentProduct, setcurrentProduct] = useState<number>(0);
//Store pending updates and apply them in batches
const pendingUpdates = useRef<
Record<number, { status: number; testingMode: boolean }>
@ -165,56 +160,36 @@ const InstallationsContextProvider = ({
// Function to fetch installations and manage the socket
const fetchAllInstallations = useCallback(
async (product: number) => {
axiosConfig
.get(`/GetAllInstallationsFromProduct?product=${product}`)
.then((res: AxiosResponse<I_Installation[]>) => {
setSalimax_Or_Sodistore_Installations(res.data); // Update installations
openSocket(res.data); // Open a new socket after installations are fetched
})
.catch((err: AxiosError) => {
if (err.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
async (product: number, open_socket: boolean = true) => {
try {
const res = await axiosConfig.get(
`/GetAllInstallationsFromProduct?product=${product}`
);
if (product === 2) {
setSodiohomeInstallations(res.data);
} else if (product === 1) {
setSalidomoInstallations(res.data);
} else if (product === 0 || product === 3) {
setSalimax_Or_Sodistore_Installations(res.data);
}
if (open_socket) {
openSocket(res.data);
}
} catch (err: any) {
if (err.response?.status === 401) {
removeToken();
navigate(routes.login);
}
}
},
[navigate, removeToken]
);
const fetchAllSalidomoInstallations = useCallback(async () => {
axiosConfig
.get('/GetAllSalidomoInstallations')
.then((res: AxiosResponse<I_Installation[]>) => {
setSalidomoInstallations(res.data);
openSocket(res.data);
})
.catch((err: AxiosError) => {
if (err.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
}, [navigate, removeToken]);
const fetchAllSodiohomeInstallations = useCallback(async () => {
axiosConfig
.get('/GetAllSodioHomeInstallations')
.then((res: AxiosResponse<I_Installation[]>) => {
setSodiohomeInstallations(res.data);
openSocket(res.data);
})
.catch((err: AxiosError) => {
if (err.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
}, [navigate, removeToken]);
const fetchAllFoldersAndInstallations = useCallback(
async (product: number) => {
axiosConfig
await axiosConfig
.get(`/GetAllFoldersAndInstallations?productId=${product}`)
.then((res) => setFoldersAndInstallations(res.data))
.catch((err) => {
@ -229,52 +204,50 @@ const InstallationsContextProvider = ({
const createInstallation = useCallback(
async (formValues: Partial<I_Installation>) => {
axiosConfig
.post('/CreateInstallation', formValues)
.then(() => {
setLoading(false);
fetchAllFoldersAndInstallations(formValues.product);
})
.catch((error) => {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
try {
// wait for the POST to finish
const res = await axiosConfig.post('/CreateInstallation', formValues);
const product = formValues.product; // or res.data.product
await Promise.resolve(fetchAllFoldersAndInstallations(product));
await Promise.resolve(fetchAllInstallations(product));
setLoading(false);
return res.data; // let callers await this if they want
} catch (error: any) {
setError(true);
setLoading(false);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
}
},
[fetchAllFoldersAndInstallations, navigate, removeToken]
[navigate, removeToken, fetchAllFoldersAndInstallations]
);
const updateInstallation = useCallback(
async (formValues: I_Installation, view: string) => {
axiosConfig
.put('/UpdateInstallation', formValues)
.then(() => {
setUpdated(true);
setLoading(false);
if (formValues.product === 0 && view === 'installation')
fetchAllInstallations(formValues.product);
else if (formValues.product === 1 && view === 'installation')
fetchAllSalidomoInstallations();
else if (formValues.product === 2 && view === 'installation')
fetchAllSodiohomeInstallations();
else fetchAllFoldersAndInstallations(formValues.product);
setTimeout(() => setUpdated(false), 3000);
})
.catch((error) => {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
try {
const res = await axiosConfig.put('/UpdateInstallation', formValues);
setUpdated(true);
setLoading(false);
const product = formValues.product; // or res.data.product
await Promise.resolve(fetchAllFoldersAndInstallations(product));
await Promise.resolve(fetchAllInstallations(product));
setTimeout(() => setUpdated(false), 3000);
} catch (error) {
setError(true);
setLoading(false);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
}
},
[
fetchAllFoldersAndInstallations,
fetchAllInstallations,
fetchAllSalidomoInstallations,
fetchAllSodiohomeInstallations,
navigate,
removeToken
]
@ -282,97 +255,96 @@ const InstallationsContextProvider = ({
const deleteInstallation = useCallback(
async (formValues: I_Installation, view: string) => {
axiosConfig
.delete(`/DeleteInstallation?installationId=${formValues.id}`)
.then(() => {
setUpdated(true);
setLoading(false);
if (formValues.product === 0 && view === 'installation')
fetchAllInstallations(formValues.product);
else if (formValues.product === 1 && view === 'installation')
fetchAllSalidomoInstallations();
else if (formValues.product === 2 && view === 'installation')
fetchAllSodiohomeInstallations();
else fetchAllFoldersAndInstallations(formValues.product);
setTimeout(() => setUpdated(false), 3000);
})
.catch((error) => {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
try {
// wait for the POST to finish
const res = await axiosConfig.delete(
`/DeleteInstallation?installationId=${formValues.id}`
);
setUpdated(true);
setTimeout(() => setUpdated(false), 3000);
setLoading(false);
const product = formValues.product; // or res.data.product
await Promise.resolve(fetchAllFoldersAndInstallations(product));
await Promise.resolve(fetchAllInstallations(product));
return res.data; // let callers await this if they want
} catch (error: any) {
setError(true);
setLoading(false);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
}
},
[
fetchAllFoldersAndInstallations,
fetchAllInstallations,
fetchAllSalidomoInstallations,
fetchAllSodiohomeInstallations,
navigate,
removeToken
removeToken,
sodiohomeInstallations,
fetchAllFoldersAndInstallations,
fetchAllInstallations
]
);
const createFolder = useCallback(
async (formValues: Partial<I_Folder>, product: number) => {
axiosConfig
.post('/CreateFolder', formValues)
.then(() => {
setLoading(false);
fetchAllFoldersAndInstallations(product);
})
.catch((error) => {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
try {
const res = await axiosConfig.post('/CreateFolder', formValues);
setLoading(false);
await Promise.resolve(fetchAllFoldersAndInstallations(product));
} catch (error) {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
}
},
[fetchAllFoldersAndInstallations, navigate, removeToken]
);
const updateFolder = useCallback(
async (formValues: I_Folder, product: number) => {
axiosConfig
.put('/UpdateFolder', formValues)
.then(() => {
setLoading(false);
setUpdated(true);
try {
const res = await axiosConfig.put('/UpdateFolder', formValues);
fetchAllFoldersAndInstallations(product);
setTimeout(() => setUpdated(false), 3000);
setLoading(false);
})
.catch((error) => {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
setLoading(false);
setUpdated(true);
setTimeout(() => setUpdated(false), 3000);
await Promise.resolve(fetchAllFoldersAndInstallations(product));
setLoading(false);
} catch (error) {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
}
},
[fetchAllFoldersAndInstallations, navigate, removeToken]
);
const deleteFolder = useCallback(
async (formValues: I_Folder, product: number) => {
axiosConfig
.delete(`/DeleteFolder?folderId=${formValues.id}`)
.then(() => {
setUpdated(true);
setLoading(false);
fetchAllFoldersAndInstallations(product);
setTimeout(() => setUpdated(false), 3000);
})
.catch((error) => {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
});
try {
const res = await axiosConfig.delete(
`/DeleteFolder?folderId=${formValues.id}`
);
setUpdated(true);
setLoading(false);
await Promise.resolve(fetchAllFoldersAndInstallations(product));
setTimeout(() => setUpdated(false), 3000);
} catch (error) {
setError(true);
if (error.response?.status === 401) {
removeToken();
navigate(routes.login);
}
}
},
[fetchAllFoldersAndInstallations, navigate, removeToken]
);
@ -384,8 +356,6 @@ const InstallationsContextProvider = ({
sodiohomeInstallations,
foldersAndInstallations,
fetchAllInstallations,
fetchAllSalidomoInstallations,
fetchAllSodiohomeInstallations,
fetchAllFoldersAndInstallations,
createInstallation,
updateInstallation,
@ -413,7 +383,6 @@ const InstallationsContextProvider = ({
error,
updated,
socket
//currentProduct
]
);

View File

@ -255,7 +255,7 @@ function SidebarMenu() {
<Box sx={{ marginTop: '3px' }}>
<FormattedMessage
id="sodiohome"
defaultMessage="Sodiohome"
defaultMessage="Sodistore Home"
/>
</Box>
</Button>