Skip to content

Commit

Permalink
Merge pull request #6491 from TheThingsNetwork/issue/6314-console-del…
Browse files Browse the repository at this point in the history
…ete-collaborator

Update handleDelete of Collaborators in Console
  • Loading branch information
nicholaspcr authored Sep 1, 2023
2 parents 8af1a0e + fc7792b commit cd6c67d
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 23 deletions.
5 changes: 3 additions & 2 deletions pkg/webui/containers/collaborator-form/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ const useCollaboratorData = (entity, entityId, collaboratorId, tts) => {
const currentUserId = useSelector(selectUserId)
const isCurrentUser = isUser && currentUserId === collaboratorId
const updateCollaborator = patch => tts[sdkServices[entity]].Collaborators.update(entityId, patch)
const removeCollaborator = collaboratorIds =>
tts[sdkServices[entity]].Collaborators.remove(entityId, collaboratorIds)
const removeCollaborator = (isUser, collaboratorIds) => {
tts[sdkServices[entity]].Collaborators.remove(isUser, entityId, collaboratorIds)
}

return {
collaborator,
Expand Down
14 changes: 2 additions & 12 deletions pkg/webui/containers/collaborator-form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ const CollaboratorForm = props => {
removeCollaborator,
} = useCollaboratorData(entity, entityId, collaboratorId, tts)

const collaboratorType = isCollaboratorUser ? 'user' : 'organization'

const [submitError, setSubmitError] = useState(undefined)
const navigate = useNavigate()
const error = submitError || passedError
Expand Down Expand Up @@ -127,18 +125,10 @@ const CollaboratorForm = props => {
[navigate, update, updateCollaborator],
)
const handleDelete = useCallback(async () => {
const collaborator_ids = {
[`${collaboratorType}_ids`]: {
[`${collaboratorType}_id`]: collaboratorId,
},
}
const updatedCollaborator = {
ids: collaborator_ids,
}
setSubmitError(undefined)

try {
await removeCollaborator(updatedCollaborator)
await removeCollaborator(isCollaboratorUser, collaboratorId)
toast({
message: sharedMessages.collaboratorDeleteSuccess,
type: toast.types.SUCCESS,
Expand All @@ -147,7 +137,7 @@ const CollaboratorForm = props => {
} catch (error) {
setSubmitError(error)
}
}, [collaboratorId, collaboratorType, navigate, removeCollaborator])
}, [collaboratorId, isCollaboratorUser, navigate, removeCollaborator])

const initialValues = React.useMemo(() => {
if (!collaborator) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/webui/lib/store/actions/collaborators.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ export const [
},
] = createRequestActions(
DELETE_COLLABORATOR_BASE,
(parentType, parentId) => ({
(parentType, parentId, collaboratorId, isUser) => ({
parentType,
parentId,
collaboratorId,
isUser,
}),
(parentType, parentId, selector) => ({ selector }),
(parentType, parentId, collaboratorId, isUser, selector) => ({ selector }),
)
11 changes: 6 additions & 5 deletions pkg/webui/lib/store/middleware/collaborators.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ export default tts => {
const deleteCollaboratorLogic = createRequestLogic({
type: collaborators.DELETE_COLLABORATOR,
process: async ({ action }) => {
const { parentType, parentId } = action.payload

const result = await tts.Clients.Collaborators.update(parentType, parentId)

return result
const { parentType, parentId, collaboratorId, isUser } = action.payload
return await tts[entitySdkServiceMap[parentType]].Collaborators.remove(
isUser,
parentId,
collaboratorId,
)
},
})

Expand Down
1 change: 1 addition & 0 deletions sdk/js/src/service/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Applications {
get: 'application_ids.application_id',
list: 'application_ids.application_id',
set: 'application_ids.application_id',
delete: 'application_ids.application_id',
},
})
this.Webhooks = new Webhooks(api.ApplicationWebhookRegistry)
Expand Down
1 change: 1 addition & 0 deletions sdk/js/src/service/clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Clients {
get: 'client_ids.client_id',
list: 'client_ids.client_id',
set: 'client_ids.client_id',
delete: 'client_ids.client_id',
},
})

Expand Down
16 changes: 14 additions & 2 deletions sdk/js/src/service/collaborators.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,20 @@ class Collaborators {
return await this.add(entityId, data)
}

async remove(entityId, data) {
return await this.add(entityId, { ...data, rights: [] })
async remove(isUser, entityId, collaboratorId) {
const entityIdRoute = this._parentRoutes.delete
const collaboratorIdRoute = isUser
? 'collaborator_ids.user_ids.user_id'
: 'collaborator_ids.organization_ids.organization_id'

const result = await this._api.DeleteCollaborator({
routeParams: {
[entityIdRoute]: entityId,
[collaboratorIdRoute]: collaboratorId,
},
})

return Marshaler.payloadSingleResponse(result)
}
}

Expand Down
1 change: 1 addition & 0 deletions sdk/js/src/service/gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Gateways {
get: 'gateway_ids.gateway_id',
list: 'gateway_ids.gateway_id',
set: 'gateway_ids.gateway_id',
delete: 'gateway_ids.gateway_id',
},
})
autoBind(this)
Expand Down
1 change: 1 addition & 0 deletions sdk/js/src/service/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Organizations {
get: 'organization_ids.organization_id',
list: 'organization_ids.organization_id',
set: 'organization_ids.organization_id',
delete: 'organization_ids.organization_id',
},
})
autoBind(this)
Expand Down

0 comments on commit cd6c67d

Please sign in to comment.