Fix retention date on object UI (#556)
Co-authored-by: Daniel Valdivia <hola@danielvaldivia.com>
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -27,6 +27,7 @@ import FormSwitchWrapper from "../../../../Common/FormComponents/FormSwitchWrapp
|
|||||||
import RadioGroupSelector from "../../../../Common/FormComponents/RadioGroupSelector/RadioGroupSelector";
|
import RadioGroupSelector from "../../../../Common/FormComponents/RadioGroupSelector/RadioGroupSelector";
|
||||||
import DateSelector from "../../../../Common/FormComponents/DateSelector/DateSelector";
|
import DateSelector from "../../../../Common/FormComponents/DateSelector/DateSelector";
|
||||||
import api from "../../../../../../common/api";
|
import api from "../../../../../../common/api";
|
||||||
|
import { twoDigitDate } from "../../../../Common/FormComponents/DateSelector/utils";
|
||||||
|
|
||||||
const styles = (theme: Theme) =>
|
const styles = (theme: Theme) =>
|
||||||
createStyles({
|
createStyles({
|
||||||
@@ -76,6 +77,19 @@ const SetRetention = ({
|
|||||||
setType(objectInfo.retention_mode.toLowerCase());
|
setType(objectInfo.retention_mode.toLowerCase());
|
||||||
setAlreadyConfigured(true);
|
setAlreadyConfigured(true);
|
||||||
}
|
}
|
||||||
|
// get retention_until_date if defined
|
||||||
|
if (objectInfo.retention_until_date) {
|
||||||
|
const valueDate = new Date(objectInfo.retention_until_date);
|
||||||
|
if (valueDate.toString() !== "Invalid Date") {
|
||||||
|
const year = valueDate.getFullYear();
|
||||||
|
const month = twoDigitDate(valueDate.getMonth() + 1);
|
||||||
|
const day = valueDate.getDate();
|
||||||
|
if (!isNaN(day) && month !== "NaN" && !isNaN(year)) {
|
||||||
|
setDate(`${year}-${month}-${day}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setAlreadyConfigured(true);
|
||||||
|
}
|
||||||
}, [objectInfo]);
|
}, [objectInfo]);
|
||||||
|
|
||||||
const dateElement = useRef<IRefObject>(null);
|
const dateElement = useRef<IRefObject>(null);
|
||||||
@@ -217,10 +231,13 @@ const SetRetention = ({
|
|||||||
label="Date"
|
label="Date"
|
||||||
disableOptions={dateFieldDisabled()}
|
disableOptions={dateFieldDisabled()}
|
||||||
ref={dateElement}
|
ref={dateElement}
|
||||||
|
value={date}
|
||||||
borderBottom={true}
|
borderBottom={true}
|
||||||
onDateChange={(date: string, isValid: boolean) => {
|
onDateChange={(date: string, isValid: boolean) => {
|
||||||
setIsDateValid(isValid);
|
setIsDateValid(isValid);
|
||||||
setDate(date);
|
if (isValid) {
|
||||||
|
setDate(date);
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ interface IDateSelectorProps {
|
|||||||
addSwitch?: boolean;
|
addSwitch?: boolean;
|
||||||
tooltip?: string;
|
tooltip?: string;
|
||||||
borderBottom?: boolean;
|
borderBottom?: boolean;
|
||||||
|
value?: string;
|
||||||
onDateChange: (date: string, isValid: boolean) => any;
|
onDateChange: (date: string, isValid: boolean) => any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,6 +101,7 @@ const DateSelector = forwardRef(
|
|||||||
tooltip = "",
|
tooltip = "",
|
||||||
borderBottom = false,
|
borderBottom = false,
|
||||||
onDateChange,
|
onDateChange,
|
||||||
|
value = "",
|
||||||
}: IDateSelectorProps,
|
}: IDateSelectorProps,
|
||||||
ref: any
|
ref: any
|
||||||
) => {
|
) => {
|
||||||
@@ -110,6 +112,18 @@ const DateSelector = forwardRef(
|
|||||||
const [day, setDay] = useState<string>("");
|
const [day, setDay] = useState<string>("");
|
||||||
const [year, setYear] = useState<string>("");
|
const [year, setYear] = useState<string>("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// verify if there is a current value
|
||||||
|
// assume is in the format "2021-12-30"
|
||||||
|
if (value !== "") {
|
||||||
|
const valueSplit = value.split("-");
|
||||||
|
setYear(valueSplit[0]);
|
||||||
|
setMonth(valueSplit[1]);
|
||||||
|
// Turn to single digit to be displayed on dropdown buttons
|
||||||
|
setDay(`${parseInt(valueSplit[2])}`);
|
||||||
|
}
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const [isValid, dateString] = validDate(year, month, day);
|
const [isValid, dateString] = validDate(year, month, day);
|
||||||
onDateChange(dateString, isValid);
|
onDateChange(dateString, isValid);
|
||||||
|
|||||||
@@ -56,3 +56,9 @@ export const validDate = (year: string, month: string, day: string): any[] => {
|
|||||||
|
|
||||||
return [parsedDate === dateString, dateString];
|
return [parsedDate === dateString, dateString];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// twoDigitDate gets a two digit string number used for months or days
|
||||||
|
// returns "NaN" if number is NaN
|
||||||
|
export const twoDigitDate = (num: number): string => {
|
||||||
|
return num < 10 ? `0${num}` : `${num}`;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user