Skip to content

Commit

Permalink
console: Refactor connect in collaborators table
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelJankoski committed Nov 28, 2023
1 parent 8badbf2 commit 07556e7
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions pkg/webui/account/containers/collaborators-table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
// limitations under the License.

import React from 'react'
import { connect, useDispatch } from 'react-redux'
import { useDispatch, useSelector } from 'react-redux'
import { defineMessages, useIntl } from 'react-intl'
import { bindActionCreators } from 'redux'
import { createSelector } from 'reselect'

import Icon from '@ttn-lw/components/icon'
Expand All @@ -29,10 +28,12 @@ import FetchTable from '@ttn-lw/containers/fetch-table'
import Message from '@ttn-lw/lib/components/message'

import { getCollaboratorId } from '@ttn-lw/lib/selectors/id'
import PropTypes from '@ttn-lw/lib/prop-types'
import sharedMessages from '@ttn-lw/lib/shared-messages'
import attachPromise from '@ttn-lw/lib/store/actions/attach-promise'
import { getCollaboratorsList, deleteCollaborator } from '@ttn-lw/lib/store/actions/collaborators'
import {
getCollaboratorsList,
deleteCollaborator as deleteCollaboratorAction,
} from '@ttn-lw/lib/store/actions/collaborators'
import {
selectCollaborators,
selectCollaboratorsTotalCount,
Expand All @@ -52,9 +53,17 @@ const m = defineMessages({
})

const CollaboratorsTable = props => {
const { clientId, currentUserId, handleDeleteCollaborator, ...rest } = props
const { ...rest } = props
const dispatch = useDispatch()
const intl = useIntl()
const clientId = useSelector(selectSelectedClientId)
const currentUserId = useSelector(selectUserId)

const handleDeleteCollaborator = React.useCallback(
(collaboratorID, isUsr) =>
dispatch(attachPromise(deleteCollaboratorAction('client', clientId, collaboratorID, isUsr))),
[clientId, dispatch],
)

const deleteCollaborator = React.useCallback(
async ids => {
Expand Down Expand Up @@ -194,30 +203,4 @@ const CollaboratorsTable = props => {
)
}

CollaboratorsTable.propTypes = {
clientId: PropTypes.string.isRequired,
currentUserId: PropTypes.string.isRequired,
handleDeleteCollaborator: PropTypes.func.isRequired,
}

export default connect(
state => ({
clientId: selectSelectedClientId(state),
currentUserId: selectUserId(state),
}),
dispatch => ({
...bindActionCreators(
{
handleDeleteCollaborator: attachPromise(deleteCollaborator),
},
dispatch,
),
}),
(stateProps, dispatchProps, ownProps) => ({
...stateProps,
...dispatchProps,
...ownProps,
handleDeleteCollaborator: (collaboratorID, isUsr) =>
dispatchProps.handleDeleteCollaborator('client', stateProps.clientId, collaboratorID, isUsr),
}),
)(CollaboratorsTable)
export default CollaboratorsTable

0 comments on commit 07556e7

Please sign in to comment.