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AAC,EAAA+D,OAAD,CAAQC,KAAK,SAAb","sources":["webpack://grafana/./public/app/features/serviceaccounts/ServiceAccountCreatePage.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useState } from 'react';\nimport { connect } from 'react-redux';\nimport { useHistory } from 'react-router-dom';\n\nimport { NavModel } from '@grafana/data';\nimport { getBackendSrv } from '@grafana/runtime';\nimport { Form, Button, Input, Field } from '@grafana/ui';\nimport Page from 'app/core/components/Page/Page';\nimport { UserRolePicker } from 'app/core/components/RolePicker/UserRolePicker';\nimport { fetchBuiltinRoles, fetchRoleOptions, updateUserRoles } from 'app/core/components/RolePicker/api';\nimport { contextSrv } from 'app/core/core';\nimport { AccessControlAction, OrgRole, Role, ServiceAccountCreateApiResponse, ServiceAccountDTO } from 'app/types';\n\nimport { getNavModel } from '../../core/selectors/navModel';\nimport { StoreState } from '../../types';\n\ninterface ServiceAccountCreatePageProps {\n navModel: NavModel;\n}\n\nconst createServiceAccount = async (sa: ServiceAccountDTO) => getBackendSrv().post('/api/serviceaccounts/', sa);\nconst updateServiceAccount = async (id: number, sa: ServiceAccountDTO) =>\n getBackendSrv().patch(`/api/serviceaccounts/${id}`, sa);\n\nconst ServiceAccountCreatePage: React.FC<ServiceAccountCreatePageProps> = ({ navModel }) => {\n const [roleOptions, setRoleOptions] = useState<Role[]>([]);\n const [builtinRoles, setBuiltinRoles] = useState<{ [key: string]: Role[] }>({});\n const [pendingRoles, setPendingRoles] = useState<Role[]>([]);\n\n const currentOrgId = contextSrv.user.orgId;\n const [serviceAccount, setServiceAccount] = useState<ServiceAccountDTO>({\n id: 0,\n orgId: contextSrv.user.orgId,\n role: OrgRole.Viewer,\n tokens: 0,\n name: '',\n login: '',\n isDisabled: false,\n createdAt: '',\n teams: [],\n });\n\n useEffect(() => {\n async function fetchOptions() {\n try {\n if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {\n let options = await fetchRoleOptions(currentOrgId);\n setRoleOptions(options);\n }\n\n if (contextSrv.hasPermission(AccessControlAction.ActionBuiltinRolesList)) {\n const builtInRoles = await fetchBuiltinRoles(currentOrgId);\n setBuiltinRoles(builtInRoles);\n }\n } catch (e) {\n console.error('Error loading options', e);\n }\n }\n if (contextSrv.licensedAccessControlEnabled()) {\n fetchOptions();\n }\n }, [currentOrgId]);\n\n const history = useHistory();\n\n const onSubmit = useCallback(\n async (data: ServiceAccountDTO) => {\n data.role = serviceAccount.role;\n const response = await createServiceAccount(data);\n try {\n const newAccount: ServiceAccountCreateApiResponse = {\n avatarUrl: response.avatarUrl,\n id: response.id,\n isDisabled: response.isDisabled,\n login: response.login,\n name: response.name,\n orgId: response.orgId,\n role: response.role,\n tokens: response.tokens,\n };\n await updateServiceAccount(response.id, data);\n await updateUserRoles(pendingRoles, newAccount.id, newAccount.orgId);\n } catch (e) {\n console.error(e);\n }\n history.push('/org/serviceaccounts/');\n },\n [history, serviceAccount.role, pendingRoles]\n );\n\n const onRoleChange = (role: OrgRole) => {\n setServiceAccount({\n ...serviceAccount,\n role: role,\n });\n };\n\n const onPendingRolesUpdate = (roles: Role[], userId: number, orgId: number | undefined) => {\n // keep the new role assignments for user\n setPendingRoles(roles);\n };\n\n return (\n <Page navModel={navModel}>\n <Page.Contents>\n <h1>Create service account</h1>\n <Form onSubmit={onSubmit} validateOn=\"onBlur\">\n {({ register, errors }) => {\n return (\n <>\n <Field\n label=\"Display name\"\n required\n invalid={!!errors.name}\n error={errors.name ? 'Display name is required' : undefined}\n >\n <Input id=\"display-name-input\" {...register('name', { required: true })} />\n </Field>\n {contextSrv.accessControlEnabled() && (\n <Field label=\"Role\">\n <UserRolePicker\n userId={serviceAccount.id || 0}\n orgId={serviceAccount.orgId}\n builtInRole={serviceAccount.role}\n builtInRoles={builtinRoles}\n onBuiltinRoleChange={(newRole) => onRoleChange(newRole)}\n builtinRolesDisabled={false}\n roleOptions={roleOptions}\n updateDisabled={true}\n onApplyRoles={onPendingRolesUpdate}\n pendingRoles={pendingRoles}\n />\n </Field>\n )}\n <Button type=\"submit\">Create</Button>\n </>\n );\n }}\n </Form>\n </Page.Contents>\n </Page>\n );\n};\n\nconst mapStateToProps = (state: StoreState) => ({\n navModel: getNavModel(state.navIndex, 'serviceaccounts'),\n});\n\nexport default connect(mapStateToProps)(ServiceAccountCreatePage);\n"],"names":["connect","state","navModel","getNavModel","navIndex","roleOptions","setRoleOptions","useState","builtinRoles","setBuiltinRoles","pendingRoles","setPendingRoles","currentOrgId","contextSrv","serviceAccount","setServiceAccount","id","orgId","role","OrgRole","tokens","name","login","isDisabled","createdAt","teams","useEffect","async","AccessControlAction","options","fetchRoleOptions","builtInRoles","fetchBuiltinRoles","e","console","error","fetchOptions","history","useHistory","onSubmit","useCallback","data","response","getBackendSrv","post","sa","createServiceAccount","newAccount","avatarUrl","patch","updateServiceAccount","updateUserRoles","push","onPendingRolesUpdate","roles","userId","Form","validateOn","register","errors","Field","label","required","invalid","undefined","Input","builtInRole","onBuiltinRoleChange","newRole","onRoleChange","builtinRolesDisabled","updateDisabled","onApplyRoles","Button","type"],"sourceRoot":""}
|