2016-06-30 21:14:25 +03:00
import Component from 'ember-component' ;
import computed from 'ember-computed' ;
import service from 'ember-service/inject' ;
2016-09-26 19:03:53 +03:00
import { isNotFoundError } from 'ember-ajax/errors' ;
2015-10-28 14:36:45 +03:00
export default Component . extend ( {
2015-06-13 17:34:09 +03:00
tagName : '' ,
2016-09-26 19:03:53 +03:00
invite : null ,
2015-08-20 12:25:30 +03:00
isSending : false ,
2015-06-13 17:34:09 +03:00
2016-01-19 16:03:27 +03:00
notifications : service ( ) ,
2016-09-26 19:03:53 +03:00
store : service ( ) ,
2015-06-13 17:34:09 +03:00
2016-09-26 19:03:53 +03:00
createdAt : computed ( 'invite.createdAtUTC' , function ( ) {
let createdAtUTC = this . get ( 'invite.createdAtUTC' ) ;
2015-06-13 17:34:09 +03:00
2016-06-14 11:11:59 +03:00
return createdAtUTC ? moment ( createdAtUTC ) . fromNow ( ) : '' ;
2015-06-13 17:34:09 +03:00
} ) ,
2016-09-26 19:03:53 +03:00
expiresAt : computed ( 'invite.expires' , function ( ) {
let expires = this . get ( 'invite.expires' ) ;
return expires ? moment ( expires ) . fromNow ( ) : '' ;
} ) ,
2017-02-27 16:11:39 +03:00
isExpired : computed ( 'invite.expires' , function ( ) {
let expires = this . get ( 'invite.expires' ) ;
let now = ( new Date ( ) ) . valueOf ( ) ;
return expires < now ;
} ) ,
2015-06-13 17:34:09 +03:00
actions : {
2015-10-28 14:36:45 +03:00
resend ( ) {
2016-09-26 19:03:53 +03:00
let invite = this . get ( 'invite' ) ;
2015-10-28 14:36:45 +03:00
let notifications = this . get ( 'notifications' ) ;
2015-06-13 17:34:09 +03:00
2015-08-20 12:25:30 +03:00
this . set ( 'isSending' , true ) ;
2016-09-26 19:03:53 +03:00
invite . resend ( ) . then ( ( result ) => {
let notificationText = ` Invitation resent! ( ${ invite . get ( 'email' ) } ) ` ;
// the server deletes the old record and creates a new one when
// resending so we need to update the store accordingly
invite . unloadRecord ( ) ;
this . get ( 'store' ) . pushPayload ( 'invite' , result ) ;
2015-06-13 17:34:09 +03:00
// If sending the invitation email fails, the API will still return a status of 201
2016-09-26 19:03:53 +03:00
// but the invite's status in the response object will be 'invited-pending'.
if ( result . invites [ 0 ] . status === 'invited-pending' ) {
2015-10-07 17:44:23 +03:00
notifications . showAlert ( 'Invitation email was not sent. Please try resending.' , { type : 'error' , key : 'invite.resend.not-sent' } ) ;
2015-06-13 17:34:09 +03:00
} else {
2015-11-18 13:50:48 +03:00
notifications . showNotification ( notificationText , { key : 'invite.resend.success' } ) ;
2015-06-13 17:34:09 +03:00
}
2015-10-28 14:36:45 +03:00
} ) . catch ( ( error ) => {
2015-10-07 17:44:23 +03:00
notifications . showAPIError ( error , { key : 'invite.resend' } ) ;
2015-10-28 14:36:45 +03:00
} ) . finally ( ( ) => {
this . set ( 'isSending' , false ) ;
2015-06-13 17:34:09 +03:00
} ) ;
} ,
2015-10-28 14:36:45 +03:00
revoke ( ) {
2016-09-26 19:03:53 +03:00
let invite = this . get ( 'invite' ) ;
let email = invite . get ( 'email' ) ;
2015-10-28 14:36:45 +03:00
let notifications = this . get ( 'notifications' ) ;
2015-06-13 17:34:09 +03:00
2016-09-26 19:03:53 +03:00
// reload the invite to get the most up-to-date information
invite . reload ( ) . then ( ( ) => {
invite . destroyRecord ( ) . then ( ( ) => {
let notificationText = ` Invitation revoked. ( ${ email } ) ` ;
notifications . showNotification ( notificationText , { key : 'invite.revoke.success' } ) ;
} ) . catch ( ( error ) => {
notifications . showAPIError ( error , { key : 'invite.revoke' } ) ;
} ) ;
} ) . catch ( ( error ) => {
if ( isNotFoundError ( error ) ) {
// if the invite no longer exists, then show a warning and reload the route
2015-10-28 14:36:45 +03:00
this . sendAction ( 'reload' ) ;
2016-09-26 19:03:53 +03:00
notifications . showAlert ( 'This invite has been revoked or a user has already accepted the invitation.' , { type : 'error' , delayed : true , key : 'invite.revoke.already-accepted' } ) ;
} else {
throw error ;
2015-06-13 17:34:09 +03:00
}
} ) ;
}
}
} ) ;