From 721c2a15e6e7a817039243071cce04d91f41b1b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Chrastina?= Date: Tue, 14 Dec 2021 13:40:28 +0100 Subject: [PATCH 1/3] add persona filter --- src/ListingPage.js | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/ListingPage.js b/src/ListingPage.js index 33ff19c..9eb1981 100644 --- a/src/ListingPage.js +++ b/src/ListingPage.js @@ -14,7 +14,7 @@ import React, { useState } from "react"; import { gql, useQuery } from "@apollo/client"; import { assetFields, seoFields } from "./graphQLFragments"; import getSeo from "./utils/getSeo"; -import { getAuthor, setAuthor } from "./utils/queryString"; +import { getAuthor, setAuthor, getPersona, setPersona } from "./utils/queryString"; const useStyles = makeStyles((theme) => ({ root: { @@ -32,12 +32,26 @@ const useStyles = makeStyles((theme) => ({ }, })); +function getListingPageQuerySignatureSuffix(author, persona) { + let result = author ? ", $author: String!" : ""; + result += persona ? ", $persona: String!" : ""; + return result; +} + +function getListingPageQueryCondition(author, persona) { + + const personaQuery = "persona: {containsAny: [$persona]}"; + const authorQuery = "author: {containsAny: [$author]}"; + + let result = author && persona ? `, where: { AND: [{${authorQuery}}, {${personaQuery}} ] }` : ""; + result += !author && persona ? `, where: {${personaQuery}}` : ""; + result += author && !persona ? `, where: {${authorQuery}}` : ""; + return result; +} + function ListingPage(props) { const listingPageQuery = gql` - # TODO add persona once https://kentico.atlassian.net/browse/DEL-3086 is done - query ListingPageQuery($limit: Int, $offset: Int, $codename: String! ${ - props.author ? ", $author: String!" : "" - }){ + query ListingPageQuery($limit: Int, $offset: Int, $codename: String! ${getListingPageQuerySignatureSuffix(props.author, props.persona)}){ author_All { items { firstName @@ -47,10 +61,7 @@ function ListingPage(props) { } } } - # TODO add persona once https://kentico.atlassian.net/browse/DEL-3086 is done - post_All(limit: $limit, offset: $offset, ${ - props.author ? "where: {author: {containsAny: [$author]} }" : "" - }) { + post_All(limit: $limit, offset: $offset ${getListingPageQueryCondition(props.author, props.persona)}) { items { _system_ { type { @@ -95,7 +106,7 @@ function ListingPage(props) { const [relatedItems, setRelatedItems] = useState([]); const [authors, setAuthors] = useState([]); - // const [personas, setPersonas] = useState([]); + const [personas, setPersonas] = useState([]); const [seo, setSeo] = useState(null); const { loading, error, data } = useQuery( @@ -104,7 +115,8 @@ function ListingPage(props) { variables: { codename: props.codename, author: props.author, - /* persona: props.persona,*/ limit: props.limit, + persona: props.persona, + limit: props.limit, offset: props.offset, }, onCompleted: (data) => { @@ -126,10 +138,10 @@ function ListingPage(props) { ); // TODO update hardcoded personas and load them from Kontent - // setPersonas([{ - // name: "Developer", - // codename: "developer" - // }]); + setPersonas([{ + name: "Developer", + codename: "developer" + }]); setSeo(getSeo(data.navigationItem._seo)); }, @@ -164,7 +176,7 @@ function ListingPage(props) { updateLocation={setAuthor} getValueFromLocation={getAuthor} /> - {/* */} + {relatedItems.length > 0 && ( {relatedItems.map((item, item_idx) => { From aa2679576c86614ba52067e465390373a49e41a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Chrastina?= Date: Tue, 14 Dec 2021 17:43:14 +0100 Subject: [PATCH 2/3] update readme --- README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 90d1af0..ac9539e 100644 --- a/README.md +++ b/README.md @@ -531,7 +531,7 @@ You can extend the posts query setting `where` parameter in query. ```graphql query PostsQuery($author: String) { - post_All(where: {authorLinksCodename: $author}) { + post_All(where: {author: {containsAny: [$author]}}) { # Strongly typed collections of items based on `Post`content type items { # ... @@ -544,7 +544,25 @@ query PostsQuery($author: String) { ```graphql query PostsQuery($persona: String) { - post_All(where: {personaLinksTerm: $persona}) { + post_All(where: {persona: {containsAny: [$persona]}}) { + # Strongly typed collections of items based on `Post`content type + items { + # ... + } + } +} +``` + +#### Filter blog by multiple conditions (persona and author) + +```graphql +query PostsQuery($persona: String) { + post_All(where: { + AND: [ # It is also possible to use OR and create more complex filter queries + { persona: {containsAny: [$persona]} }, + { author: {containsAny: [$author]} } + ] + }) { # Strongly typed collections of items based on `Post`content type items { # ... From 608161c469cd03132c944a4a66b69daeeb43bc8a Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Fri, 7 Jan 2022 12:39:42 +0100 Subject: [PATCH 3/3] remove ToDo task --- src/ListingPage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ListingPage.js b/src/ListingPage.js index 9eb1981..de7a98f 100644 --- a/src/ListingPage.js +++ b/src/ListingPage.js @@ -137,7 +137,6 @@ function ListingPage(props) { }) ); - // TODO update hardcoded personas and load them from Kontent setPersonas([{ name: "Developer", codename: "developer"