@c2dh/react-miller
Advanced tools
Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
@@ -131,3 +131,71 @@ 'use strict'; | ||
} | ||
function mapStoryWithRelatedModulesDocuments(story) { | ||
var _story$contents; | ||
// No modules for this story! | ||
if (!Array.isArray((_story$contents = story.contents) === null || _story$contents === void 0 ? void 0 : _story$contents.modules)) { | ||
return story; | ||
} // Index docs by id | ||
var documentsById = story.documents.reduce(function (all, doc) { | ||
all[doc.document_id] = doc; | ||
return all; | ||
}, {}); // NOTE: | ||
// Ok at this points typings sucks ... a lot ... but for the module | ||
// schema isn't so clear ... so for now this makumba works keep it .... | ||
var mapId = function mapId(obj) { | ||
if (obj.id) { | ||
var _documentsById$obj$id; | ||
var document = (_documentsById$obj$id = documentsById[obj.id]) !== null && _documentsById$obj$id !== void 0 ? _documentsById$obj$id : null; | ||
return _objectSpread__default["default"](_objectSpread__default["default"]({}, obj), {}, { | ||
document: document | ||
}); | ||
} | ||
return obj; | ||
}; | ||
var modules = story.contents.modules.map(function (mod) { | ||
var _mappedModule$gallery; | ||
var mappedModule = mapId(mod); | ||
if (mappedModule.object) { | ||
mappedModule = _objectSpread__default["default"](_objectSpread__default["default"]({}, mappedModule), {}, { | ||
object: mapId(mappedModule.object) | ||
}); | ||
} | ||
if (Array.isArray(mappedModule.objects)) { | ||
mappedModule = _objectSpread__default["default"](_objectSpread__default["default"]({}, mappedModule), {}, { | ||
objects: mappedModule.objects.map(mapId) | ||
}); | ||
} | ||
if (Array.isArray(mappedModule.speakers)) { | ||
mappedModule = _objectSpread__default["default"](_objectSpread__default["default"]({}, mappedModule), {}, { | ||
speakers: mappedModule.speakers.map(mapId) | ||
}); | ||
} | ||
if (Array.isArray((_mappedModule$gallery = mappedModule.gallery) === null || _mappedModule$gallery === void 0 ? void 0 : _mappedModule$gallery.objects)) { | ||
mappedModule = _objectSpread__default["default"](_objectSpread__default["default"]({}, mappedModule), {}, { | ||
gallery: _objectSpread__default["default"](_objectSpread__default["default"]({}, mappedModule.gallery), {}, { | ||
objects: mappedModule.gallery.objects.map(mapId) | ||
}) | ||
}); | ||
} | ||
return mappedModule; | ||
}); | ||
return _objectSpread__default["default"](_objectSpread__default["default"]({}, story), {}, { | ||
contents: _objectSpread__default["default"](_objectSpread__default["default"]({}, story.contents), {}, { | ||
modules: modules | ||
}) | ||
}); | ||
} | ||
var _excluded = ["params"], | ||
@@ -143,3 +211,3 @@ _excluded2 = ["data"], | ||
_excluded10 = ["data"], | ||
_excluded11 = ["params"], | ||
_excluded11 = ["params", "mapModulesWithRelatedDocuments"], | ||
_excluded12 = ["data"]; | ||
@@ -314,2 +382,4 @@ function Miller(_ref) { | ||
givenParams = _ref12.params, | ||
_ref12$mapModulesWith = _ref12.mapModulesWithRelatedDocuments, | ||
mapModulesWithRelatedDocuments = _ref12$mapModulesWith === void 0 ? true : _ref12$mapModulesWith, | ||
queryOptions = _objectWithoutProperties__default["default"](_ref12, _excluded11); | ||
@@ -329,3 +399,14 @@ | ||
return [useTranslate(data), other]; | ||
var mappedData = react.useMemo(function () { | ||
if (!mapModulesWithRelatedDocuments) { | ||
return data; | ||
} | ||
if (!data) { | ||
return data; | ||
} | ||
return mapStoryWithRelatedModulesDocuments(data); | ||
}, [data, mapModulesWithRelatedDocuments]); | ||
return [useTranslate(mappedData), other]; | ||
} | ||
@@ -332,0 +413,0 @@ |
@@ -27,3 +27,5 @@ import { ReactNode } from 'react'; | ||
export declare function useStories(options?: UseQueryMillerOptions): [MillerPaginatedResponse<MillerStory[]> | undefined, UseQueryOtherResult]; | ||
export declare function useStory(idOrSlug: number | string, options?: UseQueryMillerOptions): [MillerStory | undefined, UseQueryOtherResult]; | ||
export declare function useStory(idOrSlug: number | string, options?: UseQueryMillerOptions & { | ||
mapModulesWithRelatedDocuments?: boolean; | ||
}): [MillerStory | undefined, UseQueryOtherResult]; | ||
export {}; |
@@ -120,3 +120,71 @@ import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2'; | ||
} | ||
function mapStoryWithRelatedModulesDocuments(story) { | ||
var _story$contents; | ||
// No modules for this story! | ||
if (!Array.isArray((_story$contents = story.contents) === null || _story$contents === void 0 ? void 0 : _story$contents.modules)) { | ||
return story; | ||
} // Index docs by id | ||
var documentsById = story.documents.reduce(function (all, doc) { | ||
all[doc.document_id] = doc; | ||
return all; | ||
}, {}); // NOTE: | ||
// Ok at this points typings sucks ... a lot ... but for the module | ||
// schema isn't so clear ... so for now this makumba works keep it .... | ||
var mapId = function mapId(obj) { | ||
if (obj.id) { | ||
var _documentsById$obj$id; | ||
var document = (_documentsById$obj$id = documentsById[obj.id]) !== null && _documentsById$obj$id !== void 0 ? _documentsById$obj$id : null; | ||
return _objectSpread(_objectSpread({}, obj), {}, { | ||
document: document | ||
}); | ||
} | ||
return obj; | ||
}; | ||
var modules = story.contents.modules.map(function (mod) { | ||
var _mappedModule$gallery; | ||
var mappedModule = mapId(mod); | ||
if (mappedModule.object) { | ||
mappedModule = _objectSpread(_objectSpread({}, mappedModule), {}, { | ||
object: mapId(mappedModule.object) | ||
}); | ||
} | ||
if (Array.isArray(mappedModule.objects)) { | ||
mappedModule = _objectSpread(_objectSpread({}, mappedModule), {}, { | ||
objects: mappedModule.objects.map(mapId) | ||
}); | ||
} | ||
if (Array.isArray(mappedModule.speakers)) { | ||
mappedModule = _objectSpread(_objectSpread({}, mappedModule), {}, { | ||
speakers: mappedModule.speakers.map(mapId) | ||
}); | ||
} | ||
if (Array.isArray((_mappedModule$gallery = mappedModule.gallery) === null || _mappedModule$gallery === void 0 ? void 0 : _mappedModule$gallery.objects)) { | ||
mappedModule = _objectSpread(_objectSpread({}, mappedModule), {}, { | ||
gallery: _objectSpread(_objectSpread({}, mappedModule.gallery), {}, { | ||
objects: mappedModule.gallery.objects.map(mapId) | ||
}) | ||
}); | ||
} | ||
return mappedModule; | ||
}); | ||
return _objectSpread(_objectSpread({}, story), {}, { | ||
contents: _objectSpread(_objectSpread({}, story.contents), {}, { | ||
modules: modules | ||
}) | ||
}); | ||
} | ||
var _excluded = ["params"], | ||
@@ -132,3 +200,3 @@ _excluded2 = ["data"], | ||
_excluded10 = ["data"], | ||
_excluded11 = ["params"], | ||
_excluded11 = ["params", "mapModulesWithRelatedDocuments"], | ||
_excluded12 = ["data"]; | ||
@@ -303,2 +371,4 @@ function Miller(_ref) { | ||
givenParams = _ref12.params, | ||
_ref12$mapModulesWith = _ref12.mapModulesWithRelatedDocuments, | ||
mapModulesWithRelatedDocuments = _ref12$mapModulesWith === void 0 ? true : _ref12$mapModulesWith, | ||
queryOptions = _objectWithoutProperties(_ref12, _excluded11); | ||
@@ -318,5 +388,16 @@ | ||
return [useTranslate(data), other]; | ||
var mappedData = useMemo(function () { | ||
if (!mapModulesWithRelatedDocuments) { | ||
return data; | ||
} | ||
if (!data) { | ||
return data; | ||
} | ||
return mapStoryWithRelatedModulesDocuments(data); | ||
}, [data, mapModulesWithRelatedDocuments]); | ||
return [useTranslate(mappedData), other]; | ||
} | ||
export { Miller, useDocument, useDocuments, useDocumentsFacets, useInfiniteDocuments, useStories, useStory }; |
@@ -73,2 +73,5 @@ export interface MillerConfig { | ||
} | ||
export interface MillerRelatedDocumenntInList { | ||
document_id: number; | ||
} | ||
export interface MillerDocumentDetail extends BaseMillerDocument { | ||
@@ -130,2 +133,9 @@ copyrights: string; | ||
} | ||
export interface MillerModule { | ||
module: string; | ||
[key: string]: any; | ||
} | ||
export interface MillerModuleContents { | ||
modules: MillerModule[]; | ||
} | ||
export interface MillerStory { | ||
@@ -140,2 +150,3 @@ id: number; | ||
covers: MillerCover[]; | ||
documents: MillerRelatedDocumenntInList[]; | ||
authors: any[]; | ||
@@ -145,2 +156,3 @@ tags: MillerTag[]; | ||
data: MillerStoryData; | ||
contents?: MillerModuleContents; | ||
} |
import { AxiosRequestConfig } from 'axios'; | ||
import { MillerStory } from './types'; | ||
export declare function useGetJSON(): <T = any, D = any>(path: string, req: AxiosRequestConfig<D>) => Promise<T>; | ||
export declare function useTranslator(): (data: any) => any; | ||
export declare function useTranslate(data: any): any; | ||
export declare function mapStoryWithRelatedModulesDocuments(story: MillerStory): MillerStory; |
@@ -5,3 +5,3 @@ { | ||
"license": "AGPL-3.0", | ||
"version": "1.0.0-beta.2", | ||
"version": "1.0.0-beta.3", | ||
"author": "c2dh (https://c2dh.uni.lu)", | ||
@@ -8,0 +8,0 @@ "main": "dist/index.cjs.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
68281
872
0