Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support errors
  • Loading branch information
BrunoQuaresma committed May 9, 2024
commit eef159cf5d2b1c2f1336a6c8b238f7164adb16e2
25 changes: 14 additions & 11 deletions site/src/components/DurationField/DurationField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ import KeyboardArrowDown from "@mui/icons-material/KeyboardArrowDown";
import FormHelperText from "@mui/material/FormHelperText";
import MenuItem from "@mui/material/MenuItem";
import Select from "@mui/material/Select";
import TextField from "@mui/material/TextField";
import { type ReactNode, useState, type FC, useEffect } from "react";
import TextField, { type TextFieldProps } from "@mui/material/TextField";
import { useState, type FC, useEffect } from "react";
import {
type TimeUnit,
durationInDays,
durationInHours,
suggestedTimeUnit,
} from "utils/time";

type DurationFieldProps = {
label: string;
type DurationFieldProps = Omit<TextFieldProps, "value" | "onChange"> & {
// Value is in ms
value: number;
disabled?: boolean;
helperText?: ReactNode;
onChange: (value: number) => void;
};

Expand All @@ -28,7 +25,7 @@ type State = {
};

export const DurationField: FC<DurationFieldProps> = (props) => {
const { label, value: parentValue, disabled, helperText, onChange } = props;
const { value: parentValue, onChange, helperText, ...textFieldProps } = props;
const [state, setState] = useState<State>(() => initState(parentValue));
const currentDurationInMs = durationInMs(
state.durationFieldValue,
Expand All @@ -50,10 +47,9 @@ export const DurationField: FC<DurationFieldProps> = (props) => {
}}
>
<TextField
{...textFieldProps}
type="number"
css={{ maxWidth: 160 }}
label={label}
disabled={disabled}
value={state.durationFieldValue}
onChange={(e) => {
const durationFieldValue = e.currentTarget.value;
Expand All @@ -76,7 +72,7 @@ export const DurationField: FC<DurationFieldProps> = (props) => {
}}
/>
<Select
disabled={disabled}
disabled={props.disabled}
css={{ width: 120, "& .MuiSelect-icon": { padding: 2 } }}
value={state.unit}
onChange={(e) => {
Expand Down Expand Up @@ -107,7 +103,9 @@ export const DurationField: FC<DurationFieldProps> = (props) => {
</Select>
</div>

{helperText && <FormHelperText>{helperText}</FormHelperText>}
{helperText && (
<FormHelperText error={props.error}>{helperText}</FormHelperText>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason why props.error wasn't destructured from the props at the top of the component?

Copy link
Collaborator Author

@BrunoQuaresma BrunoQuaresma May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is used by the TextField props as well.

)}
</div>
);
};
Expand All @@ -127,6 +125,11 @@ function initState(value: number): State {

function durationInMs(durationFieldValue: string, unit: TimeUnit): number {
const durationInMs = parseInt(durationFieldValue);

if (Number.isNaN(durationInMs)) {
return 0;
}

return unit === "hours"
? hoursToDuration(durationInMs)
: daysToDuration(durationInMs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,14 @@ export const TemplateScheduleForm: FC<TemplateScheduleForm> = ({
/>

<DurationField
{...getFieldHelpers("time_til_dormant_ms", {
helperText: (
<DormancyTTLHelperText
ttl={form.values.time_til_dormant_ms}
/>
),
})}
label="Time until dormant"
helperText={
<DormancyTTLHelperText
ttl={form.values.time_til_dormant_ms}
/>
}
value={form.values.time_til_dormant_ms ?? 0}
onChange={(v) => form.setFieldValue("time_til_dormant_ms", v)}
disabled={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export const getValidationSchema = (): Yup.AnyObjectSchema =>
time_til_dormant_ms: Yup.number()
.integer()
.required()
.min(0, "Dormancy threshold days must not be less than 0.")
.min(0, "Dormancy threshold must not be less than 0.")
.test(
"positive-if-enabled",
"Dormancy threshold days must be greater than zero when enabled.",
"Dormancy threshold must be greater than zero when enabled.",
function (value) {
const parent = this.parent as TemplateScheduleFormValues;
if (parent.inactivity_cleanup_enabled) {
Expand Down