| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from 'cockpit'; |
| 7 |
import React, { useState } from 'react'; |
| 8 |
import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 9 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 10 |
import { DatePicker } from "@patternfly/react-core/dist/esm/components/DatePicker/index.js"; |
| 11 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 12 |
import { Form, FormGroup, FormAlert } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 13 |
import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js"; |
| 14 |
import { InputGroup } from "@patternfly/react-core/dist/esm/components/InputGroup/index.js"; |
| 15 |
import { |
| 16 |
Modal, ModalBody, ModalFooter, ModalHeader |
| 17 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 18 |
import { Radio } from "@patternfly/react-core/dist/esm/components/Radio/index.js"; |
| 19 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 20 |
import { TimePicker } from "@patternfly/react-core/dist/esm/components/TimePicker/index.js"; |
| 21 |
import { MinusIcon, PlusIcon } from '@patternfly/react-icons'; |
| 22 |
|
| 23 |
import { FormHelper } from "cockpit-components-form-helper"; |
| 24 |
import { ModalError } from 'cockpit-components-inline-notification.jsx'; |
| 25 |
import { useDialogs } from "dialogs.jsx"; |
| 26 |
|
| 27 |
import { updateTime } from './services.jsx'; |
| 28 |
import { create_timer } from './timer-dialog-helpers.js'; |
| 29 |
import * as timeformat from "timeformat"; |
| 30 |
|
| 31 |
import "./timers.scss"; |
| 32 |
|
| 33 |
const _ = cockpit.gettext; |
| 34 |
|
| 35 |
export const CreateTimerDialogButton = ({ owner, isLoading }) => { |
| 36 |
const Dialogs = useDialogs(); |
| 37 |
return ( |
| 38 |
<Button key='create-timer-action' |
| 39 |
variant="secondary" |
| 40 |
id="create-timer" |
| 41 |
isDisabled={isLoading} |
| 42 |
onClick={() => { |
| 43 |
updateTime(); |
| 44 |
Dialogs.show(<TimerDialog owner={owner} />); |
| 45 |
}}> |
| 46 |
{_("Create timer")} |
| 47 |
</Button> |
| 48 |
); |
| 49 |
}; |
| 50 |
|
| 51 |
export const TimerDialog = ({ owner, timer }) => { |
| 52 |
const Dialogs = useDialogs(); |
| 53 |
const [command, setCommand] = useState(timer?.command || ''); |
| 54 |
const [delay, setDelay] = useState(timer?.delay || 'specific-time'); |
| 55 |
const [delayNumber, setDelayNumber] = useState(timer?.delayNumber || 0); |
| 56 |
const [delayUnit, setDelayUnit] = useState(timer?.delayUnit || 'sec'); |
| 57 |
const [description, setDescription] = useState(timer?.description || ''); |
| 58 |
const [dialogError, setDialogError] = useState(undefined); |
| 59 |
const [inProgress, setInProgress] = useState(false); |
| 60 |
const [name, setName] = useState(timer?.name || ''); |
| 61 |
const [repeat, setRepeat] = useState(timer?.repeat || 'no'); |
| 62 |
const [repeatPatterns, setRepeatPatterns] = useState(timer?.repeatPatterns || []); |
| 63 |
const [specificTime, setSpecificTime] = useState(timer?.specificTime || "00:00"); |
| 64 |
const [isSpecificTimeOpen, setSpecificTimeOpen] = useState(false); |
| 65 |
const [submitted, setSubmitted] = useState(false); |
| 66 |
const validationFailed = {}; |
| 67 |
|
| 68 |
if (!name.trim().length || !/^[a-zA-Z0-9:_.@-]+$/.test(name)) |
| 69 |
validationFailed.name = true; |
| 70 |
if (!description.trim().length) |
| 71 |
validationFailed.description = true; |
| 72 |
if (!command.trim().length) |
| 73 |
validationFailed.command = true; |
| 74 |
if (!/^[0-9]+$/.test(delayNumber)) |
| 75 |
validationFailed.delayNumber = true; |
| 76 |
|
| 77 |
const timePicker = (idx) => ( |
| 78 |
<TimePicker className="create-timer-time-picker" |
| 79 |
time={repeatPatterns[idx].time || "00:00"} |
| 80 |
is24Hour |
| 81 |
isOpen={repeatPatterns[idx].isOpen || false} |
| 82 |
setIsOpen={isOpen => setRepeatPatterns(old => { |
| 83 |
const arr = [...old]; |
| 84 |
arr[idx].isOpen = isOpen; |
| 85 |
return arr; |
| 86 |
})} |
| 87 |
menuAppendTo={() => document.body} |
| 88 |
onChange={(_, time) => setRepeatPatterns(old => { |
| 89 |
const arr = [...old]; |
| 90 |
arr[idx].time = time; |
| 91 |
return arr; |
| 92 |
})} |
| 93 |
/> |
| 94 |
); |
| 95 |
|
| 96 |
function onSubmit(event) { |
| 97 |
setSubmitted(true); |
| 98 |
|
| 99 |
if (event) |
| 100 |
event.preventDefault(); |
| 101 |
|
| 102 |
if (Object.keys(validationFailed).length) |
| 103 |
return false; |
| 104 |
|
| 105 |
create_timer({ name, description, command, delay, delayUnit, delayNumber, repeat, specificTime, repeatPatterns, owner }) |
| 106 |
.then(Dialogs.close, exc => { |
| 107 |
setDialogError(exc.message); |
| 108 |
setInProgress(false); |
| 109 |
}); |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
return ( |
| 115 |
<Modal id="timer-dialog" |
| 116 |
className="timer-dialog" position="top" |
| 117 |
variant="medium" isOpen onClose={Dialogs.close} |
| 118 |
> |
| 119 |
<ModalHeader title={!timer ? _("Create timer") : _("Edit timer")} /> |
| 120 |
<ModalBody> |
| 121 |
{dialogError && <ModalError dialogError={_("Timer creation failed")} dialogErrorDetail={dialogError} />} |
| 122 |
<Form isHorizontal onSubmit={onSubmit}> |
| 123 |
{timer && !timer.delay && <FormAlert> |
| 124 |
<Alert variant="danger" title={_("Failed to get the starting conditions for the timer")} isInline /> |
| 125 |
</FormAlert>} |
| 126 |
{timer && !timer.command && <FormAlert> |
| 127 |
<Alert variant="danger" title={_("Failed to get the timer command")} isInline /> |
| 128 |
</FormAlert>} |
| 129 |
<FormGroup label={_("Name")} |
| 130 |
fieldId="servicename"> |
| 131 |
<TextInput id='servicename' |
| 132 |
value={name} |
| 133 |
validated={submitted && validationFailed.name ? "error" : "default"} |
| 134 |
onChange={(_event, value) => setName(value)} |
| 135 |
readOnlyVariant={!!timer} /> |
| 136 |
<FormHelper fieldId="servicename" |
| 137 |
helperTextInvalid={submitted && validationFailed.name && (!name.trim().length ? _("This field cannot be empty") : _("Only alphabets, numbers, : , _ , . , @ , - are allowed"))} /> |
| 138 |
</FormGroup> |
| 139 |
<FormGroup label={_("Description")} |
| 140 |
fieldId="description"> |
| 141 |
<TextInput id='description' |
| 142 |
value={description} |
| 143 |
validated={submitted && validationFailed.description ? "error" : "default"} |
| 144 |
onChange={(_event, value) => setDescription(value)} /> |
| 145 |
<FormHelper fieldId="description" helperTextInvalid={submitted && validationFailed.description && _("This field cannot be empty")} /> |
| 146 |
</FormGroup> |
| 147 |
<FormGroup label={_("Shell command")} |
| 148 |
fieldId="command"> |
| 149 |
<TextInput id='command' |
| 150 |
value={command} |
| 151 |
validated={submitted && validationFailed.command ? "error" : "default"} |
| 152 |
onChange={(_event, str) => { setCommand(str) }} /> |
| 153 |
<FormHelper fieldId="command" |
| 154 |
helperText={_("This command will be executed by /bin/sh.")} |
| 155 |
helperTextInvalid={submitted && validationFailed.command && _("This field cannot be empty")} /> |
| 156 |
</FormGroup> |
| 157 |
<FormGroup label={_("Trigger")} hasNoPaddingTop> |
| 158 |
<Flex> |
| 159 |
<Radio value="specific-time" |
| 160 |
id="specific-time" |
| 161 |
name="boot-or-specific-time" |
| 162 |
onChange={() => setDelay("specific-time")} |
| 163 |
isChecked={delay == "specific-time"} |
| 164 |
label={_("At specific time")} /> |
| 165 |
<Radio value="system-boot" |
| 166 |
id="system-boot" |
| 167 |
name="boot-or-specific-time" |
| 168 |
onChange={() => setDelay("system-boot")} |
| 169 |
isChecked={delay == "system-boot"} |
| 170 |
label={_("After system boot")} /> |
| 171 |
</Flex> |
| 172 |
{ delay == "system-boot" && |
| 173 |
<FormGroup className="delay-group" |
| 174 |
label={_("Delay")}> |
| 175 |
<Flex> |
| 176 |
<TextInput className="delay-number" |
| 177 |
value={delayNumber} |
| 178 |
validated={submitted && validationFailed.delayNumber ? "error" : "default"} |
| 179 |
onChange={(_event, value) => setDelayNumber(value)} /> |
| 180 |
<FormSelect className="delay-unit" |
| 181 |
value={delayUnit} |
| 182 |
onChange={(_, val) => setDelayUnit(val)} |
| 183 |
aria-label={_("Delay")}> |
| 184 |
<FormSelectOption value="sec" label={_("Seconds")} /> |
| 185 |
<FormSelectOption value="min" label={_("Minutes")} /> |
| 186 |
<FormSelectOption value="hr" label={_("Hours")} /> |
| 187 |
<FormSelectOption value="weeks" label={_("Weeks")} /> |
| 188 |
</FormSelect> |
| 189 |
</Flex> |
| 190 |
<FormHelper helperTextInvalid={submitted && validationFailed.delayNumber && _("Delay must be a number")} /> |
| 191 |
</FormGroup> } |
| 192 |
{ delay == "specific-time" && |
| 193 |
<> |
| 194 |
<FormGroup label={_("Repeat")}> |
| 195 |
<FormSelect value={repeat} |
| 196 |
id="drop-repeat" |
| 197 |
onChange={(_, value) => { |
| 198 |
if (value == repeat) |
| 199 |
return; |
| 200 |
|
| 201 |
setRepeat(value); |
| 202 |
if (value == "minutely") |
| 203 |
setRepeatPatterns([{ key: 0, second: "0" }]); |
| 204 |
else if (value == "hourly") |
| 205 |
setRepeatPatterns([{ key: 0, minute: "0" }]); |
| 206 |
else if (value == "daily") |
| 207 |
setRepeatPatterns([{ key: 0, time: "00:00" }]); |
| 208 |
else if (value == "weekly") |
| 209 |
setRepeatPatterns([{ key: 0, day: "mon", time: "00:00" }]); |
| 210 |
else if (value == "monthly") |
| 211 |
setRepeatPatterns([{ key: 0, day: 1, time: "00:00" }]); |
| 212 |
else if (value == "yearly") |
| 213 |
setRepeatPatterns([{ key: 0, date: undefined, time: "00:00" }]); |
| 214 |
}} |
| 215 |
aria-label={_("Repeat")}> |
| 216 |
<FormSelectOption value="no" label={_("Don't repeat")} /> |
| 217 |
<FormSelectOption value="minutely" label={_("Minutely")} /> |
| 218 |
<FormSelectOption value="hourly" label={_("Hourly")} /> |
| 219 |
<FormSelectOption value="daily" label={_("Daily")} /> |
| 220 |
<FormSelectOption value="weekly" label={_("Weekly")} /> |
| 221 |
<FormSelectOption value="monthly" label={_("Monthly")} /> |
| 222 |
<FormSelectOption value="yearly" label={_("Yearly")} /> |
| 223 |
</FormSelect> |
| 224 |
</FormGroup> |
| 225 |
{repeat == "no" && |
| 226 |
<FormGroup label={_("Run at")}> |
| 227 |
<TimePicker className="create-timer-time-picker specific-no-repeat" |
| 228 |
isOpen={isSpecificTimeOpen} setIsOpen={setSpecificTimeOpen} |
| 229 |
menuAppendTo={() => document.body} time={specificTime} is24Hour onChange={(_, val) => setSpecificTime(val)} /> |
| 230 |
</FormGroup>} |
| 231 |
{repeatPatterns.map((item, idx) => { |
| 232 |
let label; |
| 233 |
if (repeat == "minutely") |
| 234 |
label = _("At second"); |
| 235 |
else if (repeat == "hourly") |
| 236 |
label = _("At minute"); |
| 237 |
else if (repeat == "daily") |
| 238 |
label = _("Run at"); |
| 239 |
else if (repeat == "weekly" || repeat == "monthly" || repeat == "yearly") |
| 240 |
label = _("Run on"); |
| 241 |
|
| 242 |
let helperTextInvalid; |
| 243 |
const min = repeatPatterns[idx].minute; |
| 244 |
const validationFailedMinute = !(/^[0-9]+$/.test(min) && min <= 59 && min >= 0); |
| 245 |
|
| 246 |
if (submitted && repeat == 'hourly' && validationFailedMinute) { |
| 247 |
helperTextInvalid = _("Minute needs to be a number between 0-59"); |
| 248 |
} |
| 249 |
|
| 250 |
const sec = repeatPatterns[idx].second; |
| 251 |
const validationFailedSecond = !(/^[0-9]+$/.test(sec) && sec <= 59 && sec >= 0); |
| 252 |
|
| 253 |
if (submitted && repeat == 'minutely' && validationFailedSecond) { |
| 254 |
helperTextInvalid = _("Second needs to be a number between 0-59"); |
| 255 |
} |
| 256 |
|
| 257 |
return ( |
| 258 |
<FormGroup label={label} key={item.key}> |
| 259 |
<Flex className="specific-repeat-group" data-index={idx}> |
| 260 |
{repeat == "minutely" && |
| 261 |
<TextInput className='delay-number' |
| 262 |
id={repeat} |
| 263 |
value={repeatPatterns[idx].second} |
| 264 |
onChange={(_event, second) => setRepeatPatterns(old => { |
| 265 |
const arr = [...old]; |
| 266 |
arr[idx].second = second; |
| 267 |
return arr; |
| 268 |
})} |
| 269 |
validated={submitted && validationFailedSecond ? "error" : "default"} /> |
| 270 |
} |
| 271 |
{repeat == "hourly" && |
| 272 |
<TextInput className='delay-number' |
| 273 |
value={repeatPatterns[idx].minute} |
| 274 |
onChange={(_event, minute) => setRepeatPatterns(old => { |
| 275 |
const arr = [...old]; |
| 276 |
arr[idx].minute = minute; |
| 277 |
return arr; |
| 278 |
})} |
| 279 |
validated={submitted && validationFailedMinute ? "error" : "default"} /> |
| 280 |
} |
| 281 |
{repeat == "daily" && timePicker(idx)} |
| 282 |
{repeat == "weekly" && <> |
| 283 |
<FormSelect value={repeatPatterns[idx].day} |
| 284 |
className="week-days" |
| 285 |
onChange={(_, day) => setRepeatPatterns(old => { |
| 286 |
const arr = [...old]; |
| 287 |
arr[idx].day = day; |
| 288 |
return arr; |
| 289 |
})} |
| 290 |
aria-label={_("Repeat weekly")}> |
| 291 |
<FormSelectOption value="mon" label={_("Mondays")} /> |
| 292 |
<FormSelectOption value="tue" label={_("Tuesdays")} /> |
| 293 |
<FormSelectOption value="wed" label={_("Wednesdays")} /> |
| 294 |
<FormSelectOption value="thu" label={_("Thursdays")} /> |
| 295 |
<FormSelectOption value="fri" label={_("Fridays")} /> |
| 296 |
<FormSelectOption value="sat" label={_("Saturdays")} /> |
| 297 |
<FormSelectOption value="sun" label={_("Sundays")} /> |
| 298 |
</FormSelect> |
| 299 |
{timePicker(idx)} |
| 300 |
</>} |
| 301 |
{repeat == "monthly" && <> |
| 302 |
<FormSelect value={repeatPatterns[idx].day} |
| 303 |
className="month-days" |
| 304 |
onChange={(_, day) => setRepeatPatterns(old => { |
| 305 |
const arr = [...old]; |
| 306 |
arr[idx].day = day; |
| 307 |
return arr; |
| 308 |
})} |
| 309 |
aria-label={_("Repeat monthly")}> |
| 310 |
{[_("1st"), _("2nd"), _("3rd"), _("4th"), _("5th"), |
| 311 |
_("6th"), _("7th"), _("8th"), _("9th"), _("10th"), |
| 312 |
_("11th"), _("12th"), _("13th"), _("14th"), _("15th"), |
| 313 |
_("16th"), _("17th"), _("18th"), _("19th"), _("20th"), |
| 314 |
_("21th"), _("22th"), _("23th"), _("24th"), _("25th"), |
| 315 |
_("26th"), _("27th"), _("28th"), _("29th"), _("30th"), _("31st") |
| 316 |
].map((day, index) => <FormSelectOption key={day} value={index + 1} label={day} />)} |
| 317 |
</FormSelect> |
| 318 |
{timePicker(idx)} |
| 319 |
</>} |
| 320 |
{repeat == "yearly" && <> |
| 321 |
<DatePicker aria-label={_("Pick date")} |
| 322 |
buttonAriaLabel={_("Toggle date picker")} |
| 323 |
locale={timeformat.dateFormatLang()} |
| 324 |
weekStart={timeformat.firstDayOfWeek()} |
| 325 |
onChange={(_, str, data) => setRepeatPatterns(old => { |
| 326 |
const arr = [...old]; |
| 327 |
arr[idx].date = str; |
| 328 |
return arr; |
| 329 |
})} |
| 330 |
appendTo={() => document.body} |
| 331 |
value={repeatPatterns[idx].date || ""} /> |
| 332 |
{timePicker(idx)} |
| 333 |
</>} |
| 334 |
{repeat !== "no" && <FlexItem align={{ default: 'alignRight' }}> |
| 335 |
<InputGroup> |
| 336 |
<Button icon={<MinusIcon />} aria-label={_("Remove")} |
| 337 |
variant="secondary" |
| 338 |
isDisabled={repeatPatterns.length == 1} |
| 339 |
onClick={() => setRepeatPatterns(old => old.filter((item, item_idx) => idx != item_idx))} /> |
| 340 |
<Button icon={<PlusIcon />} aria-label={_("Add")} |
| 341 |
variant="secondary" |
| 342 |
onClick={() => { |
| 343 |
if (repeat == "minutely") |
| 344 |
setRepeatPatterns(old => [...old, { key: repeatPatterns.length, second: "0" }]); |
| 345 |
else if (repeat == "hourly") |
| 346 |
setRepeatPatterns(old => [...old, { key: repeatPatterns.length, minute: "0" }]); |
| 347 |
else if (repeat == "daily") |
| 348 |
setRepeatPatterns(old => [...old, { key: repeatPatterns.length, time: "00:00" }]); |
| 349 |
else if (repeat == "weekly") |
| 350 |
setRepeatPatterns(old => [...old, { key: repeatPatterns.length, day: "mon", time: "00:00" }]); |
| 351 |
else if (repeat == "monthly") |
| 352 |
setRepeatPatterns(old => [...old, { key: repeatPatterns.length, day: 1, time: "00:00" }]); |
| 353 |
else if (repeat == "yearly") |
| 354 |
setRepeatPatterns(old => [...old, { key: repeatPatterns.length, date: undefined, time: "00:00" }]); |
| 355 |
}} /> |
| 356 |
</InputGroup> |
| 357 |
</FlexItem>} |
| 358 |
</Flex> |
| 359 |
<FormHelper helperTextInvalid={helperTextInvalid} /> |
| 360 |
</FormGroup> |
| 361 |
); |
| 362 |
})} |
| 363 |
</>} |
| 364 |
</FormGroup> |
| 365 |
</Form> |
| 366 |
</ModalBody> |
| 367 |
<ModalFooter> |
| 368 |
<Button variant='primary' |
| 369 |
id="timer-save-button" |
| 370 |
isLoading={inProgress} |
| 371 |
isDisabled={inProgress} |
| 372 |
onClick={onSubmit}> |
| 373 |
{_("Save")} |
| 374 |
</Button> |
| 375 |
<Button id="timer-dialog-close-button" variant='link' onClick={Dialogs.close}> |
| 376 |
{_("Cancel")} |
| 377 |
</Button> |
| 378 |
</ModalFooter> |
| 379 |
</Modal> |
| 380 |
); |
| 381 |
}; |