Hello, I'm building a course platform using Primsic with Slice Machine, Next JS and GraphQL. I have two questions, the first one regarding my approach on content modeling and the other on querying data from other document inside the home page.
I plan to display a two level Mega Menu where people will be able to filter the courses first by Scholarly (elementary school, secondary school, high school, uni) and then, on the second level by grade/semester. Finally this will result on a list of filtered courses.
My current approach for this (not sure if the best one) was to create a Course
custom type where I can add all the information for the course including a multi select for level and grade. Then I fetch all the documents and use JavaScript to do the filtering part. This way whenever someone adds a new course it will be added to the Course Mega Menu.
My first question is if it makes sense to use a multi select filed in the Course
custom type rather than making a relationship field. And in the case a relationship is the way to go, how would that look?
My second question is regarding the use of slices on the home page and how to query the data.
I was able to make the Mega Menu work using this code to get all the documents:
export async function getStaticProps({ params }) {
const client = Client()
const courses = await client.query(
Prismic.Predicates.at('document.type', 'curso')
)
return {
props: {
courses: courses.results,
},
}
}
However this was done in a separate Next Page called Courses
. I would like it to be a separate component to be able to drop in inside the pages/couses.js
and pages/index.js
This is how my index.js
currently looks.
export default function Home(props) {
return <SliceZone {...props} resolver={resolver} />
}
// Fetch content from prismic
export const getStaticProps = useGetStaticProps({
client: Client(),
type: 'home-page',
queryType: 'single',
})
For the Courses
Component I planned to pass all the courses
as props but can't figure out how to fetch all the courses documents as well as the slices for the homepage.
I tried doing the fetch like this
export async function getAllHome() {
const data = await fetchAPI(`
{
homePage(uid: "homepage", lang: "es-mx") {
slices {
__typename
}
}
allCursos {
edges {
node {
title
description
grade
level
}
}
}
}
`)
return {
cursos: { ...data?.allCursos?.edges },
home: { ...data?.homePage?.slices },
}
}
But I'm not sure if that is the right way to do it and couldn't figure out how to pass the home slices to the <SliceZone {...props} resolver={resolver} />
Component since the only thing that comes back for the homePage
is this:
"homePage": {
"slices": [
{
"__typename": "HomePageSlicesBanner_slice"
},
{
"__typename": "HomePageSlicesCourse_menu"
}
]
},
Another strange thing is that when fetching the courses I'm not able to get a field property called shortTitle
, this property is there when using Prismic.Predicates.at('document.type', 'curso')
to get the documents.
Bonus question:
Since my Couses
component is currently not a slice I'm not sure how to insert it between all the homePage
slices. My current approach was to create a simple Courses
slice with only a title
and the drop my custom courses
component inside of it, seems a bit hacky tho...
Sorry for the super long post that ended being 3 questions in one Thank you in advance!