2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../base'
|
2021-04-14 09:17:21 -04:00
|
|
|
App.controller('UserMembershipController', function ($scope, queuedHttp) {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.users = window.users
|
|
|
|
$scope.groupSize = window.groupSize
|
|
|
|
$scope.paths = window.paths
|
|
|
|
$scope.selectedUsers = []
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.inputs = {
|
|
|
|
addMembers: {
|
|
|
|
content: '',
|
|
|
|
error: false,
|
|
|
|
errorMessage: null,
|
2021-04-27 03:52:58 -04:00
|
|
|
inflightCount: 0,
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
removeMembers: {
|
|
|
|
error: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
errorMessage: null,
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const parseEmails = function (emailsString) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const regexBySpaceOrComma = /[\s,]+/
|
|
|
|
let emails = emailsString.split(regexBySpaceOrComma)
|
|
|
|
emails = _.map(emails, email => (email = email.trim()))
|
|
|
|
emails = _.filter(emails, email => email.indexOf('@') !== -1)
|
|
|
|
return emails
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.addMembers = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.inputs.addMembers.error = false
|
|
|
|
$scope.inputs.addMembers.errorMessage = null
|
|
|
|
$scope.inputs.addMembers.inflightCount = 0
|
|
|
|
const emails = parseEmails($scope.inputs.addMembers.content)
|
|
|
|
return Array.from(emails).map(email => {
|
|
|
|
$scope.inputs.addMembers.inflightCount += 1
|
|
|
|
return queuedHttp
|
|
|
|
.post(window.paths.addMember, {
|
|
|
|
email,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.then(function (response) {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.inputs.addMembers.inflightCount -= 1
|
|
|
|
const { data } = response
|
|
|
|
if (data.user != null) {
|
2022-07-07 04:01:47 -04:00
|
|
|
const alreadyListed = $scope.users.find(
|
|
|
|
scopeUser => scopeUser.email === data.user.email
|
|
|
|
)
|
|
|
|
if (!alreadyListed) {
|
|
|
|
$scope.users.push(data.user)
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
return ($scope.inputs.addMembers.content = '')
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.catch(function (response) {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.inputs.addMembers.inflightCount -= 1
|
|
|
|
const { data } = response
|
|
|
|
$scope.inputs.addMembers.error = true
|
|
|
|
return ($scope.inputs.addMembers.errorMessage =
|
|
|
|
data.error != null ? data.error.message : undefined)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.removeMembers = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.inputs.removeMembers.error = false
|
|
|
|
$scope.inputs.removeMembers.errorMessage = null
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const user of Array.from($scope.selectedUsers)) {
|
2021-04-14 09:17:21 -04:00
|
|
|
;(function (user) {
|
2020-05-19 05:02:56 -04:00
|
|
|
let url
|
|
|
|
if (window.paths.removeInvite && user.invite && user._id == null) {
|
|
|
|
url = `${window.paths.removeInvite}/${encodeURIComponent(user.email)}`
|
|
|
|
} else if (window.paths.removeMember && user._id != null) {
|
|
|
|
url = `${window.paths.removeMember}/${user._id}`
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return queuedHttp({
|
|
|
|
method: 'DELETE',
|
|
|
|
url,
|
|
|
|
headers: {
|
2021-04-27 03:52:58 -04:00
|
|
|
'X-Csrf-Token': window.csrfToken,
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.then(function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const index = $scope.users.indexOf(user)
|
|
|
|
if (index === -1) {
|
|
|
|
return
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return $scope.users.splice(index, 1)
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.catch(function (response) {
|
2018-11-05 05:06:39 -05:00
|
|
|
const { data } = response
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.inputs.removeMembers.error = true
|
|
|
|
return ($scope.inputs.removeMembers.errorMessage =
|
2018-11-05 05:06:39 -05:00
|
|
|
data.error != null ? data.error.message : undefined)
|
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
})(user)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return $scope.updateSelectedUsers
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return ($scope.updateSelectedUsers = () =>
|
|
|
|
($scope.selectedUsers = $scope.users.filter(user => user.selected)))
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
export default App.controller('UserMembershipListItemController', $scope =>
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.$watch('user.selected', function (value) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (value != null) {
|
|
|
|
return $scope.updateSelectedUsers()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
)
|