feat: adds ability to remove contribution types when editing existing user (#94)

* adds ability to remove contribution types when editing existing user

* removes .swp file

* refactor: concat types in all cases except when removing
This commit is contained in:
Tyler Reitz 2018-06-19 13:09:05 -07:00 committed by Kent C. Dodds
parent 4f7bbdf56f
commit 2c9f47629b
3 changed files with 31 additions and 4 deletions

View file

@ -135,7 +135,7 @@ function promptForCommand(argv) {
message: 'What do you want to do?', message: 'What do you want to do?',
choices: [ choices: [
{ {
name: 'Add a new contributor or add a new contribution type', name: 'Add new contributor or edit contribution type',
value: 'add', value: 'add',
}, },
{ {

View file

@ -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'],
})
},
)
})

View file

@ -4,15 +4,23 @@ function uniqueTypes(contribution) {
return contribution.type || 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) { if (options.url) {
return (existing || []).concat( return existing.concat(
types.map(type => { types.map(type => {
return {type, url: options.url} 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) { function updateContributor(options, contributor, contributions) {