Hey @nkuDev!
I reviewed your code, and this is what you need to change:
First, letās go to src/components/handlers.js
. There you are using libraries
in a place where that variable doesnāt exist. Also, you donāt need to export an array with your handler inside (CustomPostHandler
), you can export the handlers directly. I would do something like this:
// Do not use `libraries.source.handlers.push()` here.
export const postHandler = {
pattern: "/posts/",
func: async ({ route, params, state, libraries }) => {
const response = await libraries.source.api.get({
endpoint: "/pre/v1/all_posts"
});
// You can name this as `items`... .
const items = await libraries.source.populate({ response, state });
Object.assign(state.source.data[route], {
isPostType: true,
// ...and use it directly here without using `map()`.
items
});
}
};
Second, you need to import the handler in your src/index.js
file an add it in libraries.source.handlers
. To do that, itās better to add it directly to the array ā instead of using push
in the init
action ā because Frontity appends the same array from different packages (we need to clarify this in the wp-source
docs ).
import Theme from "./components";
import image from "@frontity/html2react/processors/image";
// Import your custom handler.
import { postHandler } from "./components/handlers";
const marsTheme = {
name: "@frontity/mars-theme",
roots: { ... },
state: { ... },
actions: {
theme: {
// You can remove the `init` function here.
toggleMobileMenu: ({ state }) => { ... },
closeMobileMenu: ({ state }) => { ... }
}
},
libraries: {
html2react: { ... },
// Add your custom handler using an array.
source: {
handlers: [postHandler]
}
}
};
export default marsTheme;
And last, you can just remove the line that imports CustomPostHandler
in your src/components/CustomPost.js
file, itās not being used anywhere.
Thatās all, it should work after these changes.
Appart from that, I would recommend you to move handlers.js
to a different folder (they are not components after all) and, also, I would add build
to the .gitignore
file (that folder is generated when running npx frontity dev
or npx frontity prod
, thereās no need to version them).
Let us know if it works or if you need more help.
Cheers!