Skip to content

Commit

Permalink
fix(runs): fix bug where setting the time to 0 did not work after eve…
Browse files Browse the repository at this point in the history
…nts are loaded from the server again
  • Loading branch information
benji6 committed Jul 22, 2024
1 parent 90eebd8 commit 58bcc7f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
7 changes: 4 additions & 3 deletions client/src/components/pages/Run/EditRun.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export default function EditRun() {

const payload = { id } as UpdateRun;
if (formMeters !== event.meters) payload.meters = formMeters;
if (formSeconds !== event.seconds) payload.seconds = formSeconds;
if (formSeconds !== event.seconds)
payload.seconds = formSeconds ?? null;

dispatch(
eventsSlice.actions.add({
Expand All @@ -95,7 +96,7 @@ export default function EditRun() {
<Select
{...FIELDS.runMinutes}
defaultValue={
event.seconds === undefined
event.seconds == null
? undefined
: Math.floor(event.seconds / TIME.secondsPerMinute)
}
Expand All @@ -110,7 +111,7 @@ export default function EditRun() {
<Select
{...FIELDS.runSeconds}
defaultValue={
event.seconds === undefined
event.seconds == null
? undefined
: event.seconds % TIME.secondsPerMinute
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/shared/RunCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function RunCard({ id }: Props) {
</b>
)}
{event.meters !== undefined && event.seconds !== undefined && <br />}
{event.seconds !== undefined && (
{event.seconds != undefined && (
<b data-test-id={TEST_IDS.runCardTime}>
{formatSecondsAsTime(event.seconds)}
</b>
Expand Down
3 changes: 2 additions & 1 deletion client/src/store/eventsSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ describe("eventsSlice", () => {
payload: {
id: "2024-07-22T08:00:00.000Z",
meters: 1e3,
seconds: undefined,
seconds: null,
},
}),
);
Expand All @@ -932,6 +932,7 @@ describe("eventsSlice", () => {
byId: {
"2024-07-22T08:00:00.000Z": {
meters: 1e3,
seconds: null,
updatedAt: "2024-07-22T09:00:00.000Z",
},
},
Expand Down
22 changes: 17 additions & 5 deletions client/src/store/eventsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,23 @@ const trackedCategoriesSelector = createSelector(

// for reasons that are beyond my energy to investigate there is
// a runtime error if you try to update the data object directly
all.byId[payload.id] = {
...all.byId[payload.id],
...rest,
updatedAt: event.createdAt,
};
const previousEventVersion = all.byId[payload.id];
if (previousEventVersion.type === "meditations")
all.byId[payload.id] = {
...previousEventVersion,
// meditations have a `second` property which cannot be `null`
// but runs have a `seconds` property which can
// TypeScript doesn't know that `rest` cannot have `null` `seconds` and apply to a meditation simultaneously
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(rest as any),
updatedAt: event.createdAt,
};
else
all.byId[payload.id] = {
...previousEventVersion,
...rest,
updatedAt: event.createdAt,
};
normalizedCategory.byId[payload.id] = {
...normalizedCategory.byId[payload.id],
...rest,
Expand Down
8 changes: 4 additions & 4 deletions client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,24 @@ export type Run =
| {
location?: DeviceGeolocation;
meters: number;
seconds?: number;
seconds?: number | null;
}
| {
location?: DeviceGeolocation;
meters?: number;
seconds: number;
seconds: number | null;
};

export type UpdateRun =
| {
id: string;
meters: number;
seconds?: number;
seconds?: number | null;
}
| {
id: string;
meters?: number;
seconds: number;
seconds: number | null;
};

interface ValueAndLocationEvent {
Expand Down
2 changes: 1 addition & 1 deletion scripts/cloudformation/lambdas/events_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def handler(event, context):
k: float(v) if isinstance(v, Decimal) else v
for k, v in payload["location"].items()
}
if "seconds" in payload:
if isinstance(payload.get("seconds"), Decimal):
payload["seconds"] = int(payload["seconds"])
for k, v in payload.items():
if isinstance(v, Decimal):
Expand Down

0 comments on commit 58bcc7f

Please sign in to comment.