updates frontend reducer to accept new sio_callback event data

This commit is contained in:
Mose Müller
2023-12-05 10:49:33 +01:00
parent 52d571e551
commit aab2b4ee77
2 changed files with 8 additions and 13 deletions

View File

@@ -18,12 +18,11 @@ type Action =
| { type: 'SET_DATA'; data: State } | { type: 'SET_DATA'; data: State }
| { | {
type: 'UPDATE_ATTRIBUTE'; type: 'UPDATE_ATTRIBUTE';
parentPath: string; fullAccessPath: string;
name: string; newValue: SerializedValue;
value: SerializedValue;
}; };
type UpdateMessage = { type UpdateMessage = {
data: { parent_path: string; name: string; value: SerializedValue }; data: { full_access_path: string; value: SerializedValue };
}; };
type LogMessage = { type LogMessage = {
levelname: LevelName; levelname: LevelName;
@@ -35,10 +34,7 @@ const reducer = (state: State, action: Action): State => {
case 'SET_DATA': case 'SET_DATA':
return action.data; return action.data;
case 'UPDATE_ATTRIBUTE': { case 'UPDATE_ATTRIBUTE': {
const pathList = action.parentPath.split('.').slice(1).concat(action.name); return setNestedValueByPath(state, action.fullAccessPath, action.newValue);
const joinedPath = pathList.join('.');
return setNestedValueByPath(state, joinedPath, action.value);
} }
default: default:
throw new Error(); throw new Error();
@@ -129,14 +125,13 @@ const App = () => {
function onNotify(value: UpdateMessage) { function onNotify(value: UpdateMessage) {
// Extracting data from the notification // Extracting data from the notification
const { parent_path: parentPath, name, value: newValue } = value.data; const { full_access_path: fullAccessPath, value: newValue } = value.data;
// Dispatching the update to the reducer // Dispatching the update to the reducer
dispatch({ dispatch({
type: 'UPDATE_ATTRIBUTE', type: 'UPDATE_ATTRIBUTE',
parentPath, fullAccessPath,
name, newValue
value: newValue
}); });
} }

View File

@@ -123,7 +123,7 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
// Whether to show the name infront of the component (false if used with a slider) // Whether to show the name infront of the component (false if used with a slider)
const showName = props.showName !== undefined ? props.showName : true; const showName = props.showName !== undefined ? props.showName : true;
// If emitUpdate is passed, use this instead of the emit_update from the socket // If emitUpdate is passed, use this instead of the setAttribute from the socket
// Also used when used with a slider // Also used when used with a slider
const emitUpdate = const emitUpdate =
props.customEmitUpdate !== undefined ? props.customEmitUpdate : setAttribute; props.customEmitUpdate !== undefined ? props.customEmitUpdate : setAttribute;