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