Project

General

Profile

1
import {Component, OnInit, Input}                        from '@angular/core';
2
import {SimpleChanges, OnChanges}                        from '@angular/core';
3
import {FormGroup, FormArray, FormBuilder, Validators}   from "@angular/forms";
4
import {ActivatedRoute, Router}                          from '@angular/router';
5

    
6
import {HelpContentService}                              from "../../../services/help-content.service";
7
import {CommunityService}                                from "../../../openaireLibrary/connect/community/community.service";
8
import {EnvProperties}                                   from '../../../openaireLibrary/utils/properties/env-properties';
9

    
10
import {CommunityInfo}                                   from '../../../openaireLibrary/connect/community/communityInfo';
11

    
12
@Component({
13
    selector: 'community-edit-form',
14
    templateUrl: './community-edit-form.component.html',
15
})
16

    
17
export class CommunityEditFormComponent implements OnInit{
18

    
19
    @Input('group')
20
    myForm: FormGroup;
21

    
22
    public showLoading: boolean = true;
23
    public errorMessage: string = '';
24
    public updateErrorMessage: string = '';
25

    
26
    public successfulSaveMessage: string = '';
27
    public successfulResetMessage: string = '';
28

    
29
    public hasChanged: boolean = false;
30

    
31
    public res=[];
32

    
33
    params: any;
34

    
35
    public communityId = null;
36
    public community = null;
37
    public properties:EnvProperties = null;
38

    
39
    constructor (private route: ActivatedRoute,
40
                 private _router: Router,
41
                 public _fb: FormBuilder,
42
                 private _helpContentService: HelpContentService,
43
                 private _communityService: CommunityService){ }
44

    
45

    
46
    ngOnInit() {
47
        this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
48
            this.properties = data.envSpecific;
49
            this.route.queryParams.subscribe(
50
              communityId => {
51
                    this.communityId = communityId['communityId'];
52

    
53
                    if (this.communityId != null && this.communityId != '') {
54
                        this.showLoading = true;
55
                        this.updateErrorMessage = "";
56
                        this.errorMessage = "";
57

    
58
                        this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
59
                          community => {
60
                                this.community = community;
61
                                this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
62
                                // TODO remove after testing!!!!!!!
63
                                console.log(community);
64
                                this.showLoading = false;
65
                          },
66
                          error => this.handleError('System error retrieving community profile', error)
67
                        );
68
                    }
69
             });
70
        });
71
    }
72

    
73
    public addManager() {
74
            this.community.managers.push("");
75
    }
76

    
77
    public removeManager(i : any) {
78
            this.community.managers.splice(i,1);
79
    }
80

    
81
    // public addSubject() {
82
    //         this.community.subjects.push("");
83
    // }
84
    //
85
    // public removeSubject(i : any) {
86
    //         this.community.subjects.splice(i,1);
87
    // }
88

    
89
    public resetForm(communityId: string) {
90
        if (communityId != null && communityId != '') {
91
            this.showLoading = true;
92
            this.updateErrorMessage = "";
93
            this.errorMessage = "";
94

    
95
            this._communityService.getCommunity(this.properties, this.properties.communityAPI+communityId).subscribe (
96
              community => {
97
                    this.community = community;
98
                    this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
99
                    this.showLoading = false;
100
                    this.handleSuccessfulReset('Form reseted!')
101
              },
102
              error => this.handleError('System error retrieving community profile', error)
103
            );
104
        }
105
        this.resetChange();
106
    }
107

    
108
    public updateCommunity() {
109

    
110
        if (this.communityId != null && this.communityId != '') {
111
            this.showLoading = true;
112
            var community = this.parseUpdatedCommunity();
113

    
114
            // TODO delete after testing
115
            console.log(community);
116

    
117
            if (!this.hasValidEmail(community) || !this.hasFilled(community["name"])) {
118
              //console.info("NAVIGAAAATE");
119
                this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
120
            } else {
121
                this._communityService.updateCommunity(this.properties.communityAPI+this.communityId, community).subscribe(
122
                  community => {
123
                     this.handleSuccessfulSave('Community saved!')
124
                  },
125
                  error => this.handleUpdateError('System error updating community profile', error)
126
                );
127
                this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
128
            }
129
        }
130
        this.resetChange();
131
    }
132

    
133
    private parseUpdatedCommunity() : {} {
134
        var community = {};
135

    
136
        community["name"] = this.community.title;
137
        community["shortName"] = this.community.shortTitle;
138
        community["status"] = this.community.status;
139
        community["description"] = this.community.description;
140
        community["logoUrl"] = this.community.logoUrl;
141

    
142
        // TODO delete after testing
143
        //console.log(this.community.managers);
144

    
145
        community['managers'] = new Array<string>();
146
        this.community.managers = this.getNonEmptyItems(this.community.managers);
147
        community['managers'] = this.community.managers;
148

    
149
        // TODO delete after testing
150
        //console.log(this.community.subjects);
151

    
152
        // community['subjects'] = new Array<string>();
153
        // this.community.subjects = this.getNonEmptyItems(this.community.subjects);
154
        // community['subjects'] = this.community.subjects;
155

    
156
        // TODO delete after testing
157
        //console.log(community);
158
        return community;
159
    }
160

    
161
    private getNonEmptyItems(data: string[]): string[] {
162
        let length = data.length;
163
        let arrayNonEmpty = new Array<string>();
164

    
165
        let j = 0;
166
        for (let i = 0; i < length; i++) {
167
            if (this.isEmpty(data[i])) {
168
                 //console.log(data[i]);
169
            } else if (this.isNonEmpty(data[i])) {
170
                 arrayNonEmpty[j] = data[i];
171
                 j++;
172
                 //console.log(data[i]);
173
            }
174
        }
175
        return arrayNonEmpty;
176
    }
177

    
178
    private hasFilled(data: any): boolean {
179
        if (this.isNonEmpty(data) && !this.isEmpty(data)) {
180
            // TODO remove console message after final testing
181
            console.log("NAME IS NOT EMPTY");
182
            return true;
183
        }
184
        // TODO remove console message after final testing
185
        console.log("NAME IS EMPTY");
186
        return false;
187
    }
188

    
189
    private isEmpty(data: string): boolean {
190
        if (data != undefined && !data.replace(/\s/g, '').length)
191
            return true;
192
        else
193
            return false;
194
    }
195

    
196
    private isNonEmpty(data: string): boolean {
197
        if (data != undefined && data != null)
198
            return true;
199
        else
200
            return false;
201
    }
202

    
203
    private hasValidEmail(data: any): boolean {
204
        let length = data['managers'].length;
205

    
206
        for(let i = 0; i < length; i++) {
207
            if (!this.emailValidator(data['managers'][i])){
208
                // TODO remove console message after final testing
209
                console.log("INVALID EMAIL");
210
                return false;
211
            }
212
        }
213
        // TODO remove console message after final testing
214
        console.log("ALL EMAILS ARE VALID");
215
        return true;
216
    }
217

    
218
    private emailValidator(email : any): boolean {
219
        if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
220
            return true;
221
        else
222
            return false;
223
    }
224

    
225
    private change() {
226
        this.hasChanged = true;
227
        this.successfulSaveMessage = '';
228
        this.successfulResetMessage = '';
229
        // TODO remove after testing
230
        console.log('I have changed: I AM TRUE');
231
    }
232

    
233
    private resetChange() {
234
        this.hasChanged = false;
235
        // TODO remove after testing
236
        console.log('I have changed: I AM FALSE');
237
    }
238

    
239
    public get form() {
240
        return this._fb.group({
241
            _id : '',
242
            name : ['', Validators.required]
243
        });
244
    }
245

    
246
    public reset() {
247
        this.myForm.patchValue({
248
            name : '',
249
            _id : ''
250
        });
251
    }
252

    
253
    handleUpdateError(message: string, error) {
254
        this.updateErrorMessage = message;
255
        console.log('Server responded: ' +error);
256

    
257
        this.showLoading = false;
258
    }
259

    
260
    handleError(message: string, error) {
261
        this.errorMessage = message;
262
        console.log('Server responded: ' + error);
263

    
264
        this.showLoading = false;
265
    }
266

    
267
    handleSuccessfulSave(message) {
268
        this.showLoading = false;
269
        this.successfulSaveMessage = message;
270
    }
271

    
272
    handleSuccessfulReset(message) {
273
        this.showLoading = false;
274
        this.successfulResetMessage = message;
275
    }
276

    
277
    trackByFn(index: any, item: any) {
278
        return index;
279
    }
280
}
(2-2/4)