|
| 1 | +// Copyright © 2025 The Things Network Foundation, The Things Industries B.V. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import React from 'react' |
| 16 | +import { useParams } from 'react-router-dom' |
| 17 | +import { useSelector } from 'react-redux' |
| 18 | + |
| 19 | +import { CLIENT } from '@console/constants/entities' |
| 20 | + |
| 21 | +import PageTitle from '@ttn-lw/components/page-title' |
| 22 | +import { useBreadcrumbs } from '@ttn-lw/components/breadcrumbs/context' |
| 23 | +import Breadcrumb from '@ttn-lw/components/breadcrumbs/breadcrumb' |
| 24 | + |
| 25 | +import RequireRequest from '@ttn-lw/lib/components/require-request' |
| 26 | + |
| 27 | +import AccountCollaboratorsForm from '@console/containers/collaborators-form' |
| 28 | + |
| 29 | +import { selectCollaboratorById } from '@ttn-lw/lib/store/selectors/collaborators' |
| 30 | +import sharedMessages from '@ttn-lw/lib/shared-messages' |
| 31 | +import { getCollaborator } from '@ttn-lw/lib/store/actions/collaborators' |
| 32 | + |
| 33 | +const OAuthClientCollaboratorEditInner = () => { |
| 34 | + const { clientId, collaboratorId, collaboratorType } = useParams() |
| 35 | + |
| 36 | + useBreadcrumbs( |
| 37 | + 'user-settings.oauth-clients.single.collaborators.single.edit', |
| 38 | + <Breadcrumb |
| 39 | + path={`/user-settings/oauth-clients/${clientId}/collaborators/${collaboratorType}/${collaboratorId}`} |
| 40 | + content={sharedMessages.edit} |
| 41 | + />, |
| 42 | + ) |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="container container--xxl grid"> |
| 46 | + <PageTitle title={sharedMessages.collaboratorEdit} values={{ collaboratorId }} /> |
| 47 | + <div className="item-12 xl:item-8"> |
| 48 | + <AccountCollaboratorsForm |
| 49 | + entity={CLIENT} |
| 50 | + entityId={clientId} |
| 51 | + collaboratorId={collaboratorId} |
| 52 | + collaboratorType={collaboratorType === 'user' ? 'user' : 'organization'} |
| 53 | + update |
| 54 | + /> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +const OAuthClientCollaboratorEdit = () => { |
| 61 | + const { clientId, collaboratorId, collaboratorType } = useParams() |
| 62 | + |
| 63 | + // Check if collaborator still exists after being possibly deleted. |
| 64 | + const collaborator = useSelector(state => selectCollaboratorById(state, collaboratorId)) |
| 65 | + const hasCollaborator = Boolean(collaborator) |
| 66 | + const isUser = collaboratorType === 'user' |
| 67 | + |
| 68 | + useBreadcrumbs( |
| 69 | + 'user-settings.oauth-clients.single.collaborators.single', |
| 70 | + <Breadcrumb |
| 71 | + path={`/user-settings/oauth-clients/${clientId}/collaborators/${collaboratorType}/${collaboratorId}`} |
| 72 | + content={collaboratorId} |
| 73 | + />, |
| 74 | + ) |
| 75 | + |
| 76 | + if (collaboratorType !== 'user' && collaboratorType !== 'organization') { |
| 77 | + return null |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <RequireRequest requestAction={getCollaborator('client', clientId, collaboratorId, isUser)}> |
| 82 | + {hasCollaborator && <OAuthClientCollaboratorEditInner />} |
| 83 | + </RequireRequest> |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +export default OAuthClientCollaboratorEdit |
0 commit comments