PostHandler not working

Hello

My Post handler is

const postHandler = {
name: "posts",
  priority: 10,
  pattern: "/posts/:slug",

  func: async ({ link, params, state, libraries }) => {

  	const {slug} = params;  	

    // 1. get product
    const response = await libraries.source.api.get({
    	endpoint: `posts/${slug}`
    	
    });

    // 2. add product to state
    const postData = await response.json();

    // 3. add link to data
	const post = state.source.data[link];
	Object.assign(post,{
		items : postData.items,
	});
  },
};
export default postHandler;

Getting This Post handler latestblog.js : i want to use post handler API and get post behalf of ID…

import React, { useEffect } from 'react';
import { connect, styled } from "frontity";

const GetLatestBlogs = ({ state, actions }) => {
         const itmes = state.source.get(`/posts/${state.theme.postID}/`);
	 console.log(itmes);
}

Index.js

import Root from "./components";
import image from "@frontity/html2react/processors/image";
import iframe from "@frontity/html2react/processors/iframe";
import link from "@frontity/html2react/processors/link";
import menuHandler from "./components/handlers/menu-handlers";
import postHandler from "./components/handlers/post-handlers";

const blogtimeTheme = {
  name: "blogtime-theme",
  roots: {
    theme: Root,
  },
  state: {
    theme: {
      autoPrefetch: "in-view",
      menu: [],
      menUrl: 'main-menu',
      postID: 1,
      isMobileMenuOpen: false,
      featured: {
        showOnList: false,
        showOnPost: false,
      },
    },
  },

  /**
   * Actions are functions that modify the state or deal with other parts of
   * Frontity like libraries.
   */
  actions: {
    theme: {
      toggleMobileMenu: ({ state }) => {
        state.theme.isMobileMenuOpen = !state.theme.isMobileMenuOpen;
      },
      closeMobileMenu: ({ state }) => {
        state.theme.isMobileMenuOpen = false;
      },
      beforeSSR: async({state, actions}) =>{
        await actions.source.fetch(`/menu/${state.theme.menUrl}/`);
        await actions.source.fetch(`/posts/${state.theme.postID}/`);

      },
    },
  },

  libraries: {
    html2react: {
      processors: [image, iframe, link],
    },

      source : {
        handlers : [menuHandler,postHandler],
        //handlers : [postHandler]
      }

  },
};
export default blogtimeTheme;

I am not getting data by post handler on my lastestblogs. js file

Help me please …

Hi @rajsha1024,

Welcome to the Frontity Community

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


handlers in Frontity allows you to create a custom route that can be fetched, so to “trigger” the handler you have to fetch that route by using actions.source.fetch

Inside the handler you have to take care of populating the data:

  • about the link itself → state.source.data[link]
  • with the details of the proper entity → state.source.post, state.source.page…

Here you have a video that explains how to create non WordPress routes in your Frontiy project

Hope this helps

Using custom Theme : blogtime-theme
GitHub : Bitbucket

Note : Wordpress is connected from my localhost.

I’ve created two handlers, First is menuHandler that is working fine…

Second is postHandler that is not working and show undefined error.
In postHandler i want to fetch post by Id or limit but it’s not working.

const itmes = state.source.get(`/posts/${state.theme.postID}/`);
console.log(itmes);

It’s showing undefined error…

But i am passing the url this way (state.source.get(’/’)) then it’s working and show all post.

I need post by Id or limit, In this case it’s not working…

Please help…

Thanks

Hi @rajsha1024 ,

What is the purpose of that handler? It seems you’re just getting the posts.
Frontity will get automatically the posts from the same relative path you’re getting those posts in your WordPress installation

For example:

The Frontity path is automatically available without further configurations or handlers

If these paths are not working because in your WordPress installation you have your post archive defined under the /posts/ subdirectory maybe just setting the property state.source.postsPage is enough to make it work

Hope this helps

Hello Juanma,

Thanks for reply,

const itmes = state.source.get(’/blog’); But still not getting any post data.
While i’m running my Wordpress with /blog url then it’s showing all posts.

I’m accessing directly the blog data by this url in frontity
const itmes = state.source.get(’/blog’);
for output please check below image…

My Wordpress screenshot:

And please suggest how to fetch limited post or latest 5 post from wordpress ? So basically i was using handler.

Thanks

Hi @rajsha1024 ,

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.

The WordPress installation you’re using for this project also need to be available online because specific settings in WordPress may have an impact on how the final Frontity project behaves


Did you try this?

You can try to set state.source.param property to specify the per_page param to limit the number of posts

If this doesn’t work, then performing a custom request via libraries.source.api.get() specifying custom params, seems to be the way to go

Hello Juanma,

Please check and give me your valuable feedback…

For git code check below link…
https://bitbucket.org/rajsha1024/blogtime/

My Wordpress is okay… Please suggest which setting you need for this?

Wordpress admin
http://test.safa.ae/wp-admin/
user : admin
pass : 12345

Now please check what is the issue? Still i’m not getting blog data with blog url…
const itmes = state.source.get(’/blog’);

Hello Juanma,

Have you checked my issue?

I’m waiting your reply.

Thanks

Hi @rajsha1024,

By looking at your project I’ve detected a few issues:

<Link> component uses link property

You’re using href prop for this component so links are not working and errors are displayed

Uncontrolled display of React components based on route content

Your packages/blogtime-theme/src/components/index.js is not determining the type of React component to show based on the type of content for the current route
Have a look at the index.js of mars-theme to see how this theme displays a different type of component depending on the type of data in the route by using the <Switch> component

<Switch>
  <Loading when={data.isFetching} />
  <List when={data.isArchive} />
  <Post when={data.isPostType} />
  <PageError when={data.isError} />
</Switch>

Because of not managing this you’re getting the same content no matter the URL you load


I recommend you to do the project explained at https://tutorial.frontity.org/ to get a better understanding of how a Frontity project works