Skip to content

Commit f2bc443

Browse files
ninacebanNina Ciocanu
andauthored
Return response tokens into "rendering-succeeded" hook (#1186)
* add response tokens to rendering-succeeded --------- Co-authored-by: Nina Ciocanu <[email protected]>
1 parent e4fb8d3 commit f2bc443

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

src/components/Personalization/createFetchDataHandler.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
99
OF ANY KIND, either express or implied. See the License for the specific language
1010
governing permissions and limitations under the License.
1111
*/
12-
import {groupBy, isNonEmptyArray} from "../../utils/index.js";
12+
import { groupBy, isNonEmptyArray } from "../../utils/index.js";
1313
import PAGE_WIDE_SCOPE from "../../constants/pageWideScope.js";
1414

1515
const DECISIONS_HANDLE = "personalization:decisions";
@@ -46,14 +46,14 @@ export default ({
4646

4747
onResponse(({ response }) => {
4848
const handles = response.getPayloadsByType(DECISIONS_HANDLE);
49-
if(!isNonEmptyArray(handles)){
49+
if (!isNonEmptyArray(handles)) {
5050
logger.logOnContentRendering({
5151
status: "no-offers",
5252
message: "No offers were returned.",
5353
logLevel: "info",
5454
detail: {
55-
query: personalizationDetails.createQueryDetails()
56-
}
55+
query: personalizationDetails.createQueryDetails(),
56+
},
5757
});
5858
}
5959
const propositions = handles.map((handle) => createProposition(handle));
@@ -82,7 +82,9 @@ export default ({
8282
logLevel: "info",
8383
detail: {
8484
scope: PAGE_WIDE_SCOPE,
85-
propositions: pagePropositions.map(proposition => proposition.toJSON())
85+
propositions: pagePropositions.map((proposition) =>
86+
proposition.toJSON(),
87+
),
8688
},
8789
});
8890
}
@@ -94,7 +96,9 @@ export default ({
9496
logLevel: "info",
9597
detail: {
9698
scope: personalizationDetails.getViewName(),
97-
propositions: currentViewPropositions.map(proposition => proposition.toJSON())
99+
propositions: currentViewPropositions.map((proposition) =>
100+
proposition.toJSON(),
101+
),
98102
},
99103
});
100104
}

src/components/Personalization/createViewChangeHandler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export default ({ processPropositions, viewCache, logger }) => {
3535
logLevel: "info",
3636
detail: {
3737
scope: viewName,
38-
propositions: propositions.map(proposition => proposition.toJSON())
38+
propositions: propositions.map((proposition) =>
39+
proposition.toJSON(),
40+
),
3941
},
4042
});
4143

src/components/Personalization/flicker/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ export const showElements = (prehidingSelector) => {
5656
}
5757
};
5858

59-
export const createHideContainers = ( logger ) => {
60-
return ( prehidingStyle ) => {
59+
export const createHideContainers = (logger) => {
60+
return (prehidingStyle) => {
6161
if (!prehidingStyle) {
6262
return;
6363
}
@@ -84,9 +84,9 @@ export const createHideContainers = ( logger ) => {
8484

8585
appendNode(document.head, styleNode);
8686
};
87-
}
87+
};
8888

89-
export const createShowContainers = ( logger ) => {
89+
export const createShowContainers = (logger) => {
9090
return () => {
9191
// If containers prehiding style exists
9292
// we will remove it
@@ -102,5 +102,5 @@ export const createShowContainers = ( logger ) => {
102102
logLevel: "info",
103103
});
104104
removeNode(node);
105-
}
105+
};
106106
};

src/components/Personalization/handlers/createProcessPropositions.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010
governing permissions and limitations under the License.
1111
*/
1212

13-
import { groupBy } from "../../../utils/index.js";
13+
import { groupBy, isNonEmptyArray } from "../../../utils/index.js";
1414

1515
export default ({ schemaProcessors, logger }) => {
1616
const wrapRenderWithLogging = (render, item) => () => {
@@ -20,7 +20,7 @@ export default ({ schemaProcessors, logger }) => {
2020
if (logger.enabled) {
2121
logger.info(`Action ${item.toString()} executed.`);
2222
}
23-
return true;
23+
return item.toJSON();
2424
})
2525
.catch((error) => {
2626
const { message, stack } = error;
@@ -36,18 +36,19 @@ export default ({ schemaProcessors, logger }) => {
3636
logLevel: "warn",
3737
});
3838

39-
return false;
39+
return undefined;
4040
});
4141
};
4242

4343
const renderItems = (renderers, meta) =>
44-
Promise.all(renderers.map((renderer) => renderer())).then((successes) => {
44+
Promise.all(renderers.map((renderer) => renderer())).then((results) => {
45+
const successes = results.filter((result) => result);
4546
// as long as at least one renderer succeeds, we want to add the notification
4647
// to the display notifications
47-
if (!successes.includes(true)) {
48-
return undefined;
48+
if (meta && isNonEmptyArray(successes)) {
49+
return { ...meta, items: successes };
4950
}
50-
return meta;
51+
return undefined;
5152
});
5253

5354
const processItem = (item) => {
@@ -197,15 +198,20 @@ export default ({ schemaProcessors, logger }) => {
197198
const render = () => {
198199
return Promise.all(renderers.map((renderer) => renderer())).then(
199200
(metas) => {
200-
const renderedPropositions = metas.filter((meta) => meta);
201-
const propsByScope = groupBy(renderedPropositions, (p) => p.scope);
202-
logger.logOnContentRendering({
203-
status: "rendering-succeeded",
204-
detail: { ...propsByScope },
205-
message: `Scopes: ${JSON.stringify(propsByScope)} successfully executed.`,
206-
logLevel: "info",
201+
const propositions = metas.filter((meta) => meta);
202+
const renderedPropositions = propositions.map((prop) => {
203+
const { id, scope, scopeDetails } = prop;
204+
return { id, scope, scopeDetails };
207205
});
208-
206+
if (isNonEmptyArray(propositions)) {
207+
const propsByScope = groupBy(propositions, (p) => p.scope);
208+
logger.logOnContentRendering({
209+
status: "rendering-succeeded",
210+
detail: { ...propsByScope },
211+
message: `Scopes: ${JSON.stringify(propsByScope)} successfully executed.`,
212+
logLevel: "info",
213+
});
214+
}
209215
return renderedPropositions;
210216
},
211217
);

src/components/Personalization/handlers/processDefaultContent.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
99
OF ANY KIND, either express or implied. See the License for the specific language
1010
governing permissions and limitations under the License.
1111
*/
12+
import noop from "../../../utils/noop.js";
13+
1214
export default () => {
13-
return { setRenderAttempted: true, includeInNotification: true };
15+
return {
16+
render: noop,
17+
setRenderAttempted: true,
18+
includeInNotification: true,
19+
};
1420
};

test/unit/specs/components/Personalization/handlers/processDefaultContent.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe("processDefaultContent", () => {
1515
it("always renders the default content", () => {
1616
const result = processDefaultContent();
1717
expect(result).toEqual({
18+
render: jasmine.any(Function),
1819
setRenderAttempted: true,
1920
includeInNotification: true,
2021
});

0 commit comments

Comments
 (0)