2017-08-22 10:53:26 +03:00
import Component from '@ember/component' ;
2022-02-01 12:34:03 +03:00
import classic from 'ember-classic-decorator' ;
2017-05-29 21:50:03 +03:00
import moment from 'moment' ;
2022-02-01 12:34:03 +03:00
import { action , 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' ;
2022-02-01 12:34:03 +03:00
import { tagName } from '@ember-decorators/component' ;
2015-10-28 14:36:45 +03:00
2022-02-01 12:34:03 +03:00
@ classic
@ tagName ( '' )
export default class GhUserInvited extends Component {
2022-02-01 20:03:45 +03:00
@ service notifications ;
@ service store ;
2015-06-13 17:34:09 +03:00
2022-02-01 12:34:03 +03:00
invite = null ;
isSending = false ;
2015-06-13 17:34:09 +03:00
2022-02-01 12:34:03 +03:00
@ computed ( 'invite.createdAtUTC' )
get createdAt ( ) {
2016-09-26 19:03:53 +03:00
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 ( ) : '' ;
2022-02-01 12:34:03 +03:00
}
2015-06-13 17:34:09 +03:00
2022-02-01 12:34:03 +03:00
@ computed ( 'invite.expires' )
get expiresAt ( ) {
2016-09-26 19:03:53 +03:00
let expires = this . get ( 'invite.expires' ) ;
return expires ? moment ( expires ) . fromNow ( ) : '' ;
2022-02-01 12:34:03 +03:00
}
2016-09-26 19:03:53 +03:00
2022-02-01 12:34:03 +03:00
@ computed ( 'invite.expires' )
get isExpired ( ) {
2017-02-27 16:11:39 +03:00
let expires = this . get ( 'invite.expires' ) ;
let now = ( new Date ( ) ) . valueOf ( ) ;
return expires < now ;
2022-02-01 12:34:03 +03:00
}
@ action
resend ( ) {
let invite = this . invite ;
let notifications = this . notifications ;
this . set ( 'isSending' , true ) ;
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 . store . pushPayload ( 'invite' , result ) ;
// If sending the invitation email fails, the API will still return a status of 201
// but the invite's status in the response object will be 'invited-pending'.
if ( result . invites [ 0 ] . status === 'invited-pending' ) {
notifications . showAlert ( 'Invitation email was not sent. Please try resending.' , { type : 'error' , key : 'invite.resend.not-sent' } ) ;
} else {
notifications . showNotification ( notificationText , { icon : 'send-email' , key : 'invite.resend.success' } ) ;
}
} ) . catch ( ( error ) => {
notifications . showAPIError ( error , { key : 'invite.resend' } ) ;
} ) . finally ( ( ) => {
this . set ( 'isSending' , false ) ;
} ) ;
}
@ action
revoke ( ) {
let invite = this . invite ;
let email = invite . get ( 'email' ) ;
let notifications = this . notifications ;
// reload the invite to get the most up-to-date information
invite . reload ( ) . then ( ( ) => {
invite . destroyRecord ( ) . then ( ( ) => {
notifications . showNotification ( 'Invitation revoked' , { key : 'invite.revoke.success' , description : ` ${ email } ` } ) ;
2015-10-28 14:36:45 +03:00
} ) . catch ( ( error ) => {
2022-02-01 12:34:03 +03:00
notifications . showAPIError ( error , { key : 'invite.revoke' } ) ;
2015-06-13 17:34:09 +03:00
} ) ;
2022-02-01 12:34:03 +03:00
} ) . catch ( ( error ) => {
if ( isNotFoundError ( error ) ) {
// if the invite no longer exists, then show a warning and reload the route
let reloadAction = this . reload ;
if ( reloadAction ) {
reloadAction ( ) ;
2015-06-13 17:34:09 +03:00
}
2022-02-01 12:34:03 +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
}
2022-02-01 12:34:03 +03:00
}