Commit a0ec8904 authored by Matija Obreza's avatar Matija Obreza
Browse files

dereferenceReferences with _self option

parent a97955cf
......@@ -3,10 +3,5 @@
"welcome": {
"title": "Grin global application"
}
},
"validate": {
"error": {
"required": "Required"
}
}
}
}
\ No newline at end of file
......@@ -65,15 +65,47 @@ export function isNumeric(value: any): boolean {
* This function remaps reference-by-ID to full objects
*
* Examples:
* dereferenceReferences2(paged.content, { uuid: [ 'owner', 'foobar' ], id: [ 'institute' ]});
* dereferenceReferences2(paged.content, { uuid: [ 'owner', 'foobar' ], id: [ 'institute' ]});
*
* `_self` allows you to handle top-level references:
* dereferenceReferences2(paged.content, { _self: 'user', uuid: [ 'user', 'owner', 'foobar' ], id: [ 'institute' ]});
*
* @param content The list of objects
* @param referenceMap The map of ID-keys of properties
*/
export function dereferenceReferences2(content: any[], referenceMap: object, refs: { [key: string]: any } = {}) {
export function dereferenceReferences(content: any[], referenceMap: any, refs: { [key: string]: any } = {}): any[] {
// Convert cross-references to Partner object
if (!content) {
return;
return content;
}
const { _self, ...refMap } = referenceMap;
let _selfName = null;
if (_self) {
_selfName = Object.keys(refMap).filter((id) => {
// check if mapping for this id contains _self name
return refMap[id].filter((elem) => elem === _self).length === 1;
});
if (_selfName.length === 1) {
_selfName = _selfName[0];
console.log(`_Self ${_self} is by ${_selfName}`);
if (! refs[`${_self}`]) {
refs[`${_self}`] = {}; // initialize map
}
}
} else {
// console.log('No _self found in', referenceMap);
}
if (_selfName) {
// scan for selfs first
content.forEach((entry) => {
if (entry && typeof entry === 'object') {
// add self if declared
// console.log(`Registered _self reference ${_self}.'${entry[_selfName]}' using ${_selfName}`);
refs[`${_self}`][entry[`${_selfName}`]] = entry;
}
});
}
// deep search first
......@@ -81,40 +113,50 @@ export function dereferenceReferences2(content: any[], referenceMap: object, ref
if (entry && typeof entry === 'object') {
for (const p of Object.keys(entry)) {
if (entry[p] && typeof entry[p] === 'object') {
dereferenceReferences2([entry[p]], referenceMap, refs);
dereferenceReferences([entry[p]], refMap, refs);
}
}
}
});
// console.log('Handling references', content);
Object.keys(referenceMap).forEach((id) => {
const props: string[] = referenceMap[id];
Object.keys(refMap).forEach((id) => {
const props: string[] = refMap[id];
// iterate over unique prop names
props.filter((elem, pos, arr) => arr.indexOf(elem) === pos).forEach((props) => {
props.filter((elem, pos, arr) => arr.indexOf(elem) === pos).forEach((prop) => {
const prop0 = Array.isArray(props) ? props[0] : props; // use 1st element as master prop key
if (!refs[prop0]) {
refs[prop0] = {}
} // setup
(Array.isArray(props) ? props : [props]).forEach((prop) => {
// console.log(`Dereferencing ${prop}.${id} using ${prop0}`);
content.filter((entry) => entry && entry[prop] && typeof entry[prop] === 'object' && entry[prop][id])
.map((entry) => entry[prop])
.forEach((ref) => {
refs[prop0][`${ref[id]}`] = ref;
// console.log(`Registered ${prop0}.${id}.'${ref[id]}' in`, refs[prop0]);
});
content.filter((entry) => entry && entry[prop] && typeof entry[prop] !== 'object')
.forEach((entry) => {
// console.log(`Looking up ${prop0}.'${entry[prop]}' in`, refs[prop0]);
entry[prop] = refs[prop0][`${entry[prop]}`] || entry[prop]; // keep original if not found
});
});
// console.log(`Dereferencing ${prop}.${id} as ${prop0}`);
content.filter((entry) => entry && entry[prop] && typeof entry[prop] === 'object' && entry[prop][id])
.map((entry) => entry[prop])
.forEach((ref) => {
refs[prop0][`${ref[id]}`] = ref;
// console.log(`Registered ${prop0}.'${ref[id]}' using ${id} in`, refs[prop0]);
});
content.filter((entry) => entry && entry[prop] && typeof entry[prop] !== 'object')
.forEach((entry) => {
// console.log(`Looking up ${prop0}.'${entry[prop]}' in`, refs[prop0]);
entry[prop] = refs[prop0][`${entry[prop]}`] || entry[prop]; // keep original if not found
});
});
});
if (_selfName) {
content.forEach((entry, index) => {
if (entry && typeof entry !== 'object') {
// add self if declared
// console.log(`Looking up _self reference ${_self}.'${entry}'`);
content[index] = refs[`${_self}`][`${entry}`];
}
});
}
return content;
}
export function insertAtCaret(element, myValue) {
......
......@@ -11,6 +11,7 @@ import Cooperator from '@gringlobal/client/model/gringlobal/Cooperator';
import { IPageRequest, FilteredPage } from '@gringlobal/client/model/page';
// Service
import CooperatorService from '@gringlobal/client/service/CooperatorService';
import { dereferenceReferences } from '@gringlobal/client/utilities';
export const cooperatorPublicSagas = [
......@@ -56,7 +57,11 @@ function * listCooperatorsSaga(action) {
type: API_RECEIVE_COOPERATORS,
method: CooperatorService.filter_1,
params: [action.payload.filter, action.payload.pageR],
onSuccess: (cooperators) => receiveCooperatorsSuccess(cooperators),
onSuccess: (cooperators) => {
dereferenceReferences(cooperators.content, { _self: 'cooperator', id: [ 'cooperator', 'ownedBy' ] });
// console.log(cooperators.content);
return receiveCooperatorsSuccess(cooperators);
},
});
}
......
......@@ -7,13 +7,14 @@ const publicRoutes: IRoute[] = [
{
exact: true,
component: Loadable({
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "user" */ 'cooperator/ui/BrowsePage'),
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "cooperator" */ 'cooperator/ui/BrowsePage'),
}),
path: '/cooperator',
},
{
exact: true,
component: Loadable({
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "user" */'cooperator/ui/DetailsPage'),
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "cooperator" */'cooperator/ui/DetailsPage'),
}),
path: '/cooperator/:id',
},
......
......@@ -39,7 +39,7 @@ class CooperatorBrowsePage extends React.Component<IBrowsePageProps> {
<ul>
{ cooperators && cooperators.content && cooperators.content.length > 0 && cooperators.content.map((cooperator) => (
<li key={ cooperator.id }>
<Link to={ `/cooperator/${ cooperator.id }` }>{ `${ cooperator.email } - ${ cooperator.title } - ${ cooperator.job }` }</Link>
<Link to={ `/cooperator/${ cooperator.id }` }>{ cooperator.id } { cooperator.firstName } {cooperator.lastName} {cooperator.job}</Link>
</li>
)) }
</ul>
......
......@@ -36,7 +36,7 @@ class CooperatorDetailsPage extends React.Component<ICooperatorDetailsPage> {
</pre>
<BackButton/>
</div>
) : null;
) : <div>No such thing</div>;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment