mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
update #6
This commit is contained in:
@@ -23,14 +23,13 @@ export const updateValue =
|
||||
};
|
||||
|
||||
export const updateValueDirty =
|
||||
<S>(origData: any, dirtyFlags: any, setDirtyFlags: any, updateEntity: any) =>
|
||||
// <S>(origData: any, dirtyFlags: any, setDirtyFlags: any, updateEntity: UpdateEntity<S>) =>
|
||||
(origData: any, dirtyFlags: any, setDirtyFlags: any, updateDataValue: any) =>
|
||||
(event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const updated_value = extractEventValue(event);
|
||||
const name = event.target.name;
|
||||
|
||||
// TODO not sure how this is even working!!
|
||||
updateEntity((prevState) => ({
|
||||
updateDataValue((prevState) => ({
|
||||
...prevState,
|
||||
[name]: updated_value
|
||||
}));
|
||||
@@ -38,9 +37,9 @@ export const updateValueDirty =
|
||||
const arr: string[] = dirtyFlags;
|
||||
|
||||
// TODO remove comments
|
||||
// console.log('updating ' + name + ' to ' + updated_value); // TODO remove
|
||||
// console.log('dirtyFlags:', dirtyFlags); // TODO remove
|
||||
// console.log('binding.ts origData:', origData); // TODO remove
|
||||
// console.log('updating ' + name + ' to ' + updated_value);
|
||||
// console.log('dirtyFlags:', dirtyFlags);
|
||||
// console.log('binding.ts origData:', origData);
|
||||
|
||||
if (origData[name] !== updated_value) {
|
||||
if (!arr.includes(name)) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// TODO can be removed!
|
||||
// TODO extractErrorMessage function can be removed!
|
||||
export const extractErrorMessage = (error: any, defaultMessage: string) => {
|
||||
if (error.request) {
|
||||
return defaultMessage + ' (' + error.request.status + ': ' + error.request.statusText + ')';
|
||||
|
||||
@@ -4,6 +4,6 @@ export * from './route';
|
||||
export * from './submit';
|
||||
export * from './time';
|
||||
export * from './useRest';
|
||||
// TODO remove
|
||||
// TODO remove useRest2
|
||||
export * from './useRest2';
|
||||
export * from './props';
|
||||
|
||||
@@ -52,8 +52,8 @@ export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
const response = await update(toSave);
|
||||
setOrigData(response.data);
|
||||
setData(response.data);
|
||||
if (response.status === 202) {
|
||||
setRestartNeeded(true);
|
||||
if (response.status === 205) {
|
||||
setRestartNeeded(true); // reboot required
|
||||
} else {
|
||||
toast.success(LL.UPDATED_OF(LL.SETTINGS()));
|
||||
}
|
||||
|
||||
@@ -7,14 +7,15 @@ import type { AlovaXHRRequestConfig, AlovaXHRResponse, AlovaXHRResponseHeaders }
|
||||
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
export interface RestRequestOptions<D> {
|
||||
export interface RestRequestOptions2<D> {
|
||||
read: () => Method<any, any, any, any, any, AlovaXHRResponse<any>, AlovaXHRResponseHeaders>;
|
||||
update: (
|
||||
value: D
|
||||
) => Method<any, unknown, unknown, unknown, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders>;
|
||||
}
|
||||
|
||||
export const useRest2 = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
// TODO rename back to useRest
|
||||
export const useRest2 = <D>({ read, update }: RestRequestOptions2<D>) => {
|
||||
const { LL } = useI18nContext();
|
||||
|
||||
const [errorMessage, setErrorMessage] = useState<string>();
|
||||
@@ -32,12 +33,8 @@ export const useRest2 = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
onSuccess: onWriteSuccess
|
||||
} = useRequest((newData: D) => update(newData), { immediate: false });
|
||||
|
||||
const setData = (new_data: D) => {
|
||||
console.log('SET DATA'); // TODO remove console
|
||||
console.log('new_data:', new_data); // TODO remove console
|
||||
updateData({
|
||||
data: new_data
|
||||
});
|
||||
const updateDataValue = (new_data: D) => {
|
||||
updateData({ data: new_data });
|
||||
};
|
||||
|
||||
onWriteSuccess(() => {
|
||||
@@ -46,7 +43,7 @@ export const useRest2 = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
});
|
||||
|
||||
onReadComplete((event) => {
|
||||
setOrigData(event.data); // make a copy
|
||||
setOrigData(event.data);
|
||||
});
|
||||
|
||||
const loadData = async () => {
|
||||
@@ -62,7 +59,6 @@ export const useRest2 = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
console.log('SAVE DATA'); // TODO remove console
|
||||
setRestartNeeded(false);
|
||||
setErrorMessage(undefined);
|
||||
await writeData(data).catch((error) => {
|
||||
@@ -79,7 +75,7 @@ export const useRest2 = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
loadData,
|
||||
saveData,
|
||||
saving,
|
||||
setData,
|
||||
updateDataValue,
|
||||
data,
|
||||
origData,
|
||||
dirtyFlags,
|
||||
|
||||
Reference in New Issue
Block a user