2021-12-17 15:57:11 +01:00
|
|
|
import { Component, OnInit } from "@angular/core";
|
2022-02-24 12:10:07 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
|
|
|
import { OrganizationService } from "jslib-common/abstractions/organization.service";
|
|
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
|
|
|
import { SyncService } from "jslib-common/abstractions/sync.service";
|
|
|
|
import { PlanSponsorshipType } from "jslib-common/enums/planSponsorshipType";
|
|
|
|
import { Organization } from "jslib-common/models/domain/organization";
|
2021-11-22 14:41:40 +01:00
|
|
|
|
|
|
|
@Component({
|
2021-12-17 15:57:11 +01:00
|
|
|
selector: "app-sponsored-families",
|
|
|
|
templateUrl: "sponsored-families.component.html",
|
2021-11-22 14:41:40 +01:00
|
|
|
})
|
|
|
|
export class SponsoredFamiliesComponent implements OnInit {
|
2021-12-17 15:57:11 +01:00
|
|
|
loading = false;
|
|
|
|
|
|
|
|
availableSponsorshipOrgs: Organization[] = [];
|
|
|
|
activeSponsorshipOrgs: Organization[] = [];
|
2022-02-24 12:10:07 +01:00
|
|
|
selectedSponsorshipOrgId = "";
|
|
|
|
sponsorshipEmail = "";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
|
|
|
// Conditional display properties
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private syncService: SyncService,
|
|
|
|
private organizationService: OrganizationService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
await this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
|
|
|
this.formPromise = this.apiService.postCreateSponsorship(this.selectedSponsorshipOrgId, {
|
|
|
|
sponsoredEmail: this.sponsorshipEmail,
|
|
|
|
planSponsorshipType: PlanSponsorshipType.FamiliesForEnterprise,
|
|
|
|
friendlyName: this.sponsorshipEmail,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.formPromise;
|
|
|
|
this.platformUtilsService.showToast("success", null, this.i18nService.t("sponsorshipCreated"));
|
|
|
|
this.formPromise = null;
|
|
|
|
this.resetForm();
|
|
|
|
await this.load(true);
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:10:07 +01:00
|
|
|
async load(forceReload = false) {
|
2021-12-17 15:57:11 +01:00
|
|
|
if (this.loading) {
|
|
|
|
return;
|
2021-11-22 14:41:40 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
this.loading = true;
|
|
|
|
if (forceReload) {
|
|
|
|
await this.syncService.fullSync(true);
|
2021-11-22 14:41:40 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
const allOrgs = await this.organizationService.getAll();
|
|
|
|
this.availableSponsorshipOrgs = allOrgs.filter((org) => org.familySponsorshipAvailable);
|
2021-11-22 14:41:40 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
this.activeSponsorshipOrgs = allOrgs.filter(
|
|
|
|
(org) => org.familySponsorshipFriendlyName !== null
|
|
|
|
);
|
2021-11-22 14:41:40 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
if (this.availableSponsorshipOrgs.length === 1) {
|
|
|
|
this.selectedSponsorshipOrgId = this.availableSponsorshipOrgs[0].id;
|
2021-11-22 14:41:40 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
this.loading = false;
|
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
private async resetForm() {
|
|
|
|
this.sponsorshipEmail = "";
|
|
|
|
this.selectedSponsorshipOrgId = "";
|
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
get anyActiveSponsorships(): boolean {
|
|
|
|
return this.activeSponsorshipOrgs.length > 0;
|
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
get anyOrgsAvailable(): boolean {
|
|
|
|
return this.availableSponsorshipOrgs.length > 0;
|
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
get moreThanOneOrgAvailable(): boolean {
|
|
|
|
return this.availableSponsorshipOrgs.length > 1;
|
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
}
|