Skip to content

Commit

Permalink
fix(kms): keep data after unmount component on credential creation
Browse files Browse the repository at this point in the history
ref: MANAGER-15319

Signed-off-by: Vincent BONMARCHAND <[email protected]>
  • Loading branch information
vovh committed Oct 10, 2024
1 parent 9158906 commit c47851b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const CreateCredential = () => {
const [csr, setCsr] = useState<string | null>(null);
const [identityURNs, setIdentityURNs] = useState<string[]>([]);
const [okmsCredential, setOkmsCredential] = useState<OkmsCredential>();
const [isCustomCsr, setIsCustomCsr] = useState<boolean>(false);
const { createKmsCredential } = useCreateOkmsCredential({
okmsId,
onSuccess: (credential) => {
Expand Down Expand Up @@ -102,6 +103,8 @@ const CreateCredential = () => {
setDescription={setDescription}
csr={csr}
setCsr={setCsr}
isCustomCsr={isCustomCsr}
setIsCustomCsr={setIsCustomCsr}
nextStep={() => setStep(2)}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type CreateGeneralInformationsProps = {
setDescription: Dispatch<SetStateAction<string | null>>;
csr: string | null;
setCsr: Dispatch<SetStateAction<string | null>>;
isCustomCsr: boolean;
setIsCustomCsr: Dispatch<SetStateAction<boolean>>;
nextStep: () => void;
};

Expand All @@ -37,6 +39,8 @@ const CreateGeneralInformations = ({
setDescription,
csr,
setCsr,
isCustomCsr,
setIsCustomCsr,
nextStep,
}: CreateGeneralInformationsProps) => {
const { t } = useTranslation('key-management-service/credential');
Expand All @@ -46,7 +50,6 @@ const CreateGeneralInformations = ({
const credentialDescriptionError = validateCredentialDescription(description);
const credentialValidityError = validateValidityDate(validity);
const credentialCreationMethodError = validateCredentialCreationMethod(csr);
const [isCustomCsr, setIsCustomCsr] = useState<boolean>(false);

useEffect(() => {
if (!isCustomCsr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { VALIDITY_PERIOD_PRESET } from '../CreateGeneralInformations.constants';
import { ValidityPeriodErrorsType } from '@/utils/credential/validateValidityDate';
import {
addDaysToDate,
getDateFromDays,
getDaysFromDate,
getNextMonth,
} from '@/utils/credential/validityDateUtils';

type CreateGeneralInformationsValidityProps = {
Expand All @@ -30,9 +30,15 @@ const CreateGeneralInformationsValidity = ({
credentialValidityError,
}: CreateGeneralInformationsValidityProps) => {
const { t } = useTranslation('key-management-service/credential');
const [validityPresetPeriod, setValidityPresetPeriod] = useState(validity);

const getPresetForDays = (days: number): number =>
VALIDITY_PERIOD_PRESET.find((preset) => preset.days === days)?.days ?? -1;

const [validityPresetPeriod, setValidityPresetPeriod] = useState(
getPresetForDays(validity),
);
const [validityDatepicker, setValidityDatepicker] = useState<Date>(
getNextMonth(),
getDateFromDays(validity),
);

const getValidityErrorMessage = (error: ValidityPeriodErrorsType) => {
Expand All @@ -50,6 +56,7 @@ const CreateGeneralInformationsValidity = ({
return null;
}
};

useEffect(() => {
if (validityPresetPeriod !== -1) {
setValidity(validityPresetPeriod);
Expand All @@ -58,6 +65,7 @@ const CreateGeneralInformationsValidity = ({
setValidity(getDaysFromDate(validityDatepicker));
}
}, [validityPresetPeriod, validityDatepicker]);

return (
<div className="flex flex-col gap-5 md:gap-6">
<CommonTitle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getNextMonth,
getDaysFromDate,
addDaysToDate,
getDateFromDays,
} from './validityDateUtils';

describe('getNextMonth', () => {
Expand Down Expand Up @@ -59,8 +60,8 @@ describe('getDaysFromDate', () => {
});

it('should handle leap years correctly', () => {
const referenceDate = new Date(2024, 1, 28); // 28 février 2024
const leapDay = new Date(2024, 1, 29); // 29 février 2024
const referenceDate = new Date(2024, 1, 28);
const leapDay = new Date(2024, 1, 29);

const diffInDays = Math.floor(
(leapDay.getTime() - referenceDate.getTime()) / (24 * 60 * 60 * 1000),
Expand All @@ -84,9 +85,59 @@ describe('addDaysToDate', () => {
});

it('should handle leap years correctly', () => {
const result = addDaysToDate(365); // Ajoute 365 jours à une année bissextile
const result = addDaysToDate(365);
const expectedDate = new Date();
expectedDate.setFullYear(expectedDate.getFullYear() + 1);
expect(result.getUTCFullYear()).toBe(expectedDate.getUTCFullYear());
});
});

describe('getDateFromDays', () => {
it('should return the correct date when adding positive days', () => {
const today = new Date();
const daysToAdd = 10;
const futureDate = getDateFromDays(daysToAdd);

const expectedDate = new Date(today);
expectedDate.setDate(today.getDate() + daysToAdd);

expect(futureDate.getUTCDate()).toBe(expectedDate.getUTCDate());
expect(futureDate.getUTCMonth()).toBe(expectedDate.getUTCMonth());
expect(futureDate.getUTCFullYear()).toBe(expectedDate.getUTCFullYear());
});

it('should return the correct date when adding negative days', () => {
const today = new Date();
const daysToSubtract = -10;
const pastDate = getDateFromDays(daysToSubtract);

const expectedDate = new Date(today);
expectedDate.setDate(today.getDate() + daysToSubtract);

expect(pastDate.getUTCDate()).toBe(expectedDate.getUTCDate());
expect(pastDate.getUTCMonth()).toBe(expectedDate.getUTCMonth());
expect(pastDate.getUTCFullYear()).toBe(expectedDate.getUTCFullYear());
});

it("should return today's date when adding 0 days", () => {
const today = new Date();
const result = getDateFromDays(0);

expect(result.getUTCDate()).toBe(today.getUTCDate());
expect(result.getUTCMonth()).toBe(today.getUTCMonth());
expect(result.getUTCFullYear()).toBe(today.getUTCFullYear());
});

it('should correctly handle large numbers of days', () => {
const today = new Date();
const daysToAdd = 3650; // 10 years
const futureDate = getDateFromDays(daysToAdd);

const expectedDate = new Date(today);
expectedDate.setDate(today.getDate() + daysToAdd);

expect(futureDate.getUTCDate()).toBe(expectedDate.getUTCDate());
expect(futureDate.getUTCMonth()).toBe(expectedDate.getUTCMonth());
expect(futureDate.getUTCFullYear()).toBe(expectedDate.getUTCFullYear());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ export function getDaysFromDate(date: Date): number {
);
return Math.floor(diffInTime / oneDay);
}

export function getDateFromDays(days: number): Date {
const referenceDate = new Date();
const oneDay = 24 * 60 * 60 * 1000;
const futureDate = new Date(referenceDate.getTime() + days * oneDay);
return futureDate;
}

0 comments on commit c47851b

Please sign in to comment.