2021-05-18 23:20:41 +02:00
|
|
|
<script>
|
2022-07-15 15:15:16 +02:00
|
|
|
import { Button, Select, Input, Label } from "@budibase/bbui"
|
2022-09-07 18:05:17 +02:00
|
|
|
import { onMount, createEventDispatcher } from "svelte"
|
|
|
|
import { flags } from "stores/backend"
|
2024-02-12 16:25:00 +01:00
|
|
|
import { helpers } from "@budibase/shared-core"
|
2024-02-12 17:12:52 +01:00
|
|
|
|
2021-09-09 13:00:37 +02:00
|
|
|
const dispatch = createEventDispatcher()
|
2021-05-18 23:20:41 +02:00
|
|
|
|
|
|
|
export let value
|
2024-02-12 16:25:00 +01:00
|
|
|
let error
|
|
|
|
|
2024-02-12 16:31:19 +01:00
|
|
|
const reboot = "@reboot"
|
|
|
|
|
2024-02-12 16:25:00 +01:00
|
|
|
$: {
|
|
|
|
const exists = CRON_EXPRESSIONS.some(cron => cron.value === value)
|
|
|
|
const customIndex = CRON_EXPRESSIONS.findIndex(
|
|
|
|
cron => cron.label === "Custom"
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!exists && customIndex === -1) {
|
|
|
|
CRON_EXPRESSIONS[0] = { label: "Custom", value: value }
|
|
|
|
} else if (exists && customIndex !== -1) {
|
|
|
|
CRON_EXPRESSIONS.splice(customIndex, 1)
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 14:55:18 +01:00
|
|
|
|
2021-09-09 13:00:37 +02:00
|
|
|
const onChange = e => {
|
2024-02-12 16:31:19 +01:00
|
|
|
if (value !== reboot) {
|
|
|
|
error = helpers.cron.validate(e.detail).err
|
|
|
|
}
|
2024-02-12 16:25:00 +01:00
|
|
|
if (e.detail === value || error) {
|
2023-02-23 14:55:18 +01:00
|
|
|
return
|
|
|
|
}
|
2024-02-12 16:25:00 +01:00
|
|
|
|
2021-09-09 13:00:37 +02:00
|
|
|
value = e.detail
|
|
|
|
dispatch("change", e.detail)
|
|
|
|
}
|
2021-05-18 23:20:41 +02:00
|
|
|
|
2022-07-15 15:15:16 +02:00
|
|
|
let touched = false
|
2021-05-18 23:20:41 +02:00
|
|
|
let presets = false
|
|
|
|
|
|
|
|
const CRON_EXPRESSIONS = [
|
|
|
|
{
|
|
|
|
label: "Every Minute",
|
2021-05-19 11:46:47 +02:00
|
|
|
value: "* * * * *",
|
2021-05-18 23:20:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Every Hour",
|
2021-05-19 11:46:47 +02:00
|
|
|
value: "0 * * * *",
|
2021-05-18 23:20:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Every Morning at 8AM",
|
2021-05-19 11:46:47 +02:00
|
|
|
value: "0 8 * * *",
|
2021-05-18 23:20:41 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Every Night at Midnight",
|
2021-05-19 11:46:47 +02:00
|
|
|
value: "0 0 * * *",
|
2021-05-18 23:20:41 +02:00
|
|
|
},
|
|
|
|
]
|
2022-09-07 18:05:17 +02:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
if (!$flags.cloud) {
|
|
|
|
CRON_EXPRESSIONS.push({
|
|
|
|
label: "Every Budibase Reboot",
|
2024-02-12 16:31:19 +01:00
|
|
|
value: reboot,
|
2022-09-07 18:05:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-05-18 23:20:41 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="block-field">
|
2023-02-23 14:55:18 +01:00
|
|
|
<Input
|
2024-02-12 16:25:00 +01:00
|
|
|
{error}
|
2023-02-23 14:55:18 +01:00
|
|
|
on:change={onChange}
|
|
|
|
{value}
|
|
|
|
on:blur={() => (touched = true)}
|
|
|
|
updateOnChange={false}
|
|
|
|
/>
|
2022-07-15 15:15:16 +02:00
|
|
|
{#if touched && !value}
|
|
|
|
<Label><div class="error">Please specify a CRON expression</div></Label>
|
|
|
|
{/if}
|
2021-05-18 23:20:41 +02:00
|
|
|
<div class="presets">
|
2021-05-19 11:46:47 +02:00
|
|
|
<Button on:click={() => (presets = !presets)}
|
|
|
|
>{presets ? "Hide" : "Show"} Presets</Button
|
|
|
|
>
|
2021-05-18 23:20:41 +02:00
|
|
|
{#if presets}
|
2021-05-19 11:46:47 +02:00
|
|
|
<Select
|
2021-09-09 13:00:37 +02:00
|
|
|
on:change={onChange}
|
2024-02-12 16:25:00 +01:00
|
|
|
value={value || "Custom"}
|
2021-05-19 11:46:47 +02:00
|
|
|
secondary
|
|
|
|
extraThin
|
|
|
|
label="Presets"
|
|
|
|
options={CRON_EXPRESSIONS}
|
|
|
|
/>
|
2021-05-18 23:20:41 +02:00
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
2021-05-19 11:46:47 +02:00
|
|
|
.presets {
|
|
|
|
margin-top: var(--spacing-m);
|
|
|
|
}
|
|
|
|
.block-field {
|
|
|
|
padding-top: var(--spacing-s);
|
|
|
|
}
|
2022-07-15 15:15:16 +02:00
|
|
|
.error {
|
|
|
|
padding-top: var(--spacing-xs);
|
|
|
|
color: var(--spectrum-global-color-red-500);
|
|
|
|
}
|
2021-05-18 23:20:41 +02:00
|
|
|
</style>
|