Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 5x 78x 11x | /* * Copyright 2022 Harness Inc. All rights reserved. * Use of this source code is governed by the PolyForm Shield 1.0.0 license * that can be found in the licenses directory at the root of this repository, also available at * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt. */ import React, { useState } from 'react' import { Button, Container, Formik, FormikForm as Form, Layout, ModalErrorHandler, ModalErrorHandlerBinding, MultiSelectOption, FormInput, ButtonVariation } from '@wings-software/uicore' import * as Yup from 'yup' import { useParams } from 'react-router-dom' import { pick, cloneDeep } from 'lodash-es' import { NameIdDescriptionTags, useToaster } from '@common/components' import { useStrings } from 'framework/strings' import { UserGroupDTO, usePostUserGroup, usePutUserGroup, useGetUsers } from 'services/cd-ng' import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces' import { useMutateAsGet } from '@common/hooks' import { IdentifierSchema, NameSchema } from '@common/utils/Validation' import UserItemRenderer, { UserItem } from '@audit-trail/components/UserItemRenderer/UserItemRenderer' import UserTagRenderer from '@audit-trail/components/UserTagRenderer/UserTagRenderer' import useRBACError from '@rbac/utils/useRBACError/useRBACError' import css from '@rbac/modals/UserGroupModal/useUserGroupModal.module.scss' interface UserGroupModalData { data?: UserGroupDTO isEdit?: boolean isAddMember?: boolean onSubmit?: () => void onCancel?: () => void } interface UserGroupFormDTO extends UserGroupDTO { userList?: MultiSelectOption[] } const UserGroupForm: React.FC<UserGroupModalData> = props => { const { data: userGroupData, onSubmit, isEdit, isAddMember, onCancel } = props const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>() const { getRBACErrorMessage } = useRBACError() const { getString } = useStrings() const { showSuccess } = useToaster() const [search, setSearch] = useState<string>() const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding>() const { mutate: createUserGroup, loading: saving } = usePostUserGroup({ queryParams: { accountIdentifier: accountId, orgIdentifier, projectIdentifier } }) const { mutate: editUserGroup, loading: updating } = usePutUserGroup({ queryParams: { accountIdentifier: accountId, orgIdentifier, projectIdentifier } }) const { data: userList } = useMutateAsGet(useGetUsers, { body: { searchTerm: search }, queryParams: { accountIdentifier: accountId, orgIdentifier, projectIdentifier } }) const users: UserItem[] = userList?.data?.content?.map(value => { return { label: value.name || '', value: value.uuid, email: value.email } }) || [] const handleEdit = async (formData: UserGroupFormDTO): Promise<void> => { const values = cloneDeep(formData) const userDetails = values.userList?.map((user: MultiSelectOption) => user.value as string) delete values.userList const dataToSubmit: UserGroupDTO = values if (userDetails) dataToSubmit['users']?.push(...userDetails) try { const edited = await editUserGroup(dataToSubmit) /* istanbul ignore else */ Iif (edited) { showSuccess( isEdit ? getString('rbac.userGroupForm.editSuccess', { name: edited.data?.name }) : getString('rbac.userGroupForm.addMemberSuccess') ) onSubmit?.() } } catch (e) { /* istanbul ignore next */ modalErrorHandler?.showDanger(getRBACErrorMessage(e)) } } const handleCreate = async (values: UserGroupFormDTO): Promise<void> => { const dataToSubmit: UserGroupDTO = pick(values, ['name', 'identifier', 'description', 'tags']) dataToSubmit['users'] = values.userList?.map((user: MultiSelectOption) => user.value as string) try { const created = await createUserGroup(dataToSubmit) /* istanbul ignore else */ if (created) { showSuccess(getString('rbac.userGroupForm.createSuccess', { name: created.data?.name })) onSubmit?.() } } catch (e) { /* istanbul ignore next */ modalErrorHandler?.showDanger(getRBACErrorMessage(e)) } } return ( <Formik<UserGroupFormDTO> initialValues={{ identifier: '', name: '', description: '', tags: {}, ...userGroupData }} formName="userGroupForm" validationSchema={Yup.object().shape({ name: NameSchema(), identifier: IdentifierSchema() })} onSubmit={values => { modalErrorHandler?.hide() Iif (isEdit || isAddMember) handleEdit(values) else handleCreate(values) }} > {formikProps => { return ( <Form> <Container className={css.form}> <ModalErrorHandler bind={setModalErrorHandler} /> {isAddMember ? null : ( <NameIdDescriptionTags formikProps={formikProps} identifierProps={{ isIdentifierEditable: !isEdit }} /> )} {isEdit ? null : ( <FormInput.MultiSelect name="userList" label={getString('rbac.userGroupPage.addUsers')} items={users} className={css.input} multiSelectProps={{ allowCreatingNewItems: false, onQueryChange: (query: string) => { setSearch(query) }, tagRenderer: (item: MultiSelectOption) => ( <UserTagRenderer key={item.value.toString()} item={item} /> ), itemRender: (item, { handleClick }) => ( <UserItemRenderer key={item.value.toString()} item={item} handleClick={handleClick} /> ) }} /> )} </Container> <Layout.Horizontal spacing="small"> <Button variation={ButtonVariation.PRIMARY} text={getString('save')} type="submit" disabled={saving || updating} /> <Button text={getString('cancel')} variation={ButtonVariation.TERTIARY} onClick={onCancel} /> </Layout.Horizontal> </Form> ) }} </Formik> ) } export default UserGroupForm |