2022-08-30 18:09:08 +03:00
import Component from '@glimmer/component' ;
2022-09-23 20:15:08 +03:00
import moment from 'moment-timezone' ;
2022-08-30 18:09:08 +03:00
import { action } 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-08-30 18:09:08 +03:00
import { tracked } from '@glimmer/tracking' ;
2015-10-28 14:36:45 +03:00
2022-02-01 12:34:03 +03:00
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-08-30 18:09:08 +03:00
@ tracked isSending = false ;
2015-06-13 17:34:09 +03:00
2022-02-01 12:34:03 +03:00
get createdAt ( ) {
2022-08-30 18:09:08 +03:00
const createdAtUTC = this . args . invite . createdAtUTC ;
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
get expiresAt ( ) {
2022-08-30 18:09:08 +03:00
const expires = this . args . invite . expires ;
2016-09-26 19:03:53 +03:00
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
get isExpired ( ) {
2022-08-30 18:09:08 +03:00
const expires = this . args . invite . expires ;
const now = ( new Date ( ) ) . valueOf ( ) ;
2017-02-27 16:11:39 +03:00
return expires < now ;
2022-02-01 12:34:03 +03:00
}
@ action
2022-08-30 18:35:28 +03:00
resend ( event ) {
event ? . preventDefault ( ) ;
2022-08-30 18:09:08 +03:00
const invite = this . args . invite ;
const notifications = this . notifications ;
2022-02-01 12:34:03 +03:00
2022-08-30 18:09:08 +03:00
this . isSending = true ;
2022-02-01 12:34:03 +03:00
invite . resend ( ) . then ( ( result ) => {
2022-08-30 18:09:08 +03:00
const notificationText = ` Invitation resent! ( ${ invite . email } ) ` ;
2022-02-01 12:34:03 +03:00
// 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 ( ( ) => {
2022-08-30 18:09:08 +03:00
this . isSending = false ;
2022-02-01 12:34:03 +03:00
} ) ;
}
@ action
2022-08-30 18:35:28 +03:00
revoke ( event ) {
event ? . preventDefault ( ) ;
2022-08-30 18:09:08 +03:00
const invite = this . args . invite ;
const email = invite . email ;
const notifications = this . notifications ;
2022-02-01 12:34:03 +03:00
// 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 ) ) {
2022-08-30 18:09:08 +03:00
this . args . reload ? . ( ) ;
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
}