Skip to content

Commit d8dc2f7

Browse files
committed
feat(manager-react-component): add service state badge component
Signed-off-by: Thibault Barske <[email protected]>
1 parent c8ff2f5 commit d8dc2f7

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

.github/workflows/run-bdd-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
yarn exec turbo -- run build --filter="./packages/manager/core/*" --concurrency=5
2424
yarn exec turbo -- run build --filter="./packages/manager-react-components"
2525
yarn exec turbo -- run build --filter="./packages/manager/modules/order"
26+
yarn exec turbo -- run build --filter="./packages/manager/modules/common-translations"
2627
yarn exec turbo -- run build --filter="./packages/manager/modules/common-api"
2728
yarn exec turbo -- run build --filter="./packages/manager/modules/vcd-api"
2829
yarn exec turbo -- run build --filter="./packages/manager/modules/manager-pci-common"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fr_FR from '@ovh-ux/manager-common-translations/dist/@ovh-ux/manager-common-translations/service/Messages_fr_FR.json';
2+
import { ResourceStatus } from '@ovh-ux/manager-module-common-api';
3+
import React from 'react';
4+
import { vitest } from 'vitest';
5+
import { render } from '../../utils/test.provider';
6+
import { ServiceStateBadge } from './ServiceStateBadge';
7+
8+
vitest.mock('../../hooks/iam');
9+
10+
const renderComponent = (
11+
props: React.ComponentProps<typeof ServiceStateBadge>,
12+
) => {
13+
return render(<ServiceStateBadge {...props} data-testid="badge" />);
14+
};
15+
16+
describe('should display manager state with the good color', () => {
17+
it.each([
18+
{ state: 'active', label: fr_FR.service_state_active, color: 'success' },
19+
{ state: 'deleted', label: fr_FR.service_state_deleted, color: 'critical' },
20+
{ state: 'unknown', label: 'unknown', color: 'information' },
21+
])(
22+
`should display manager $state badge for $color`,
23+
({ state, label, color }) => {
24+
const container = renderComponent({ state: state as ResourceStatus });
25+
const badge = container.getByTestId('badge');
26+
expect(badge).toBeDefined();
27+
expect(badge.getAttribute('label')).toBe(label);
28+
expect(badge.getAttribute('color')).toBe(color);
29+
},
30+
);
31+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { Meta } from '@storybook/react';
3+
import { ServiceStateBadge } from './ServiceStateBadge';
4+
import { ResourceStatus } from '../../hooks/services/services.type';
5+
6+
export const ServiceStateBadgeActive = () => (
7+
<ServiceStateBadge state="active" />
8+
);
9+
10+
export const ServiceStateBadgeDeleted = () => (
11+
<ServiceStateBadge state="deleted" />
12+
);
13+
14+
export const ServiceStateBadgeSuspended = () => (
15+
<ServiceStateBadge state="suspended" />
16+
);
17+
18+
export const ServiceStateBadgeToActivate = () => (
19+
<ServiceStateBadge state="toActivate" />
20+
);
21+
22+
export const ServiceStateBadgeToDelete = () => (
23+
<ServiceStateBadge state="toDelete" />
24+
);
25+
26+
export const ServiceStateBadgeToSuspend = () => (
27+
<ServiceStateBadge state="toSuspend" />
28+
);
29+
30+
export const ServiceStateBadgeUnknown = () => (
31+
<ServiceStateBadge state={'unknown' as unknown as ResourceStatus} />
32+
);
33+
34+
const meta: Meta<typeof ServiceStateBadge> = {
35+
title: 'Components/ServiceStateBadge',
36+
component: ServiceStateBadge,
37+
argTypes: {
38+
state: {
39+
control: { type: 'select' },
40+
options: [
41+
'active',
42+
'deleted',
43+
'suspended',
44+
'toActivate',
45+
'toDelete',
46+
'toSuspend',
47+
'unknown',
48+
],
49+
description: 'Select the state of the service.',
50+
},
51+
},
52+
args: {
53+
state: 'active',
54+
},
55+
decorators: [],
56+
};
57+
58+
export default meta;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import './translations';
2+
import React from 'react';
3+
import { NAMESPACES } from '@ovh-ux/manager-common-translations';
4+
import { useTranslation } from 'react-i18next';
5+
import { OdsBadgeColor } from '@ovhcloud/ods-components';
6+
import { OdsBadge } from '@ovhcloud/ods-components/react';
7+
import { ResourceStatus } from '../../hooks/services/services.type';
8+
9+
export type ServiceStateBadgeProps = Omit<
10+
React.ComponentProps<typeof OdsBadge>,
11+
'color' | 'label'
12+
> & {
13+
state: ResourceStatus;
14+
};
15+
16+
const stateConfig: Record<
17+
ResourceStatus,
18+
{ label: string; color: OdsBadgeColor }
19+
> = {
20+
active: { label: 'service_state_active', color: 'success' },
21+
deleted: { label: 'service_state_deleted', color: 'critical' },
22+
suspended: { label: 'service_state_suspended', color: 'warning' },
23+
toActivate: { label: 'service_state_toActivate', color: 'information' },
24+
toDelete: { label: 'service_state_toDelete', color: 'information' },
25+
toSuspend: { label: 'service_state_toSuspend', color: 'information' },
26+
};
27+
28+
export const ServiceStateBadge = ({
29+
state,
30+
...rest
31+
}: ServiceStateBadgeProps) => {
32+
const { t } = useTranslation(NAMESPACES.SERVICE);
33+
34+
const { label, color } = stateConfig[state] ?? {
35+
label: state,
36+
color: 'information',
37+
};
38+
39+
return <OdsBadge label={t(label)} color={color} {...rest}></OdsBadge>;
40+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NAMESPACES } from '@ovh-ux/manager-common-translations';
2+
import i18next from 'i18next';
3+
import service_fr_FR from '@ovh-ux/manager-common-translations/dist/@ovh-ux/manager-common-translations/service/Messages_fr_FR.json';
4+
5+
function addTranslations() {
6+
i18next.addResources('fr_FR', NAMESPACES.SERVICE, service_fr_FR);
7+
}
8+
9+
if (i18next.isInitialized) {
10+
addTranslations();
11+
} else {
12+
i18next.on('initialized', addTranslations);
13+
}

0 commit comments

Comments
 (0)