fix bug with editing goal with custom dates.

This commit is contained in:
2021-08-09 16:27:34 +02:00
parent 6efdd6fb17
commit b89535e055
2 changed files with 20 additions and 6 deletions
@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { allOffsets, pastOffsets, customDateRange } from "../utils/date-range"; import { allOffsets, pastOffsets, CustomDateRange } from "../utils/date-range";
import type { DateRange } from "../utils/date-range"; import type { DateRange } from "../utils/date-range";
import { formatFormTime } from "../utils/time"; import { formatFormTime } from "../utils/time";
@@ -11,12 +11,12 @@
let rangeOptions: DateRange[]; let rangeOptions: DateRange[];
let selected = value.name; let selected = value.name;
let [customMin, customMax] = pastOffsets[0].calculate(new Date()).map(d => formatFormTime(d)); let [customMin, customMax] = (value || pastOffsets[0]).calculate(new Date()).map(d => formatFormTime(d));
$: rangeOptions = noFuture ? pastOffsets : allOffsets; $: rangeOptions = noFuture ? pastOffsets : allOffsets;
$: { $: {
if (selected === "Specific Dates") { if (selected === "Specific Dates") {
value = customDateRange(new Date(customMin), new Date(customMax)) value = new CustomDateRange(new Date(customMin), new Date(customMax))
} else { } else {
value = rangeOptions.find(v => v.name === selected); value = rangeOptions.find(v => v.name === selected);
if (value != null) { if (value != null) {
+17 -3
View File
@@ -52,8 +52,22 @@ export class RelativeDateRange {
} }
} }
export function customDateRange(from: Date, to: Date): DateRange { export class CustomDateRange {
return {name: "Specific Dates", calculate: () => [from, to]} private from: Date;
private to: Date;
constructor(from: Date, to: Date) {
this.from = from;
this.to = to;
}
get name() {
return "Specific Dates"
}
calculate(_: Date): [Date, Date] {
return [this.from, this.to];
}
} }
const thisWeek = dateRangeCallback("This week", date => [startOfWeek(date), endOfWeek(date)]); const thisWeek = dateRangeCallback("This week", date => [startOfWeek(date), endOfWeek(date)]);
@@ -96,7 +110,7 @@ export function rangeFromDates(from: Date, to: Date, options: DateRange[]): Date
} }
} }
return customDateRange(from, to); return new CustomDateRange(from, to);
} }
export const pastOffsets: DateRange[] = allOffsets.filter(r => !r.name.startsWith("Next")); export const pastOffsets: DateRange[] = allOffsets.filter(r => !r.name.startsWith("Next"));