2017-08-22 10:53:26 +03:00
import Component from '@ember/component' ;
2017-05-29 21:50:03 +03:00
import moment from 'moment' ;
2017-08-22 10:53:26 +03:00
import { computed } from '@ember/object' ;
2016-09-26 19:03:53 +03:00
import { isNotFoundError } from 'ember-ajax/errors' ;
2017-08-22 10:53:26 +03:00
import { inject as service } from '@ember/service' ;
2015-10-28 14:36:45 +03:00
export default Component . extend ( {
2018-01-11 20:43:23 +03:00
notifications : service ( ) ,
store : service ( ) ,
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-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 ( ) {
2019-03-06 16:53:54 +03:00
let invite = this . invite ;
let notifications = this . 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 ( ) ;
2019-03-06 16:53:54 +03:00
this . 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 {
2020-02-27 12:19:29 +03:00
notifications . showNotification ( notificationText , { icon : 'send-email' , 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 ( ) {
2019-03-06 16:53:54 +03:00
let invite = this . invite ;
2016-09-26 19:03:53 +03:00
let email = invite . get ( 'email' ) ;
2019-03-06 16:53:54 +03:00
let notifications = this . 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 ( ( ) => {
2020-02-27 12:19:29 +03:00
notifications . showNotification ( 'Invitation revoked' , { key : 'invite.revoke.success' , description : ` ${ email } ` } ) ;
2016-09-26 19:03:53 +03:00
} ) . 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
2019-03-06 16:53:54 +03:00
let action = this . reload ;
2017-11-25 02:18:35 +03:00
if ( action ) {
action ( ) ;
}
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
}
} ) ;
}
}
} ) ;