Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kms): refacto delete modal #14351

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,42 @@ import {
ODS_ICON_SIZE,
ODS_TEXT_COLOR_INTENT,
} from '@ovhcloud/ods-components';
import { Outlet, useNavigate } from 'react-router-dom';
import { OsdsChip, OsdsIcon, OsdsLink } from '@ovhcloud/ods-components/react';
import React, { useContext, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
ButtonType,
PageLocation,
PageType,
ShellContext,
useOvhTracking,
} from '@ovh-ux/manager-react-shell-client';
import { OKMS } from '@/types/okms.type';
import { useTerminateOKms } from '@/data/hooks/useTerminateOKms';
import { TerminateModal } from '@/components/Modal/terminate/TerminateModal.component';
import { OkmsServiceState } from '../okmsServiceState/OkmsServiceState.component';
import { Tile } from '@/components/dashboard/tile/tile.component';
import { TileItem } from '@/components/dashboard/tile-item/tileItem.component';
import { TileValue } from '@/components/dashboard/tile-value/tileValue.component';
import { TileSeparator } from '@/components/dashboard/tile-separator/tileSeparator';
import { TileValueDate } from '@/components/dashboard/tile-value-date/tileValueDate.component';
import { DISPLAY_CONTACTS_MANAGEMENT_KEY } from './BillingInformationsTile.constants';
import { ROUTES_URLS } from '@/routes/routes.constants';

type BillingInformationsTileProps = {
okmsData?: OKMS;
okmsService?: ServiceDetails;
};

const BillingInformationsTile = ({
okmsData,
okmsService,
}: BillingInformationsTileProps) => {
const { t } = useTranslation('key-management-service/dashboard');
const { trackClick, trackPage } = useOvhTracking();
const navigate = useNavigate();
const { trackClick } = useOvhTracking();
const [contactUrl, setContactUrl] = useState('');
const [showTerminationModal, setShowTerminationModal] = useState(false);
const { data: availability } = useFeatureAvailability([
DISPLAY_CONTACTS_MANAGEMENT_KEY,
]);
const {
shell: { navigation },
} = useContext(ShellContext);
const closeTerminateModal = () => {
setShowTerminationModal(false);
};

const { terminateKms, isPending } = useTerminateOKms({
okmsId: okmsData.id,
onSuccess: () => {
trackPage({
pageType: PageType.bannerSuccess,
pageName: 'delete_kms_success',
});
closeTerminateModal();
},
onError: () => {
trackPage({
pageType: PageType.bannerError,
pageName: 'delete_kms_error',
});
closeTerminateModal();
},
});

const dateFormat: Intl.DateTimeFormatOptions = {
day: '2-digit',
Expand All @@ -90,7 +65,7 @@ const BillingInformationsTile = ({
actionType: 'navigation',
actions: ['delete_kms'],
});
setShowTerminationModal(true);
navigate(ROUTES_URLS.terminateOkms);
},
},
];
Expand Down Expand Up @@ -212,13 +187,7 @@ const BillingInformationsTile = ({
</>
)}
</Tile>
{showTerminationModal && (
<TerminateModal
onConfirmTerminate={terminateKms}
closeModal={closeTerminateModal}
isLoading={isPending}
/>
)}
<Outlet />
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ApiError, ApiResponse } from '@ovh-ux/manager-core-api';
import { useNotifications } from '@ovh-ux/manager-react-components';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import {
MutationOptions,
useMutation,
useQueryClient,
} from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { getOkmsServicesResourceListQueryKey } from '../api/okms';
import {
Expand All @@ -10,15 +14,14 @@ import {
terminateOKmsQueryKey,
} from '../api/okmsService';

export type UseTerminateOkmsParams = {
okmsId: string;
} & MutationOptions<ApiResponse<{ message: string }>, ApiError>;

export const useTerminateOKms = ({
okmsId,
onSuccess,
onError,
}: {
okmsId: string;
onSuccess?: () => void;
onError?: () => void;
}) => {
...options
}: UseTerminateOkmsParams) => {
const queryClient = useQueryClient();
const { addError, addSuccess, clearNotifications } = useNotifications();
const { t } = useTranslation('key-management-service/terminate');
Expand All @@ -34,7 +37,7 @@ export const useTerminateOKms = ({
});
return terminateOKms({ serviceId: servicesId[0] });
},
onSuccess: () => {
onSuccess: (...params) => {
clearNotifications();
addSuccess(
t('key_management_service_terminate_success_banner', {
Expand All @@ -45,18 +48,19 @@ export const useTerminateOKms = ({
queryClient.invalidateQueries({
queryKey: getOkmsServicesResourceListQueryKey,
});
onSuccess?.();
options?.onSuccess?.(...params);
},
onError: (result: ApiError) => {
onError: (result: ApiError, ...params) => {
clearNotifications();
addError(
t('key_management_service_terminate_error', {
error: result.message,
}),
true,
);
onError?.();
options?.onError?.(result, ...params);
},
...options,
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { act, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderTestApp } from '@/utils/tests/renderTestApp';
import '@testing-library/jest-dom';
import { labels } from '@/utils/tests/init.i18n';
import { okmsMock } from '@/mocks/kms/okms.mock';
import '@testing-library/jest-dom';

describe('KMS dashboard test suite', () => {
it('should display an error if the API is KO', async () => {
Expand Down Expand Up @@ -134,12 +134,12 @@ describe('KMS dashboard test suite', () => {
await waitFor(
() =>
expect(
screen.getByText(
screen.getAllByText(
labels.serviceKeys[
'key_management_service_service-keys_dashboard_field_name'
],
),
).toBeVisible(),
).toHaveLength(2),
{
timeout: 30_000,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ function GeneralInformationsTab() {
okmsData={okms.data}
okmsServiceInfos={okmsService.data}
/>
<BillingInformationsTile
okmsData={okms.data}
okmsService={okmsService.data}
/>
<BillingInformationsTile okmsService={okmsService.data} />
</div>
);
}
Expand Down
Loading
Loading