|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import moment from 'moment-timezone'; |
| 3 | +import Event from 'open-event-frontend/models/event'; |
| 4 | +import { inject as service } from '@ember/service'; |
| 5 | +import { tracked } from '@glimmer/tracking'; |
| 6 | +import { hasSessions } from 'open-event-frontend/utils/event'; |
| 7 | +import AuthManagerService from 'open-event-frontend/services/auth-manager'; |
| 8 | +import VideoStream from 'open-event-frontend/models/video-stream'; |
| 9 | +import Loader from 'open-event-frontend/services/loader'; |
| 10 | +import { action } from '@ember/object'; |
| 11 | +import EventService from 'open-event-frontend/services/event'; |
| 12 | + |
| 13 | +interface Args { |
| 14 | + videoStream: VideoStream; |
| 15 | + event: Event; |
| 16 | + hasStreams: boolean; |
| 17 | + canAccess: boolean; |
| 18 | + showSidePanel: () => void; |
| 19 | + location: string |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +export default class AddToCalender extends Component<Args> { |
| 24 | + @service |
| 25 | + authManager!: AuthManagerService; |
| 26 | + |
| 27 | + @tracked showSessions : any; |
| 28 | + |
| 29 | + @tracked isAddToCalendarModalOpen = false; |
| 30 | + |
| 31 | + @tracked modalUrls : { name: string; url: string; }[] = []; |
| 32 | + @service router: any; |
| 33 | + @service loader!: Loader; |
| 34 | + @service confirm: any; |
| 35 | + @service l10n: any; |
| 36 | + @service session : any; |
| 37 | + @service declare event: EventService; |
| 38 | + |
| 39 | + get description(): string { |
| 40 | + const { event } = this.args; |
| 41 | + let desc = `Join the event at <a href = "${event.url}">${event.url}</a>\n `; |
| 42 | + if (event.description) { |
| 43 | + desc = desc + `<section><h3>Event Description </h3>\n${event.description}\n</section>`; |
| 44 | + } |
| 45 | + if (event.ownerDescription) { |
| 46 | + desc = desc + `<section><h3>Organizer Message </h3>\n${event.ownerDescription}\n</section>`; |
| 47 | + } |
| 48 | + return desc; |
| 49 | + } |
| 50 | + |
| 51 | + constructor(owner: null, args: Args) { |
| 52 | + super(owner, args); |
| 53 | + this.checkSessions(); |
| 54 | + } |
| 55 | + |
| 56 | + openAddToCalendarModal(calendarUrls: { name: string; url: string; }[]): void { |
| 57 | + this.isAddToCalendarModalOpen = true; |
| 58 | + this.modalUrls = calendarUrls; |
| 59 | + } |
| 60 | + |
| 61 | + async checkSessions(): Promise<void> { |
| 62 | + const { event } = this.args; |
| 63 | + this.showSessions = this.showSessions ?? await hasSessions(this.loader, event); |
| 64 | + } |
| 65 | + |
| 66 | + get startsAt(): moment.Moment { |
| 67 | + const { event } = this.args; |
| 68 | + return moment(event.startsAt).tz(event.timezone); |
| 69 | + } |
| 70 | + |
| 71 | + get endsAt(): moment.Moment { |
| 72 | + const { event } = this.args; |
| 73 | + return moment(event.endsAt).tz(event.timezone); |
| 74 | + } |
| 75 | + |
| 76 | + get calendarLocation(): string { |
| 77 | + return this.args.event.online ? this.args.event.url : this.args.location; |
| 78 | + } |
| 79 | + |
| 80 | + get googleUrl(): string { |
| 81 | + const { event } = this.args; |
| 82 | + const startTime = this.startsAt.utc().format('YYYYMMDD[T]HHmmSS[Z]'); |
| 83 | + const endTime = this.endsAt.utc().format('YYYYMMDD[T]HHmmSS[Z]'); |
| 84 | + return `https://calendar.google.com/calendar/render?action=TEMPLATE&dates=${startTime}/${endTime}&text=${event.name}&location=${this.calendarLocation}&ctz=${event.timezone}&details=${this.description}`; |
| 85 | + } |
| 86 | + |
| 87 | + get yahooUrl(): string { |
| 88 | + const { event } = this.args; |
| 89 | + const startTime = this.startsAt.format('YYYYMMDD[T]HHmmSS'); |
| 90 | + const endTime = this.endsAt.format('YYYYMMDD[T]HHmmSS'); |
| 91 | + return `https://calendar.yahoo.com/?v=60&title=${event.name}&st=${startTime}&et=${endTime}&desc=${this.description.replace(/<[^>]+>/g, '')}&in_loc=${this.calendarLocation}`; |
| 92 | + } |
| 93 | + |
| 94 | + get outlookUrl(): string { |
| 95 | + const { event } = this.args; |
| 96 | + const startTime = this.startsAt.utc().format('YYYY[-]MM[-]DDTHH[:]mm[:]SS[Z]'); |
| 97 | + const endTime = this.endsAt.utc().format('YYYY[-]MM[-]DDTHH[:]mm[:]SS[Z]'); |
| 98 | + return `https://outlook.live.com/calendar/0/deeplink/compose?subject=${event.name}&startdt=${startTime}&enddt=${endTime}&body=${(this.description).substring(0, 1000)}&location=${this.calendarLocation}`; |
| 99 | + } |
| 100 | + |
| 101 | + get iCalUrl(): string { |
| 102 | + const host = this.loader.host(); |
| 103 | + return host + '/v1/events/' + this.args.event.identifier + '.ics?download'; |
| 104 | + } |
| 105 | + |
| 106 | + get calendarUrls(): { name: string; url: string; }[] { |
| 107 | + return [{ name: 'Google Calendar', url: this.googleUrl }, { name: 'iCal', url: this.iCalUrl }, { name: 'Yahoo', url: this.yahooUrl }, { name: 'Outlook', url: this.outlookUrl }]; |
| 108 | + } |
| 109 | + |
| 110 | + get sessionGoogleUrl(): string { |
| 111 | + const { event } = this.args; |
| 112 | + let host = this.loader.host(); |
| 113 | + host = host.replace('https://', '') |
| 114 | + return 'https://calendar.google.com/calendar/render?cid=webcal://' + host + '/v1/events/' + event.identifier + '.ics?include_sessions'; |
| 115 | + } |
| 116 | + |
| 117 | + get mySessionGoogleUrl(): string { |
| 118 | + const { event } = this.args; |
| 119 | + let host = this.loader.host(); |
| 120 | + host = host.replace('https://', ''); |
| 121 | + return 'https://calendar.google.com/calendar/render?cid=webcal://' + host + '/v1/events/' + event.identifier + encodeURIComponent('.ics?include_sessions&my_schedule&user_id=') + this.authManager.currentUser.id; |
| 122 | + } |
| 123 | + |
| 124 | + get mySessioniCalUrl(): string { |
| 125 | + const host = this.loader.host(); |
| 126 | + return host + '/v1/events/' + this.args.event.identifier + '.ics?include_sessions&my_schedule&user_id=' + this.authManager.currentUser.id; |
| 127 | + } |
| 128 | + |
| 129 | + get sessioniCalUrl(): string { |
| 130 | + const host = this.loader.host(); |
| 131 | + return host + '/v1/events/' + this.args.event.identifier + '.ics?include_sessions'; |
| 132 | + } |
| 133 | + |
| 134 | + get sessionCalendarUrls(): { name: string; url: string; }[] { |
| 135 | + return [{ name: 'Google Calendar', url: this.sessionGoogleUrl }, { name: 'iCal', url: this.sessioniCalUrl }]; |
| 136 | + } |
| 137 | + |
| 138 | + get mySessionCalendarUrls(): { name: string; url: string; }[] { |
| 139 | + return [{ name: 'Google Calendar', url: this.mySessionGoogleUrl }, { name: 'iCal', url: this.mySessioniCalUrl }]; |
| 140 | + } |
| 141 | + |
| 142 | + @action |
| 143 | + openPanel(): void { |
| 144 | + if (this.args.canAccess) { |
| 145 | + this.args.showSidePanel?.(); |
| 146 | + this.router.transitionTo({ queryParams: { side_panel: true } }); |
| 147 | + } else { |
| 148 | + if (this.session.isAuthenticated) { |
| 149 | + this.router.transitionTo('public', this.args.event, { queryParams: { video_dialog: true } }); |
| 150 | + } else { |
| 151 | + this.router.transitionTo({ queryParams: { video_dialog: true } }); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments