2021-06-01 15:59:42 +02:00
|
|
|
<script>
|
|
|
|
import { Select } from "@budibase/bbui"
|
|
|
|
import { getContext } from "svelte"
|
|
|
|
import dayjs from "dayjs"
|
|
|
|
import utc from "dayjs/plugin/utc"
|
|
|
|
import { onMount } from "svelte"
|
|
|
|
|
|
|
|
dayjs.extend(utc)
|
|
|
|
|
|
|
|
export let dataProvider
|
|
|
|
export let field
|
2021-06-02 09:38:19 +02:00
|
|
|
export let defaultValue
|
2021-06-01 15:59:42 +02:00
|
|
|
|
|
|
|
const component = getContext("component")
|
2021-06-15 20:36:56 +02:00
|
|
|
const { styleable, ActionTypes, getAction } = getContext("sdk")
|
2021-06-03 11:10:25 +02:00
|
|
|
|
|
|
|
const setQuery = getAction(dataProvider?.id, ActionTypes.SetDataProviderQuery)
|
2021-06-01 15:59:42 +02:00
|
|
|
const options = [
|
|
|
|
"Last 1 day",
|
|
|
|
"Last 7 days",
|
|
|
|
"Last 30 days",
|
|
|
|
"Last 3 months",
|
|
|
|
"Last 6 months",
|
|
|
|
"Last 1 year",
|
|
|
|
]
|
2021-06-02 09:38:19 +02:00
|
|
|
let value = options.includes(defaultValue) ? defaultValue : "Last 30 days"
|
2021-06-01 15:59:42 +02:00
|
|
|
|
|
|
|
const updateDateRange = option => {
|
|
|
|
const query = dataProvider?.state?.query
|
2021-06-03 11:10:25 +02:00
|
|
|
if (!query || !setQuery) {
|
2021-06-01 15:59:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-03 11:10:25 +02:00
|
|
|
value = option
|
2021-06-01 15:59:42 +02:00
|
|
|
let low = dayjs.utc().subtract(1, "year")
|
|
|
|
let high = dayjs.utc().add(1, "day")
|
|
|
|
|
2021-06-02 09:28:19 +02:00
|
|
|
if (option === "Last 1 day") {
|
2021-06-01 15:59:42 +02:00
|
|
|
low = dayjs.utc().subtract(1, "day")
|
|
|
|
} else if (option === "Last 7 days") {
|
|
|
|
low = dayjs.utc().subtract(7, "days")
|
|
|
|
} else if (option === "Last 30 days") {
|
|
|
|
low = dayjs.utc().subtract(30, "days")
|
|
|
|
} else if (option === "Last 3 months") {
|
|
|
|
low = dayjs.utc().subtract(3, "months")
|
|
|
|
} else if (option === "Last 6 months") {
|
|
|
|
low = dayjs.utc().subtract(6, "months")
|
|
|
|
}
|
|
|
|
|
2021-06-03 11:10:25 +02:00
|
|
|
// Update data provider query with the new filter
|
|
|
|
setQuery({
|
2021-06-01 15:59:42 +02:00
|
|
|
...query,
|
|
|
|
range: {
|
|
|
|
...query.range,
|
|
|
|
[field]: {
|
|
|
|
high: high.format(),
|
|
|
|
low: low.format(),
|
|
|
|
},
|
|
|
|
},
|
2021-06-03 11:10:25 +02:00
|
|
|
})
|
2021-06-01 15:59:42 +02:00
|
|
|
}
|
|
|
|
|
2021-06-03 11:10:25 +02:00
|
|
|
// Update the range on mount to the initial value
|
2021-06-01 15:59:42 +02:00
|
|
|
onMount(() => {
|
|
|
|
updateDateRange(value)
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div use:styleable={$component.styles}>
|
|
|
|
<Select
|
|
|
|
placeholder={null}
|
|
|
|
{options}
|
|
|
|
{value}
|
|
|
|
on:change={e => updateDateRange(e.detail)}
|
|
|
|
/>
|
|
|
|
</div>
|