Redmage/views/subredditsview/addview/schedule.templ

117 lines
3.7 KiB
Plaintext
Raw Normal View History

package addview
import "github.com/tigorlazuardi/redmage/views/utils"
2024-05-03 19:44:25 +07:00
import "fmt"
import "strconv"
type ScheduleInputData struct {
2024-05-03 19:44:25 +07:00
Value string
Error string
Valid string
Disabled bool
HXSwapOOB string
}
templ scheduleInputContainer() {
@ScheduleInput(ScheduleInputData{})
2024-05-03 19:44:25 +07:00
<datalist id="cron-templates">
<option value="@hourly">Every hour</option>
<option value="@daily">Every day at midnight</option>
<option value="@weekly">Every Sunday at midnight</option>
<option value="@monthly">Every start of month</option>
<option value="@yearly">Every start of year</option>
<option value="@annually">Every start of year</option>
<option value="0 0 * * MON">Every Monday at midnight</option>
<option value="0 0 * * TUE">Every Tuesday at midnight</option>
<option value="0 0 * * WED">Every Wednesday at midnight</option>
<option value="0 0 * * THU">Every Thursday at midnight</option>
<option value="0 0 * * FRI">Every Friday at midnight</option>
<option value="0 0 * * SAT">Every Saturday at midnight</option>
<option value="0 0 * * SUN">Every Sunday at midnight</option>
for i := 1; i < 24; i++ {
<option value={ fmt.Sprintf("0 %d * * *", i) }>Every day at { strconv.Itoa(i) } o'clock</option>
}
</datalist>
<script>
document.addEventListener('DOMContentLoaded', () => htmx.trigger('#schedule-input-group', 'change'))
</script>
}
templ ScheduleInput(data ScheduleInputData) {
<div
id="schedule-input-group"
2024-05-03 19:44:25 +07:00
class="form-control w-full"
hx-get="/htmx/subreddits/validate/schedule"
2024-05-03 19:44:25 +07:00
hx-trigger="change"
hx-include="this"
hx-swap="outerHTML"
2024-05-03 19:44:25 +07:00
hx-swap-oob={ data.HXSwapOOB }
>
2024-05-03 19:44:25 +07:00
<label for="schedule" class="label">
<span
class={ utils.CX(map[string]bool{
"label-text": true,
"text-error": data.Error != "",
"text-success": data.Valid != "",
"text-base": true,
}) }
>Schedule</span>
2024-05-03 19:44:25 +07:00
<div class="tooltip tooltip-left" data-tip="Whether to enable the scheduler or not">
if data.Disabled {
2024-05-03 19:44:25 +07:00
<input type="checkbox" name="enable_schedule" value="1" class="toggle toggle-primary my-auto"/>
} else {
2024-05-03 19:44:25 +07:00
<input type="checkbox" name="enable_schedule" value="1" class="toggle toggle-primary my-auto" checked/>
}
</div>
</label>
if data.Disabled {
2024-05-03 19:44:25 +07:00
<input
id="schedule"
name="schedule"
type="text"
placeholder="e.g. '@daily' or '0 0 * * MON'"
class="input input-bordered"
disabled
/>
} else {
<input
2024-05-03 19:44:25 +07:00
id="schedule"
name="schedule"
type="text"
placeholder="e.g. '@daily' or '0 0 * * MON'"
value={ data.Value }
class={ utils.CX(map[string]bool{
"input": true,
"input-bordered": true,
"input-error": data.Error != "",
"text-error": data.Error != "",
"input-success": data.Valid != "",
"text-success": data.Valid != "",
}) }
2024-05-03 19:44:25 +07:00
data-error={ data.Error }
hx-on::load="this.setCustomValidity(this.getAttribute('data-error'))"
list="cron-templates"
required
/>
}
<div class="label">
<span
class={ utils.CX(map[string]bool{
"label-text": true,
"text-error": data.Error != "",
"text-success": data.Valid != "",
"min-h-[1rem]": true,
}) }
>
if data.Valid != "" {
{ data.Valid }
2024-05-03 19:44:25 +07:00
} else if data.Error != "" {
{ data.Error }
2024-05-03 19:44:25 +07:00
} else if !data.Disabled {
Uses cron syntax. Tip: Start by typing 'every' to get suggestions or search custom expressions via Google like 'cron every 6 hours'.
}
</span>
</div>
</div>
}