Skip to content

Commit

Permalink
[EMERAL-122] Add staff when user already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
manu-d committed Dec 18, 2017
1 parent 9c27bf1 commit 5e996c8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,47 @@
vm = this

vm.admin_roles = ADMIN_ROLES
vm.staffAlreadyExists = false
vm.staff = null

vm.onSubmit = () ->
vm.isLoading = true
MnoeUsers.addStaff(vm.user).then(
MnoeUsers.search(email: vm.user.email).then(
(success) ->
toastr.success("mnoe_admin_panel.dashboard.staff.add_staff.modal.toastr_success", {extraData: {username: "#{vm.user.name} #{vm.user.surname}"}})
vm.staffAlreadyExists = !_.isEmpty(success.data)
vm.staff = if vm.staffAlreadyExists
{
id: success.data[0].id,
admin_role: vm.user.admin_role,
name: success.data[0].name,
surname: success.data[0].surname
}
else
vm.user
vm.addStaff() unless vm.staffAlreadyExists
(error) ->
vm.showError()
).finally(-> vm.isLoading = false)

vm.addStaff = () ->
vm.isLoading = true
method = if vm.staff == vm.user then 'addStaff' else 'updateStaff'
MnoeUsers[method](vm.staff).then(
(success) ->
toastr.success("mnoe_admin_panel.dashboard.staff.add_staff.modal.toastr_success", {extraData: {staff_name: "#{vm.staff.name} #{vm.staff.surname}"}})
# Close the modal returning the item to the parent window
$uibModalInstance.close(success.data)
(error) ->
toastr.error('mnoe_admin_panel.dashboard.staff.add_staff.modal.toastr_error', {extraData: { user_name: "#{vm.user.name} #{vm.user.surname}" }})
$log.error("An error occurred:", error)
vm.showError()
).finally(-> vm.isLoading = false)

vm.showError = () ->
toastr.error('mnoe_admin_panel.dashboard.staff.add_staff.modal.toastr_error', {extraData: { staff_name: "#{vm.user.name} #{vm.user.surname}" }})
$log.error("An error occurred:", error)

vm.onEmailChange = () ->
vm.staffAlreadyExists = false

vm.onCancel = () ->
$uibModalInstance.dismiss('cancel')

Expand Down
13 changes: 11 additions & 2 deletions src/app/views/staff/create-staff-modal/create-staff.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,33 @@ <h3 class="modal-title" translate>mnoe_admin_panel.dashboard.staff.add_staff.mod

<div class="row top-buffer-1">
<div class="col-sm-6">
<input type="email" class="form-control input-sm" id="user-email" ng-model="vm.user.email" placeholder="{{'mnoe_admin_panel.dashboard.staff.add_staff.modal.placeholder.email' | translate}}" aria-label="{{'mnoe_admin_panel.dashboard.staff.add_staff.modal.placeholder.email' | translate}}" required>
<input type="email" class="form-control input-sm" id="user-email" ng-model="vm.user.email" ng-change="vm.onEmailChange()" placeholder="{{'mnoe_admin_panel.dashboard.staff.add_staff.modal.placeholder.email' | translate}}" aria-label="{{'mnoe_admin_panel.dashboard.staff.add_staff.modal.placeholder.email' | translate}}" required>
</div>
<div class="col-sm-6">
<select ng-model="vm.user.admin_role" class="form-control input-sm" ng-options="role.value as role.label | translate for role in vm.admin_roles" required>
<option value="" disabled selected translate>mnoe_admin_panel.dashboard.staff.add_staff.modal.placeholder.role</option>
</select>
</div>
</div>

<div class="alert alert-warning" ng-if="vm.staffAlreadyExists">
<span><i class="fa fa-warning"></i></span>
{{'mnoe_admin_panel.dashboard.staff.add_staff.modal.staff_exists' | translate}}
</div>
</div>
</form>

<div class="modal-footer">
<div class="row actions">
<div class="col-xs-12">
<button type="button" ng-click="vm.onSubmit()" ng-disabled="vm.isLoading || vm.form.user.$invalid" class="btn btn-primary">
<button type="button" ng-click="vm.onSubmit()" ng-disabled="vm.isLoading || vm.form.user.$invalid" class="btn btn-primary" ng-hide="vm.staffAlreadyExists">
<span ng-show="vm.isLoading"><i class="fa fa-spinner fa-pulse fa-fw"></i></span>
{{'mnoe_admin_panel.dashboard.staff.add_staff.modal.save' | translate}}
</button>
<button type="button" ng-click="vm.addStaff()" ng-disabled="vm.isLoading || vm.form.user.$invalid" class="btn btn-primary" ng-show="vm.staffAlreadyExists">
<span ng-show="vm.isLoading"><i class="fa fa-spinner fa-pulse fa-fw"></i></span>
{{'mnoe_admin_panel.dashboard.staff.add_staff.modal.confirm' | translate}}
</button>
<a href="" ng-click="vm.onCancel()"><i class="fa fa-times"></i>{{ 'mnoe_admin_panel.dashboard.staff.add_staff.modal.cancel' | translate}}</a>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/app/views/staff/create-staff-modal/create-staff.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#staff-user-modal {

.modal-body .alert-warning {
margin-top: 30px;
}
}
1 change: 1 addition & 0 deletions src/locales/en-AU.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
"mnoe_admin_panel.dashboard.staff.title": "Staff",
"mnoe_admin_panel.dashboard.staff.add_staff": "Add staff",
"mnoe_admin_panel.dashboard.staff.add_staff.modal.title": "Add a new staff",
"mnoe_admin_panel.dashboard.staff.add_staff.modal.staff_exists": "This user already exists. Please confirm that you want to give them access to the Control Panel? (Only the role will be updated)",
"mnoe_admin_panel.dashboard.staff.add_staff.modal.cancel": "Cancel",
"mnoe_admin_panel.dashboard.staff.add_staff.modal.save": "Save",
"mnoe_admin_panel.dashboard.staff.add_staff.modal.placeholder.name": "First name",
Expand Down

0 comments on commit 5e996c8

Please sign in to comment.