Project

General

Profile

1
import {Component, OnDestroy, OnInit, ViewChild} from "@angular/core";
2
import {CommunityInfo} from "../../../openaireLibrary/connect/community/communityInfo";
3
import {EnvProperties} from "../../../openaireLibrary/utils/properties/env-properties";
4
import {properties} from "../../../../environments/environment";
5
import {EditCommunityComponent} from "./edit-community/edit-community.component";
6
import {CommunityService} from "../../../openaireLibrary/connect/community/community.service";
7
import {Title} from "@angular/platform-browser";
8
import {Subscription} from "rxjs";
9

    
10
@Component({
11
  selector: 'community-profile',
12
  template: `
13
    <div page-content>
14
      <div header>
15
        <community-info tab="profile"></community-info>
16
      </div>
17
      <div inner>
18
        <div class="uk-card-header">
19
          <div class="uk-flex uk-child-width-1-1 uk-child-width-1-2@m uk-grid" uk-grid>
20
            <div>
21
              <div class="uk-text-small title">
22
                Manage Community Profile
23
              </div>
24
              <div>
25
                <span *ngIf="community" class="uk-text-bold">{{community.shortTitle}}</span>
26
                <span *ngIf="editCommunityComponent.dirty && !loading"> (unsaved changes)</span>
27
              </div>
28
            </div>
29
            <div class="uk-text-right@m uk-text-center">
30
              <button class="uk-button uk-button-secondary outlined uk-margin-right"
31
                      (click)="reset()"
32
                      [disabled]="loading || !editCommunityComponent.dirty">Reset
33
              </button>
34
              <button class="uk-button uk-button-secondary"
35
                      (click)="save()"
36
                      [disabled]="loading || editCommunityComponent.disabled">Save
37
              </button>
38
            </div>
39
          </div>
40
        </div>
41
        <div class="uk-card uk-card-default uk-position-relative" style="min-height: 60vh">
42
          <div [class.hidden]="loading">
43
            <edit-community #editCommunityComponent [maxHeight]="'60vh'" [paddingLarge]="true"></edit-community>
44
          </div>
45
          <div *ngIf="loading" class="uk-position-center">
46
            <loading></loading>
47
          </div>
48
        </div>
49
      </div>
50
    </div>
51
  `
52
})
53
export class ProfileComponent implements OnInit, OnDestroy {
54
  
55
  public community: CommunityInfo;
56
  public properties: EnvProperties = properties;
57
  public loading: boolean = false;
58
  private subscriptions: any[] = [];
59
  @ViewChild('editCommunityComponent', { static: true }) editCommunityComponent: EditCommunityComponent;
60
  
61
  constructor(private communityService: CommunityService,
62
              private title: Title) {
63
  }
64
  
65
  ngOnInit() {
66
    this.loading = true;
67
    this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
68
      this.community = community;
69
      if (this.community) {
70
        this.title.setTitle(this.community.communityId.toUpperCase() + " | Profile");
71
        setTimeout(() => {
72
          this.reset();
73
          this.loading = false;
74
        }, 200);
75
      }
76
    }));
77
  }
78
  
79
  public reset() {
80
    this.editCommunityComponent.init(this.community);
81
  }
82
  
83
  public save() {
84
    this.loading = true;
85
    this.editCommunityComponent.save((community) => {
86
      this.community = community;
87
      this.reset();
88
      this.loading = false;
89
    }, (error) => {
90
      this.loading = false;
91
    });
92
  }
93
  
94
  ngOnDestroy() {
95
    this.subscriptions.forEach(subscription => {
96
      if (subscription instanceof Subscription) {
97
        subscription.unsubscribe();
98
      }
99
    });
100
  }
101
}
(1-1/2)