Project

General

Profile

« Previous | Next » 

Revision 60310

[Library | Trunk]: Admin tools pages, fix modals and update behaviour

View differences:

portals.component.ts
1
import {Component, ViewChild, OnInit, ElementRef} from '@angular/core';
1
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
2 2
import {ActivatedRoute, Router} from '@angular/router';
3 3
import {HelpContentService} from '../../services/help-content.service';
4
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
4
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
5 5
import {EnvProperties} from '../../utils/properties/env-properties';
6 6

  
7 7
import {Session} from '../../login/utils/helper.class';
......
11 11
import {CheckPortal, Portal} from "../../utils/entities/adminTool/portal";
12 12
import {PortalUtils} from "./portalHelper";
13 13
import {properties} from "../../../../environments/environment";
14
import {CheckPage, Page} from "../../utils/entities/adminTool/page";
15 14
import {AlertModal} from "../../utils/modal/alert";
16 15

  
17 16
@Component({
......
21 20

  
22 21
export class PortalsComponent implements OnInit {
23 22
  
24
  @ViewChild('portalModal') portalModal: AlertModal;
23
  @ViewChild('editModal') editModal: AlertModal;
25 24
  @ViewChild('deleteModal') deleteModal: AlertModal;
26 25
  private selectedPortals: string[] = [];
27 26
  
28 27
  public checkboxes: CheckPortal[] = [];
29 28
  public portals: Portal[] = [];
30 29
  
31
  public portalFG: FormGroup;
30
  public portalForm: FormGroup;
32 31
  public filterForm: FormGroup;
33 32
  private subscriptions: any[] = [];
34 33
  
......
42 41
  public updateErrorMessage = '';
43 42
  public modalErrorMessage = '';
44 43
  public portalUtils: PortalUtils = new PortalUtils();
44
  private index: number;
45 45
  
46 46
  ngOnInit() {
47
    this.portalFG = this._fb.group({
48
      name: this._fb.control('', Validators.required),
49
      _id: this._fb.control(''),
50
      pid: this._fb.control('', Validators.required),
51
      piwik: this._fb.control(''),
52
      type: this._fb.control('', Validators.required),
53
    });
54 47
    this.filterForm = this._fb.group({
55 48
      keyword: [''],
56 49
      type: ['all', Validators.required]
......
122 115
  
123 116
  private deletePortalsFromArray(ids: string[]): void {
124 117
    for (let id of ids) {
125
      let i = this.checkboxes.findIndex(_ => _.portal._id === id);
126
      this.checkboxes.splice(i, 1);
118
      let i = this.portals.findIndex(_ => _._id == id);
119
      this.portals.splice(i, 1);
127 120
    }
121
    this.applyTypeFilter();
122
    this.applyFilter();
128 123
  }
129 124
  
130 125
  public confirmDeletePortal(id: string) {
......
140 135
  }
141 136
  
142 137
  private confirmModalOpen() {
143
    this.deleteModal.cancelButton = true;
144
    this.deleteModal.okButton = true;
145
    this.deleteModal.alertTitle = 'Delete Confirmation';
146
    this.deleteModal.message = 'Are you sure you want to delete the selected portal(-ies)?';
147
    this.deleteModal.okButtonText = 'Yes';
148
    this.deleteModal.open();
138
    if (!Session.isLoggedIn()) {
139
      this._router.navigate(['/user-info'], {
140
        queryParams: {
141
          "errorCode": LoginErrorCodes.NOT_VALID,
142
          "redirectUrl": this._router.url
143
        }
144
      });
145
    } else {
146
      this.deleteModal.cancelButton = true;
147
      this.deleteModal.okButton = true;
148
      this.deleteModal.alertTitle = 'Delete Confirmation';
149
      this.deleteModal.message = 'Are you sure you want to delete the selected portal(-ies)?';
150
      this.deleteModal.okButtonText = 'Yes';
151
      this.deleteModal.open();
152
    }
149 153
  }
150 154
  
151 155
  public confirmedDeletePortals(data: any) {
......
169 173
  
170 174
  public editPortal(i: number) {
171 175
    const portal: Portal = this.checkboxes[i].portal;
172
    this.portalFG = this._fb.group({
176
    this.index = this.portals.findIndex(value => value._id === portal._id);
177
    this.portalForm = this._fb.group({
178
      _id: this._fb.control(portal._id),
173 179
      name: this._fb.control(portal.name, Validators.required),
174
      _id: this._fb.control(portal._id),
175 180
      pid: this._fb.control(portal.pid, Validators.required),
176 181
      piwik: this._fb.control(portal.piwik),
177 182
      type: this._fb.control(portal.type, Validators.required),
178 183
    });
179
    this.portalFG.controls['type'].disable();
184
    this.portalForm.controls['type'].disable();
180 185
    this.modalErrorMessage = '';
181
    this.portalModalOpen('Update Portal', 'Update');
186
    this.portalModalOpen('Edit Portal', 'Save');
182 187
  }
183 188
  
184 189
  public newPortal() {
185
    this.portalFG.controls['type'].enable();
186
    this.portalFG = this._fb.group({
190
    this.portalForm.controls['type'].enable();
191
    this.portalForm = this._fb.group({
192
      _id: this._fb.control(''),
187 193
      name: this._fb.control('', Validators.required),
188
      _id: this._fb.control(''),
189 194
      pid: this._fb.control('', Validators.required),
190 195
      piwik: this._fb.control(''),
191 196
      type: this._fb.control('', Validators.required),
192 197
    });
193 198
    this.modalErrorMessage = '';
194
    this.portalModalOpen('Create Portal', 'Save');
199
    this.portalModalOpen('Create Portal', 'Create');
195 200
  }
196 201
  
197 202
  private portalModalOpen(title: string, yesBtn: string) {
198
    this.portalModal.okButtonLeft = false;
199
    this.portalModal.cancelButton = true;
200
    this.portalModal.okButton = true;
201
    this.portalModal.alertTitle = title;
202
    this.portalModal.okButtonText = yesBtn;
203
    this.portalModal.open();
203
    if (!Session.isLoggedIn()) {
204
      this._router.navigate(['/user-info'], {
205
        queryParams: {
206
          "errorCode": LoginErrorCodes.NOT_VALID,
207
          "redirectUrl": this._router.url
208
        }
209
      });
210
    } else {
211
      this.editModal.okButtonLeft = false;
212
      this.editModal.cancelButton = true;
213
      this.editModal.okButton = true;
214
      this.editModal.alertTitle = title;
215
      this.editModal.okButtonText = yesBtn;
216
      this.editModal.open();
217
    }
204 218
  }
205 219
  
206 220
  public portalSaveConfirmed(data: any) {
221
    if (!Session.isLoggedIn()) {
222
      this._router.navigate(['/user-info'], {
223
        queryParams: {
224
          "errorCode": LoginErrorCodes.NOT_VALID,
225
          "redirectUrl": this._router.url
226
        }
227
      });
228
    } else {
207 229
      this.modalErrorMessage = '';
208
      if (this.portalFG.getRawValue()['_id'].length > 0) {
209
        this.portalFG.controls['type'].enable();
210
        this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalFG.value,
230
      if (this.portalForm.value._id) {
231
        this.portalForm.controls['type'].enable();
232
        this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalForm.value,
211 233
          this.properties.adminToolsAPIURL).subscribe(
212 234
          portal => {
213 235
            this.portalUpdatedSuccessfully(portal);
......
215 237
          error => this.handleUpdateError('System error updating portal', error)
216 238
        ));
217 239
      } else {
218
        this.subscriptions.push(this._helpContentService.saveCommunity(<Portal>this.portalFG.value,
240
        this.subscriptions.push(this._helpContentService.saveCommunity(<Portal>this.portalForm.value,
219 241
          this.properties.adminToolsAPIURL).subscribe(
220 242
          portal => {
221 243
            this.portalSavedSuccessfully(portal);
......
223 245
          error => this.handleUpdateError('System error creating portal', error)
224 246
        ));
225 247
      }
248
    }
226 249
  }
227 250
  
228 251
  public portalUpdateConfirmed(data: any) {
......
231 254
        queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
232 255
      });
233 256
    } else {
234
      this.portalFG.controls['type'].enable();
235
      this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalFG.value,
257
      this.portalForm.controls['type'].enable();
258
      this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalForm.value,
236 259
        this.properties.adminToolsAPIURL).subscribe(
237 260
        portal => {
238 261
          this.portalUpdatedSuccessfully(portal);
......
243 266
  }
244 267
  
245 268
  public portalSavedSuccessfully(portal: Portal) {
246
    this.checkboxes.push(<CheckPortal>{portal: portal, checked: false});
269
    this.portals.push(portal)
270
    this.applyTypeFilter();
271
    this.applyFilter();
247 272
    this.applyCheck(false);
248 273
  }
249 274
  
250 275
  public portalUpdatedSuccessfully(portal: Portal) {
251
    this.checkboxes.find(checkItem => checkItem.portal._id === portal._id).portal = portal;
276
    this.portals[this.index] = portal;
277
    this.applyTypeFilter();
278
    this.applyFilter();
252 279
    this.applyCheck(false);
253 280
  }
254 281
  
......
283 310
  
284 311
  handleUpdateError(message: string, error) {
285 312
    if (error == null) {
286
      this.portalFG = this._fb.group({
313
      this.portalForm = this._fb.group({
287 314
        name: '',
288 315
        _id: '',
289 316
        pid: '',
......
294 321
      this.updateErrorMessage = message;
295 322
      console.log('Server responded: ' + error);
296 323
    }
297
    
298 324
    this.showLoading = false;
299 325
  }
300 326
  
301 327
  handleError(message: string, error) {
302 328
    this.errorMessage = message;
303 329
    console.log('Server responded: ' + error);
304
    
305 330
    this.showLoading = false;
306 331
  }
307 332
}

Also available in: Unified diff