Skip to content

Commit

Permalink
refacto(front) merge all strings queries #292
Browse files Browse the repository at this point in the history
  • Loading branch information
t8g committed Oct 13, 2017
1 parent 465fff6 commit aef49ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 28 additions & 4 deletions client/src/app/isari-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import flatten from 'lodash/flatten';
import zipObject from 'lodash/zipObject';
import isArray from 'lodash/isArray';
import values from 'lodash/values';
import intersection from 'lodash/intersection';

import { DatePipe } from '@angular/common';
import {saveAs} from 'file-saver';
Expand Down Expand Up @@ -56,6 +57,7 @@ export class IsariDataService {
private schemasCache = {};
private columnsCache = null;
private labelsCache = {};
private tempForeignKeys = {};

private dataUrl = `${environment.API_BASE_URL}`;
private layoutUrl = `${environment.API_BASE_URL}/layouts`;
Expand Down Expand Up @@ -578,19 +580,27 @@ export class IsariDataService {
values = [values];
}
values = values.filter(v => !!v);

if (values.length === 0) {
return Observable.of([]);
}

const url = `${this.dataUrl}/${mongoSchema2Api[feature] || feature}/${values.join(',')}/string`;
const api = mongoSchema2Api[feature] || feature;
const url = `${this.dataUrl}/${api}/`
+ (<string[]>((this.tempForeignKeys[api] && intersection(this.tempForeignKeys[api], values))
? this.tempForeignKeys[api]
: (<string[]>values))).join(',')
+ '/string';

if (!this.labelsCache[url]) {
this.labelsCache[url] = this.http.get(url, this.getHttpOptions())
.map(response => response.json())
.share();
}

return this.labelsCache[url];
return this.labelsCache[url].map(allvalues => {
return allvalues.filter(v => (<string[]>values).indexOf(v.id) !== -1);
});
}

getForeignCreate(feature) {
Expand All @@ -613,18 +623,22 @@ export class IsariDataService {
}));
}

buildForm(layout, data): FormGroup {
buildForm(layout, data, base = false): FormGroup {
let form = this.fb.group({});
let fields = layout.reduce((acc, cv) => [...acc, ...cv.fields], []);

// reset tempForeignKeys
if (base) {
this.tempForeignKeys = {};
}

// build form from object after layout manipluation
if (fields[0] instanceof Array) {
fields = fields.map(f => ({ fields: f}));
}

// normalize [[a, b ], c] -> [a, b, c]
fields = fields.reduce((acc, c) => [...acc, ...(c.fields ? c.fields : [c]) ], []);

fields.forEach(field => {
const hasData = data[field.name] !== null && data[field.name] !== undefined;
const fieldData = hasData ? data[field.name] : field.multiple ? [] : field.type === 'object' ? {} : '';
Expand All @@ -635,6 +649,7 @@ export class IsariDataService {
fa.disable();
}
fieldData.forEach((d, i) => {
this.storeForeignKeys(field.ref, d);
let subdata = Object.assign({}, d || {}, {
opts: Object.assign({}, data.opts, {
path: [...data.opts.path, field.name, i]
Expand All @@ -644,13 +659,15 @@ export class IsariDataService {
});
form.addControl(field.name, fa);
} else if (field.type === 'object') {
this.storeForeignKeys(field.ref, fieldData);
let subdata = Object.assign({}, fieldData, {
opts: Object.assign({}, data.opts, {
path: [...data.opts.path, field.name]
})
});
form.addControl(field.name, this.buildForm(field.layout, subdata));
} else {
this.storeForeignKeys(field.ref, fieldData);
form.addControl(field.name, new FormControl({
value: fieldData,
// add '.x' for multiple fields (for matching fieldName.*)
Expand All @@ -661,6 +678,13 @@ export class IsariDataService {
return form;
}

private storeForeignKeys(feature, value) {
if (!feature) return;
const api = mongoSchema2Api[feature];
value = isArray(value) ? value : [value];
this.tempForeignKeys['organizations'] = uniq([...(this.tempForeignKeys['organizations'] || []), ...value]);
}

private disabled(opts, fieldName) {
// 1. test globale (editable)
if (!opts.editable) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/isari-editor/isari-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class IsariEditorComponent implements OnInit, OnDestroy {

layout = this.isariDataService.translate(layout, lang);
layout = this.isariDataService.closeAll(layout);
this.form = this.isariDataService.buildForm(layout, this.data);
this.form = this.isariDataService.buildForm(layout, this.data, true);
this.layout = this.isariDataService.rows(layout);

// disabled all form
Expand Down

0 comments on commit aef49ff

Please sign in to comment.