Skip to content

Commit

Permalink
feat: adds ability to remove contribution types when editing existing…
Browse files Browse the repository at this point in the history
… user (#94)

* adds ability to remove contribution types when editing existing user

* removes .swp file

* refactor: concat types in all cases except when removing
  • Loading branch information
tyler-reitz authored and Kent C. Dodds committed Jun 19, 2018
1 parent 4f7bbdf commit 2c9f476
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function promptForCommand(argv) {
message: 'What do you want to do?',
choices: [
{
name: 'Add a new contributor or add a new contribution type',
name: 'Add new contributor or edit contribution type',
value: 'add',
},
{
Expand Down
19 changes: 19 additions & 0 deletions src/contributors/__tests__/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,22 @@ test(`should update an existing contributor's contributions if a new type is add
},
)
})

test(`should update an existing contributor's contributions if an existing type is removed`, () => {
const {options} = fixtures()
const username = 'login2'
const contributions = ['code']

return addContributor(options, username, contributions, mockInfoFetcher).then(
contributors => {
expect(contributors.length).toBe(options.contributors.length)
expect(contributors[1]).toEqual({
login: 'login2',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
contributions: ['code'],
})
},
)
})
14 changes: 11 additions & 3 deletions src/contributors/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ function uniqueTypes(contribution) {
return contribution.type || contribution
}

function formatContributions(options, existing, types) {
function formatContributions(options, existing = [], types) {
const same = _.intersectionBy(uniqueTypes, existing, types)
const remove = types.length < existing.length && same.length

if (options.url) {
return (existing || []).concat(
return existing.concat(
types.map(type => {
return {type, url: options.url}
}),
)
}
return _.uniqBy(uniqueTypes, (existing || []).concat(types))

if (remove) {
return same
}

return _.uniqBy(uniqueTypes, existing.concat(types))
}

function updateContributor(options, contributor, contributions) {
Expand Down

0 comments on commit 2c9f476

Please sign in to comment.