Skip to content

Commit

Permalink
WIP: Reuse DateTimeSelector on IpaCalendar
Browse files Browse the repository at this point in the history
Signed-off-by: Carla Martinez <carlmart@redhat.com>
  • Loading branch information
carma12 committed Jan 11, 2024
1 parent 3d328f9 commit 5914a0d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 114 deletions.
26 changes: 23 additions & 3 deletions src/components/Form/DateTimeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import {
parseFullDateStringToUTCFormat,
toGeneralizedTime,
} from "src/utils/utils";
import { IPAObject, updateIpaObject } from "src/utils/ipaObjectUtils";

interface PropsToDateTimeSelector {
timeValue: string; // Generalized time format ('YYYYMMDDHHMMSSZ')
setTimeValue: (timeValue: string) => void;
setTimeValue?: (timeValue: string) => void;
name: string;
ariaLabel?: string;
isDisabled?: boolean;
ipaObject?: Record<string, unknown>;
onChange?: (ipaObject: IPAObject) => void;
}

const DateTimeSelector = (props: PropsToDateTimeSelector) => {
Expand Down Expand Up @@ -54,7 +57,15 @@ const DateTimeSelector = (props: PropsToDateTimeSelector) => {
) {
setValueDate(newFromDate);
// Parse to generalized format
props.setTimeValue(toGeneralizedTime(newFromDate));
if (props.setTimeValue !== undefined) {
props.setTimeValue(toGeneralizedTime(newFromDate));
}
}
// Update 'ipaObject' with the new date
// - Parse to generalized format (to return to the "user_mod" API call)
if (props.ipaObject !== undefined && props.onChange !== undefined) {
const LDAPDate: string = toGeneralizedTime(newFromDate);
updateIpaObject(props.ipaObject, props.onChange, LDAPDate, props.name);
}
}
};
Expand All @@ -70,7 +81,16 @@ const DateTimeSelector = (props: PropsToDateTimeSelector) => {

setValueDate(updatedFromDate);
// Parse to generalized format
props.setTimeValue(toGeneralizedTime(updatedFromDate));
if (props.setTimeValue !== undefined) {
props.setTimeValue(toGeneralizedTime(updatedFromDate));
}

// Update 'ipaObject' with the new date
// - Parse to generalized format (to return to the "user_mod" API call)
if (props.ipaObject !== undefined && props.onChange !== undefined) {
const LDAPDate = toGeneralizedTime(updatedFromDate);
updateIpaObject(props.ipaObject, props.onChange, LDAPDate, props.name);
}
};

// Parse the current date into 'HH:MM' format
Expand Down
126 changes: 15 additions & 111 deletions src/components/Form/IpaCalendar.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import React from "react";
// PatternFly
import {
DatePicker,
InputGroup,
TimePicker,
isValidDate,
InputGroupItem,
} from "@patternfly/react-core";
// Utils
import {
parseFullDateStringToUTCFormat,
Expand All @@ -17,9 +9,10 @@ import {
IPAParamDefinition,
ParamProperties,
getParamProperties,
updateIpaObject,
} from "src/utils/ipaObjectUtils";
import { ParamMetadata } from "src/utils/datatypes/globalDataTypes";
// Components
import DateTimeSelector from "./DateTimeSelector";

export interface DateParam {
__datetime__: string;
Expand Down Expand Up @@ -62,113 +55,24 @@ function getParamPropertiesDateTime(
};
}

function cloneDate(date: Date): Date {
return parseFullDateStringToUTCFormat(toGeneralizedTime(date)) as Date;
}

const IpaCalendar = (props: IPAParamDefinition) => {
const { readOnly, value } = getParamPropertiesDateTime(props);

// On change date handler
const onDateChange = (
_event: React.FormEvent<HTMLInputElement>,
inputDate: string,
newFromDate: Date | undefined
) => {
if (newFromDate !== undefined) {
if (
isValidDate(newFromDate) &&
inputDate === yyyyMMddFormat(newFromDate)
) {
if (value) {
newFromDate.setHours(value.getHours());
newFromDate.setMinutes(value.getMinutes());
}
// Update 'ipaObject' with the new date
// - Parse to generalized format (to return to the "user_mod" API call)
if (props.ipaObject !== undefined && props.onChange !== undefined) {
const LDAPDate: string = toGeneralizedTime(newFromDate);
updateIpaObject(
props.ipaObject,
props.onChange,
LDAPDate,
props.name
);
}
}
}
};

// On change time handler
const onTimeChange = (_event, time, hour, minute) => {
// Assume inital data is null
// If the date is empty, create a new one with the current time
let updatedFromDate: Date = new Date();
if (value && isValidDate(value)) {
updatedFromDate = cloneDate(value);
}
updatedFromDate.setHours(hour);
updatedFromDate.setMinutes(minute);

// Update 'ipaObject' with the new date
// - Parse to generalized format (to return to the "user_mod" API call)
if (props.ipaObject !== undefined && props.onChange !== undefined) {
const LDAPDate = toGeneralizedTime(updatedFromDate);
updateIpaObject(props.ipaObject, props.onChange, LDAPDate, props.name);
}
};

// Parse the current date into 'YYYY-MM-DD' format
const yyyyMMddFormat = (date: Date | null | undefined): string => {
if (date === undefined || date === null) return "";

// This convertion is needed to prevent any Date data type issues
const dt = new Date(date);
const year = dt.getFullYear();
const month = dt.getMonth() + 1;
const day = dt.getDate().toString().padStart(2, "0");

const res = year.toString() + "-" + month.toString() + "-" + day;
return res;
};

// Parse the current date into 'HH:MM' format
const hhMMFormat = (date: Date | null | undefined): string => {
if (date === undefined || date === null) return "";

// This convertion is needed to prevent any Date data type issues
const dt = new Date(date);
const hours = dt.getHours().toString().padStart(2, "0");
const minutes = dt.getMinutes().toString().padStart(2, "0");

const res = hours + ":" + minutes;
return res;
};
// Date-time selectors
const [time, setTime] = React.useState<string>(
value ? (toGeneralizedTime(value) as string) : ""
);

return (
<InputGroup>
<InputGroupItem>
<DatePicker
name={"add-date-" + props.name}
value={yyyyMMddFormat(value)}
onChange={onDateChange}
aria-label="Kerberos principal expiration date"
placeholder="YYYY-MM-DD"
isDisabled={readOnly}
/>
</InputGroupItem>
<InputGroupItem>
<TimePicker
name={"add-time-" + props.name}
time={hhMMFormat(value)}
aria-label="Kerberos principal expiration time"
onChange={onTimeChange}
placeholder="HH:MM"
is24Hour={true}
isDisabled={readOnly}
/>
</InputGroupItem>
</InputGroup>
<DateTimeSelector
timeValue={time}
setTimeValue={setTime}
name={props.name}
ariaLabel="Kerberos principal expiration date"
isDisabled={readOnly}
ipaObject={props.ipaObject}
onChange={props.onChange}
/>
);
};

Expand Down

0 comments on commit 5914a0d

Please sign in to comment.