Skip to content
Snippets Groups Projects
Commit bb06148d authored by Jure's avatar Jure
Browse files

feat(users-manager): add support for removing members

parent 9bbccab1
No related branches found
No related tags found
No related merge requests found
...@@ -16,24 +16,27 @@ class User extends React.Component { ...@@ -16,24 +16,27 @@ class User extends React.Component {
) )
} }
processTeamMembership(teamType) { findExistingTeam(teamType) {
const { user, createTeam, updateTeam, teams, configuredTeams } = this.props const { teams } = this.props
return teams.find(
const existingTeam = teams.find(
team => team.teamType === teamType && team.object === undefined, team => team.teamType === teamType && team.object === undefined,
) )
}
addMember(teamType) {
const { user, createTeam, updateTeam, configuredTeams } = this.props
const existingTeam = this.findExistingTeam(teamType)
if (existingTeam) { if (existingTeam) {
// console.log('found team', existingTeam, 'would add user', user) if (!existingTeam.members.includes(user.id)) {
existingTeam.members.push(user.id) // console.log('found team', existingTeam, 'would add user', user)
updateTeam(existingTeam) existingTeam.members.push(user.id)
updateTeam(existingTeam)
} else {
// console.log('user already member of', existingTeam)
}
} else { } else {
// console.log( // console.log('team not found', teamType, 'would create team with', user)
// 'theme not found',
// existingTeam,
// 'would create team for',
// user,
// )
createTeam({ createTeam({
teamType, teamType,
name: configuredTeams[teamType].name, name: configuredTeams[teamType].name,
...@@ -42,25 +45,47 @@ class User extends React.Component { ...@@ -42,25 +45,47 @@ class User extends React.Component {
} }
} }
removeMember(teamType) {
const { user, updateTeam } = this.props
const existingTeam = this.findExistingTeam(teamType)
if (!existingTeam) {
return
}
if (existingTeam) {
if (existingTeam.members.includes(user.id)) {
// console.log('found team', existingTeam, 'would remove user', user)
existingTeam.members = existingTeam.members.filter(
member => member !== user.id,
)
updateTeam(existingTeam)
}
}
}
onTeamChange(teamTypes) { onTeamChange(teamTypes) {
teamTypes.each(teamType => this.processTeamMembership(teamType)) const { configuredTeams } = this.props
// Idempotently add member
teamTypes.forEach(teamType => this.addMember(teamType))
// Idempotently remove member
const teamsDifference = Object.keys(configuredTeams).filter(
teamType => !teamTypes.includes(teamType),
)
teamsDifference.forEach(teamType => this.removeMember(teamType))
} }
render() { render() {
const { user, teams, configuredTeams } = this.props const { user, teams, configuredTeams } = this.props
const activeTeams = Object.entries(configuredTeams).find( const activeTeams = Object.entries(configuredTeams).filter(
([teamType, _]) => { ([teamType, _]) =>
const teamsOfType = teams.find( teams.find(
team => team.teamType === teamType && team.object === undefined, team =>
) team.teamType === teamType &&
if (teamsOfType) { team.object === undefined &&
const isMember = teamsOfType.find(team =>
team.members.includes(user.id), team.members.includes(user.id),
) ),
return isMember
}
return false
},
) )
const checkBoxValue = activeTeams const checkBoxValue = activeTeams
? activeTeams.map(([teamType, _]) => teamType) ? activeTeams.map(([teamType, _]) => teamType)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment