replace api/settings and customizatons with secure REST calls #501

This commit is contained in:
Proddy
2022-05-16 21:30:06 +02:00
parent ae9af3bf0b
commit fc29a3ad95
9 changed files with 110 additions and 57 deletions

View File

@@ -38,3 +38,4 @@ export function updateLogSettings(logSettings: LogSettings): AxiosPromise<LogSet
export function readLogEntries(): AxiosPromise<LogEntries> {
return AXIOS_BIN.get('/fetchLog');
}

View File

@@ -24,7 +24,22 @@ const HelpInformation: FC = () => {
const { me } = useContext(AuthenticatedContext);
const onDownload = async (endpoint: string) => {
const saveFile = (json: any, endpoint: string) => {
const a = document.createElement('a');
const filename = 'emsesp_' + endpoint + '.json';
a.href = URL.createObjectURL(
new Blob([JSON.stringify(json, null, 2)], {
type: 'text/plain'
})
);
a.setAttribute('download', filename);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
enqueueSnackbar('File downloaded', { variant: 'info' });
};
const callAPI = async (endpoint: string) => {
try {
const response = await EMSESP.API({
device: 'system',
@@ -34,19 +49,33 @@ const HelpInformation: FC = () => {
if (response.status !== 200) {
enqueueSnackbar('API call failed', { variant: 'error' });
} else {
const json = response.data;
const a = document.createElement('a');
const filename = 'emsesp_' + endpoint + '.json';
a.href = URL.createObjectURL(
new Blob([JSON.stringify(json, null, 2)], {
type: 'text/plain'
})
);
a.setAttribute('download', filename);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
enqueueSnackbar('File downloaded', { variant: 'info' });
saveFile(response.data, endpoint);
}
} catch (error: unknown) {
enqueueSnackbar(extractErrorMessage(error, 'Problem with downloading'), { variant: 'error' });
}
};
const downloadSettings = async () => {
try {
const response = await EMSESP.getSettings();
if (response.status !== 200) {
enqueueSnackbar('Unable to get settings', { variant: 'error' });
} else {
saveFile(response.data, 'settings');
}
} catch (error: unknown) {
enqueueSnackbar(extractErrorMessage(error, 'Problem with downloading'), { variant: 'error' });
}
};
const downloadCustomizations = async () => {
try {
const response = await EMSESP.getCustomizations();
if (response.status !== 200) {
enqueueSnackbar('Unable to get customizations', { variant: 'error' });
} else {
saveFile(response.data, 'customizations');
}
} catch (error: unknown) {
enqueueSnackbar(extractErrorMessage(error, 'Problem with downloading'), { variant: 'error' });
@@ -103,7 +132,7 @@ const HelpInformation: FC = () => {
</ListItemAvatar>
<ListItemText>
To report an issue or request a feature, please&nbsp;
<Link component="button" variant="body1" onClick={() => onDownload('info')}>
<Link component="button" variant="body1" onClick={() => callAPI('info')}>
download
</Link>
&nbsp;the debug information and include in a new&nbsp;
@@ -131,7 +160,7 @@ const HelpInformation: FC = () => {
startIcon={<DownloadIcon />}
variant="outlined"
color="primary"
onClick={() => onDownload('settings')}
onClick={() => downloadSettings()}
>
settings
</Button>
@@ -139,7 +168,7 @@ const HelpInformation: FC = () => {
startIcon={<DownloadIcon />}
variant="outlined"
color="primary"
onClick={() => onDownload('customizations')}
onClick={() => downloadCustomizations()}
>
customizations
</Button>

View File

@@ -86,3 +86,11 @@ export function resetCustomizations(): AxiosPromise<void> {
export function API(apiCall: APIcall): AxiosPromise<void> {
return AXIOS_API.post('/', apiCall);
}
export function getSettings(): AxiosPromise<void> {
return AXIOS.get('/getSettings');
}
export function getCustomizations(): AxiosPromise<void> {
return AXIOS.get('/getCustomizations');
}