Skip to content

Commit

Permalink
Update state optimistically
Browse files Browse the repository at this point in the history
  • Loading branch information
sidoh committed Oct 21, 2024
1 parent 5b45e5a commit b47f4dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions web2/components/light/light-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ export function LightList() {
variant: "destructive",
});
});

dispatch({
type: "UPDATE_ALL_STATE",
payload: { state: anyLightOn ? "OFF" : "ON" },
});
};

return (
Expand Down
17 changes: 13 additions & 4 deletions web2/components/light/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type Action =
device: Device;
payload: Partial<z.infer<typeof schemas.NormalizedGroupState>>;
}
| {
type: "UPDATE_ALL_STATE";
payload: Partial<z.infer<typeof schemas.NormalizedGroupState>>;
}
| {
type: "SET_LIGHTS";
lights: z.infer<typeof schemas.GatewayListItem>[];
Expand All @@ -37,10 +41,7 @@ type Action =
name: string;
};

function devicesAreEqual(
a: Device,
b: Device
) {
function devicesAreEqual(a: Device, b: Device) {
return (
a.device_id === b.device_id &&
a.device_type === b.device_type &&
Expand All @@ -63,6 +64,14 @@ export function reducer(
: light
),
};
case "UPDATE_ALL_STATE":
return {
...state,
lights: state.lights.map((light) => ({
...light,
state: action.payload,
})),
};
case "SET_LIGHTS":
return {
...state,
Expand Down

0 comments on commit b47f4dd

Please sign in to comment.