/*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import { Alert, KeyValuePair, Stack, StatusIndicator } from 'aws-northstar';
import { CustomComponentTypes } from '$common/components';
import { CustomValidatorTypes } from '$common/components/form-renderer/validators';
import { DefaultGroupIds } from '@ada/common';
import { LL, useI18nContext } from '$strings';
import { TagGroup, WizardStep } from '$northstar-plus';
import { apiHooks } from '$api';
import { componentTypes } from 'aws-northstar/components/FormRenderer/types';
import { isEmpty, startCase } from 'lodash';
import { sortApiAccessPolicyIds } from '$common/entity/api-access-policy';
import { validatorTypes } from 'aws-northstar/components/FormRenderer';
import React, { useCallback } from 'react';
import type { Group } from '@ada/api';
export type FormData = Group;
export const buildSteps = (groupId: string | undefined): WizardStep[] => {
const isExisting = groupId != null;
return [
{
title: !isExisting ? LL.ENTITY.Group__CREATE() : LL.ENTITY.Group__UPDATE(groupId),
description: LL.VIEW.GROUP.wizard.description(),
fields: [
{
component: CustomComponentTypes.ENTITY_NAME,
name: 'groupId',
label: LL.ENTITY['Group@'].groupId.label(),
description: LL.ENTITY['Group@'].groupId.description(),
placeholder: LL.ENTITY['Group@'].groupId.placeholder(),
isReadOnly: isExisting,
disabled: isExisting,
},
{
component: componentTypes.TEXT_FIELD,
name: 'description',
label: LL.ENTITY['Group@'].description.label(),
description: LL.ENTITY['Group@'].description.description(),
placeholder: LL.ENTITY['Group@'].description.placeholder(),
validate: [
{
type: validatorTypes.MAX_LENGTH,
threshold: 200,
},
],
},
{
component: componentTypes.CHECKBOX,
name: 'autoAssignUsers',
label: LL.ENTITY['Group@'].autoAssignUsers.label(),
description: LL.ENTITY['Group@'].autoAssignUsers.description(),
condition: { when: 'groupId', is: DefaultGroupIds.DEFAULT },
},
{
component: CustomComponentTypes.USER_SELECT,
multiSelect: true,
freeSolo: true,
name: 'members',
label: LL.ENTITY['Group@'].members.label(),
description: LL.ENTITY['Group@'].members.description(),
helperText: LL.ENTITY['Group@'].members.hintText(),
emptyText: LL.ENTITY['Group@'].members.emptyText(),
validate: [
{
type: CustomValidatorTypes.NO_DUPLICATES,
},
],
},
{
component: CustomComponentTypes.ACCESSPOLICY_SELECT,
multiSelect: true,
freeSolo: true,
name: 'apiAccessPolicyIds',
label: LL.ENTITY['Group@'].accessPolicies.label(),
description: LL.ENTITY['Group@'].accessPolicies.description(),
helperText: LL.ENTITY['Group@'].accessPolicies.hintText(),
emptyText: LL.ENTITY['Group@'].accessPolicies.emptyText(),
validate: [
{
type: CustomValidatorTypes.NO_DUPLICATES,
},
],
// Do NOT allow changing the Admin group policies
condition: {
when: 'groupId',
is: DefaultGroupIds.ADMIN,
then: { visible: false },
else: { visible: true },
},
},
],
},
{
title: LL.VIEW.wizard.STEP.review.title(),
fields: [
{
component: componentTypes.REVIEW,
name: 'review',
Template: ({ data }: { data: FormData }) => {
const [apiAccessPolicies] = apiHooks.useApiAccessPolicies();
const getApiAccessPolicyName = useCallback(
(id: string): string => {
if (apiAccessPolicies == null) return startCase(id);
return apiAccessPolicies.find((policy) => policy.apiAccessPolicyId === id)?.name || startCase(id);
},
[apiAccessPolicies],
);
return (
{data.groupId === DefaultGroupIds.DEFAULT ? (
data.autoAssignUsers ? (
<>
Yes}
/>
{LL.VIEW.GROUP.AUTO_ASSIGN_USERS.WARNING.enabled()}
{LL.VIEW.GROUP.AUTO_ASSIGN_USERS.WARNING.members()}
}
/>
>
) : (
<>
No}
/>
{LL.VIEW.GROUP.AUTO_ASSIGN_USERS.WARNING.disabled()}
}
/>
>
)
) : (
}
/>
)}
{sortApiAccessPolicyIds(data.apiAccessPolicyIds || []).map((id: string, i) => (
{getApiAccessPolicyName(id)}
))}
>
}
/>
);
},
},
],
},
];
};
const MembersPreview: React.FC<{ members?: string[] }> = ({ members }) => {
const { LL:_LL } = useI18nContext();
if (members == null || isEmpty(members)) {
return {_LL.VIEW.GROUP.wizard.review.noMembers()};
}
return ({ key: member }))} />;
};
export const getInitialValues = (group: Group | undefined): Partial => {
return {
...group,
autoAssignUsers: group?.autoAssignUsers === true,
// members: membersToFieldArray(group?.members),
members: group?.members || [],
apiAccessPolicyIds: group?.apiAccessPolicyIds || [],
claims: group?.claims || [],
};
};