2023-02-18 09:52:24 +01:00
|
|
|
import {ChangeDetectorRef, Component, Input, ViewChild} from "@angular/core";
|
2023-02-17 18:08:44 +01:00
|
|
|
import {FormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
|
|
|
|
import {SdgSelectionComponent} from "../../../sdg/sdg-selection/sdg-selection.component";
|
2023-02-20 15:34:12 +01:00
|
|
|
import {FosSelectionComponent} from "../../../fos/fos-selection/fos-selection.component";
|
2023-02-17 18:08:44 +01:00
|
|
|
import {properties} from "../../../../../environments/environment";
|
|
|
|
import {EnvProperties} from "../../../utils/properties/env-properties";
|
|
|
|
import {EmailService} from "../../../utils/email/email.service";
|
|
|
|
import {Subscription} from "rxjs";
|
|
|
|
import {Composer} from "../../../utils/email/composer";
|
2023-02-18 09:52:24 +01:00
|
|
|
import {AlertModal} from "../../../utils/modal/alert";
|
|
|
|
import {StringUtils} from "../../../utils/string-utils.class";
|
2023-02-17 18:08:44 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'sdg-fos-suggest',
|
|
|
|
template: `
|
2023-02-18 09:52:24 +01:00
|
|
|
<modal-alert #selectionModal [large]="true" (alertOutput)="modalOutput()" (cancelOutput)="modalCancel()"
|
|
|
|
[okDisabled]="!sent && !selectionStep1 && (form.invalid || sending)">
|
2023-02-20 19:54:26 +01:00
|
|
|
<sdg-selection *ngIf="subjects && subjectType == 'sdg'" #selection [class.uk-hidden]="!selectionStep1"
|
2023-02-20 15:34:12 +01:00
|
|
|
[subjects]="subjects" [entityType]="entityType"></sdg-selection>
|
2023-02-20 19:54:26 +01:00
|
|
|
<fos-selection *ngIf="subjects && subjectType == 'fos'" #selection [class.uk-hidden]="!selectionStep1"
|
|
|
|
[subjects]="subjects" [contentHeight]="selectionModal.bodyHeight" [inModal]="true"></fos-selection>
|
2023-02-18 09:52:24 +01:00
|
|
|
<div [class.uk-hidden]="selectionStep1">
|
|
|
|
<div class="uk-flex uk-flex-column uk-flex-middle">
|
|
|
|
<ng-container *ngIf="!sent && !error">
|
|
|
|
<div>Thank you for your feedback.</div>
|
|
|
|
<div>Before sending us your options, would you like to leave us your e-mail to notify you about the reporting status?</div>
|
|
|
|
<div input class="uk-width-1-2 uk-margin-medium-top uk-margin-medium-bottom"
|
|
|
|
[formInput]="form.get('email')" placeholder="E-mail">
|
2023-02-20 15:34:12 +01:00
|
|
|
<span note>(Optional)</span>
|
2023-02-18 09:52:24 +01:00
|
|
|
</div>
|
|
|
|
<div>
|
2023-02-20 19:54:26 +01:00
|
|
|
<re-captcha (resolved)="handleRecaptcha($event)" [siteKey]="properties.reCaptchaSiteKey"
|
|
|
|
[ngClass]="sending ? 'uk-hidden':''"></re-captcha>
|
|
|
|
<loading [ngClass]="sending ? '':'uk-hidden'"></loading>
|
2023-02-18 09:52:24 +01:00
|
|
|
</div>
|
|
|
|
</ng-container>
|
|
|
|
<ng-container *ngIf="sent">
|
|
|
|
<p>Your feedback is successfully received and it will soon be reviewed by our graph experts!</p>
|
|
|
|
<icon customClass="uk-text-background" name="check" [ratio]="4"></icon>
|
|
|
|
</ng-container>
|
|
|
|
<div *ngIf="error" class="uk-alert uk-alert-danger uk-text-center uk-width-large ng-star-inserted"
|
|
|
|
role="alert">Email sent failed! Please try again.
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</modal-alert>
|
|
|
|
`
|
2023-02-17 18:08:44 +01:00
|
|
|
})
|
|
|
|
export class SdgFosSuggestComponent {
|
2023-02-18 09:52:24 +01:00
|
|
|
@Input() entityType: string;
|
2023-02-17 18:08:44 +01:00
|
|
|
@Input() title;
|
2023-02-18 09:52:24 +01:00
|
|
|
public subjectType: "fos" | "sdg" = "sdg";
|
|
|
|
public subjects;
|
2023-02-17 18:08:44 +01:00
|
|
|
public properties: EnvProperties = properties;
|
2023-02-18 09:52:24 +01:00
|
|
|
public selectionStep1: boolean = true;
|
|
|
|
@ViewChild("selectionModal") selectionModal: AlertModal;
|
2023-02-20 15:34:12 +01:00
|
|
|
@ViewChild("selection") selection: SdgSelectionComponent | FosSelectionComponent;
|
2023-02-17 18:08:44 +01:00
|
|
|
public form: UntypedFormGroup;
|
2023-02-18 09:52:24 +01:00
|
|
|
public url: string = null;
|
2023-02-17 18:08:44 +01:00
|
|
|
|
2023-02-18 09:52:24 +01:00
|
|
|
public sending: boolean = false;
|
|
|
|
public sent: boolean = false;
|
|
|
|
public error: boolean = false;
|
2023-02-17 18:08:44 +01:00
|
|
|
subscriptions: Subscription[] = [];
|
|
|
|
|
2023-02-18 09:52:24 +01:00
|
|
|
constructor(private emailService: EmailService, private fb: FormBuilder, private cdr: ChangeDetectorRef) {}
|
2023-02-17 18:08:44 +01:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
if (typeof window !== "undefined") {
|
2023-02-18 09:52:24 +01:00
|
|
|
this.url = window.location.href;
|
2023-02-17 18:08:44 +01:00
|
|
|
}
|
2023-02-18 09:52:24 +01:00
|
|
|
this.init();
|
2023-02-17 18:08:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscription) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-18 09:52:24 +01:00
|
|
|
init() {
|
|
|
|
this.form = this.fb.group({
|
|
|
|
name: this.fb.control(this.title),
|
|
|
|
url: this.fb.control(this.url),
|
|
|
|
email: this.fb.control('', Validators.email),
|
|
|
|
subjects: this.fb.control([]),
|
|
|
|
recaptcha: this.fb.control('', Validators.required),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public openSelectionModal() {
|
|
|
|
this.sent = false;
|
|
|
|
this.sending = false;
|
|
|
|
this.error = false;
|
|
|
|
this.selectionStep1 = true;
|
|
|
|
this.init();
|
|
|
|
|
|
|
|
this.selectionModal.cancelButton = false;
|
|
|
|
if(this.subjectType == "sdg") {
|
|
|
|
this.selectionModal.alertTitle = "Please select SDGs that are the most relevant for this "+this.getEntityName(this.entityType)+".";
|
|
|
|
} else {
|
|
|
|
this.selectionModal.alertTitle = "Please select Fields of Science that are the most relevant for this "+this.getEntityName(this.entityType)+".";
|
|
|
|
}
|
|
|
|
this.selectionModal.okButtonText = "Next";
|
|
|
|
this.selectionModal.stayOpen = true;
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
this.selectionModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
public modalOutput() {
|
|
|
|
this.selectionModal.previousButton = true;
|
|
|
|
this.selectionModal.okButtonText = "Send feedback";
|
|
|
|
|
|
|
|
if(this.selectionStep1) {
|
|
|
|
this.selectionStep1 = false;
|
|
|
|
if(this.subjectType == "sdg") {
|
|
|
|
this.selectionModal.alertTitle = "Please send your feedback on most relevant SDGs for this "+this.getEntityName(this.entityType)+".";
|
|
|
|
} else {
|
|
|
|
this.selectionModal.alertTitle = "Please send your feedback on most relevant Fields of Science for this "+this.getEntityName(this.entityType)+".";
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-20 19:54:26 +01:00
|
|
|
this.sending = true;
|
2023-02-17 18:08:44 +01:00
|
|
|
// email functionality
|
2023-02-18 09:52:24 +01:00
|
|
|
this.form.get("subjects").setValue(this.selection.getSelectedSubjects().map(subject => subject.id));
|
2023-02-17 18:08:44 +01:00
|
|
|
this.subscriptions.push(this.emailService.contact(this.properties,
|
2023-02-18 09:52:24 +01:00
|
|
|
Composer.composeEmailForSdgsSuggestion(this.form.value, [this.properties.feedbackmail], this.subjectType), this.form.get('recaptcha').value).subscribe(sent => {
|
|
|
|
this.error = !sent;
|
2023-02-17 18:08:44 +01:00
|
|
|
if (sent) {
|
|
|
|
if (this.form.get('email').value !== '') {
|
|
|
|
this.subscriptions.push(this.emailService.contact(this.properties,
|
|
|
|
Composer.composeEmailForUserAfterFeedback([this.form.get('email').value])).subscribe(sent => {
|
|
|
|
if (sent) {
|
|
|
|
//console.log('An email has been sent to user ' + this.form.get('email').value);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2023-02-18 09:52:24 +01:00
|
|
|
this.init();
|
|
|
|
this.sent = true;
|
|
|
|
|
|
|
|
this.selectionModal.alertTitle = "Thank you for your feedback";
|
|
|
|
this.selectionModal.okButtonText = "OK";
|
|
|
|
this.selectionModal.previousButton = false;
|
|
|
|
this.selectionModal.stayOpen = false;
|
2023-02-17 18:08:44 +01:00
|
|
|
}
|
2023-02-20 19:54:26 +01:00
|
|
|
this.sending = false;
|
2023-02-17 18:08:44 +01:00
|
|
|
}, error => {
|
|
|
|
console.log(error);
|
2023-02-18 09:52:24 +01:00
|
|
|
this.error = true;
|
|
|
|
this.sending = false;
|
2023-02-17 18:08:44 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 09:52:24 +01:00
|
|
|
public modalCancel() {
|
|
|
|
if(this.subjectType == "sdg") {
|
|
|
|
this.selectionModal.alertTitle = "Please select SDGs that are the most relevant for this "+this.getEntityName(this.entityType)+".";
|
|
|
|
} else {
|
|
|
|
this.selectionModal.alertTitle = "Please select Fields of Science that are the most relevant for this "+this.getEntityName(this.entityType)+".";
|
|
|
|
}
|
|
|
|
this.selectionStep1 = true;
|
|
|
|
this.selectionModal.previousButton = false;
|
|
|
|
this.selectionModal.okButtonText = "Next";
|
|
|
|
this.selectionModal.stayOpen = true;
|
|
|
|
this.error = false;
|
|
|
|
}
|
|
|
|
|
2023-02-17 18:08:44 +01:00
|
|
|
public handleRecaptcha(captchaResponse: string) {
|
|
|
|
this.form.get('recaptcha').setValue(captchaResponse);
|
|
|
|
}
|
2023-02-18 09:52:24 +01:00
|
|
|
|
|
|
|
private getEntityName (entityType:string) {
|
|
|
|
return StringUtils.getEntityName(entityType, false);
|
|
|
|
}
|
2023-02-17 18:08:44 +01:00
|
|
|
}
|