How to extend default data with handler?

it appears if I create a handler it overrides the default state data completely?

how do we just extend the current state?

eg normal archive behaviour without handler:

isArchive: true
isEventArchive: true
isPostTypeArchive: true
isFetching; false
isReady: true,
items : {}
link: "....",
next: "...",
page: 1,
etc
etc

with handler: (a dummy one that does nothing specific to the state)

isFetching: false
isReady: true

all the default state regarding isArchive etc has disappeared. Ideally I wanted to keep the old data and just add some extra of my own (specifically injecting some extra content from a normal page into the archive page)

if I try adding those is flags items back in manually, along with other relevant properties that were in the original (dummy values just for testing), it also breaks connection to the yoast title as well, so it seems the handler is overriding the back-end process somehow rather than letting me extend it.

thanks
J

Hi @codemonkeynorth

Can you please provide a repo or code-sandbox with your code? This is especially helpful to find solutions to technical issues with specific code

Detailing the info suggested here when having issues will help the community to provide the best possible help as quickly and as efficiently as possible.


In the meantime, have a look at this portion of this video where you have an explanation of creating a handler

Here you have the documentation for libraries.source.handlers. And take into account the most used methods used inside handlers function: libraries.source.api.get() and libraries.source.populate()

1 Like

Thanks I will try put something together

As a side-note, it would be great if test.frontity.org included a custom post type archive for demonstrating issues against, but I will set up a dummy WP installation

In the meantime what I am saying is if I have a custom post type archive in Wordpress (say “events”), by default frontity will create some relevant flags “isEventArchive”, “isPostTypeArchive” etc when I visit “/events”

If I then add a custom handler on the “/events” route all those parameters get wiped, I can’t just extend the archive with extra values

@juanma codesandbox: Frontity Test https://5hok7.sse.codesandbox.io/events/ (handler present)

if look at the console.log, in the [[target]] you will see:

isFetching: false
isReady: true
link: "/events/"
page: 1
query: {}
route: "/events/"

(use this example and un/comment the handler in the theme index.js distracted-hoover-5hok7 - CodeSandbox )

by adding the handler (even though the handler is not modifying the state) i’ve lost my archive flags etc

However, by default without the custom eventsHandler you will see the events archive appear properly on the website, because isEventArchive etc will now be present in the state again (as seen on Events Archive (from Yoast) - Frontity Test)

Essentially it would be good if a handler could return default state including all the parameters such as isArchive, isEventArchive etc and I can either add to the state or replace with my own state.

// adding a handler clears the default state for this route
// all items such as isArchive, isEventArchive etc have disappeared
// handler is just used for logging purposes here
// ideally state would contain the default state and can be overridden if necessary
const eventsFunc = async ({ route, params, state, libraries }) => {
  console.log("eventsFunc:")
  console.log("state =>", state.source.data[route])
}
const eventsHandler = {
  priority: 10,
  pattern: "/events",
  func: eventsFunc
};

export default eventsHandler

rest API is at https://frontity-test-wp.codemonkeynorth.co.uk/wp-json

https://test.frontity.org/wp-json/wp/v2/destinations
https://test.frontity.org/wp-json/wp/v2/movies

2 Likes

@codemonkeynorth did you manage to solve this? I’m also interested in a solution. I thought to “handle” everything with switch and looking the urls but will be much elegant if we can do it with a “handler” without overwrite the data