|
|
@ -3,7 +3,7 @@ |
|
|
import { getContext } from "svelte" |
|
|
import { getContext } from "svelte" |
|
|
import dayjs from "dayjs" |
|
|
import dayjs from "dayjs" |
|
|
import utc from "dayjs/plugin/utc" |
|
|
import utc from "dayjs/plugin/utc" |
|
|
import { onMount } from "svelte" |
|
|
import { onDestroy } from "svelte" |
|
|
|
|
|
|
|
|
dayjs.extend(utc) |
|
|
dayjs.extend(utc) |
|
|
|
|
|
|
|
|
@ -14,7 +14,14 @@ |
|
|
const component = getContext("component") |
|
|
const component = getContext("component") |
|
|
const { styleable, ActionTypes, getAction } = getContext("sdk") |
|
|
const { styleable, ActionTypes, getAction } = getContext("sdk") |
|
|
|
|
|
|
|
|
const setQuery = getAction(dataProvider?.id, ActionTypes.SetDataProviderQuery) |
|
|
$: addExtension = getAction( |
|
|
|
|
|
dataProvider?.id, |
|
|
|
|
|
ActionTypes.AddDataProviderQueryExtension |
|
|
|
|
|
) |
|
|
|
|
|
$: removeExtension = getAction( |
|
|
|
|
|
dataProvider?.id, |
|
|
|
|
|
ActionTypes.RemoveDataProviderQueryExtension |
|
|
|
|
|
) |
|
|
const options = [ |
|
|
const options = [ |
|
|
"Last 1 day", |
|
|
"Last 1 day", |
|
|
"Last 7 days", |
|
|
"Last 7 days", |
|
|
@ -25,44 +32,30 @@ |
|
|
] |
|
|
] |
|
|
let value = options.includes(defaultValue) ? defaultValue : "Last 30 days" |
|
|
let value = options.includes(defaultValue) ? defaultValue : "Last 30 days" |
|
|
|
|
|
|
|
|
const updateDateRange = option => { |
|
|
$: queryExtension = getQueryExtension(value) |
|
|
const query = dataProvider?.state?.query |
|
|
$: addExtension?.($component.id, "range", field, queryExtension) |
|
|
if (!query || !setQuery) { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
value = option |
|
|
const getQueryExtension = value => { |
|
|
let low = dayjs.utc().subtract(1, "year") |
|
|
let low = dayjs.utc().subtract(1, "year") |
|
|
let high = dayjs.utc().add(1, "day") |
|
|
let high = dayjs.utc().add(1, "day") |
|
|
|
|
|
|
|
|
if (option === "Last 1 day") { |
|
|
if (value === "Last 1 day") { |
|
|
low = dayjs.utc().subtract(1, "day") |
|
|
low = dayjs.utc().subtract(1, "day") |
|
|
} else if (option === "Last 7 days") { |
|
|
} else if (value === "Last 7 days") { |
|
|
low = dayjs.utc().subtract(7, "days") |
|
|
low = dayjs.utc().subtract(7, "days") |
|
|
} else if (option === "Last 30 days") { |
|
|
} else if (value === "Last 30 days") { |
|
|
low = dayjs.utc().subtract(30, "days") |
|
|
low = dayjs.utc().subtract(30, "days") |
|
|
} else if (option === "Last 3 months") { |
|
|
} else if (value === "Last 3 months") { |
|
|
low = dayjs.utc().subtract(3, "months") |
|
|
low = dayjs.utc().subtract(3, "months") |
|
|
} else if (option === "Last 6 months") { |
|
|
} else if (value === "Last 6 months") { |
|
|
low = dayjs.utc().subtract(6, "months") |
|
|
low = dayjs.utc().subtract(6, "months") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Update data provider query with the new filter |
|
|
return { low: low.format(), high: high.format() } |
|
|
setQuery({ |
|
|
|
|
|
...query, |
|
|
|
|
|
range: { |
|
|
|
|
|
...query.range, |
|
|
|
|
|
[field]: { |
|
|
|
|
|
high: high.format(), |
|
|
|
|
|
low: low.format(), |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Update the range on mount to the initial value |
|
|
onDestroy(() => { |
|
|
onMount(() => { |
|
|
removeExtension?.($component.id) |
|
|
updateDateRange(value) |
|
|
|
|
|
}) |
|
|
}) |
|
|
</script> |
|
|
</script> |
|
|
|
|
|
|
|
|
@ -71,6 +64,6 @@ |
|
|
placeholder={null} |
|
|
placeholder={null} |
|
|
{options} |
|
|
{options} |
|
|
{value} |
|
|
{value} |
|
|
on:change={e => updateDateRange(e.detail)} |
|
|
on:change={e => (value = e.detail)} |
|
|
/> |
|
|
/> |
|
|
</div> |
|
|
</div> |
|
|
|