Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Implementing a Review Section: Valid Approach?

discord user avatar
seanzubrickas
last year
2

Hey

@517330076677832705

moved your question over here! You can use this channel for all future questions like this. Thanks!

  • default discord avatar
    kalon_robsonlast year

    Hi

    @517330076677832705

    , Your above plan looks like it would work.



    As for the block your two options could be:


    1. Create a block with a field relationship of hasMany that allows the admin user to select the reviews they would like to show. Then all you need to do is use eitgher:


    a: Graphql to select the name, text and stars from the relationship


    b: increase the depth on a REST request to get these fields (could effect performance as it would apply to all to all relationships)


    c: Or you could get the Id's from a depth of zero and make another call in your client side to fetch the records manually.



    2. If you would rather show reviews dynamically rather than an admin always having to select them you could just create a block named 'reviews' and when that block is rendered use the third option above to fetch the most recent records.



    If you produce a minimal version I'd be happy to take a look with you.



    More information about


    - depth:

    https://payloadcms.com/docs/getting-started/concepts#depth
  • default discord avatar
    scamdrew_tatelast year

    Hey

    @618601682460344334

    , thank you for your reply 🙂



    block > Reviews.ts

    import type { Block } from 'payload' export const ReviewsBlock: Block = { slug: 'reviewsBlock', fields: [ { type: 'relationship', name: 'reviewsToShow', label: 'Reviews to show', relationTo: 'reviews', hasMany: true, }, ], }

    collections > Reviews > index.ts

    import type { CollectionConfig } from 'payload' import { anyone } from '../../access/anyone' import { authenticated } from '../../access/authenticated' export const Reviews: CollectionConfig = { slug: 'reviews', access: { create: authenticated, delete: authenticated, read: anyone, update: authenticated, }, admin: { useAsTitle: 'personName', group: 'Admin', }, fields: [ { name: 'personName', type: 'text', required: true, }, { name: 'text', type: 'text', required: true, }, { name: 'stars', type: 'select', defaultValue: '5', options: [ { label: '⭐⭐⭐⭐⭐', value: '5', }, { label: '⭐⭐⭐⭐', value: '4', }, { label: '⭐⭐⭐', value: '3', }, { label: '⭐⭐', value: '2', }, { label: '⭐', value: '1', }, ], }, ], }

    This implementation has proven to be exactly what I need. I didnt have to modify the value of depth or use other more complicated workflows. Somehow it just works like that 🙂



    With the implementation I shared, reviews can be created in the Reviews collection and admin can select which reviews to show in the page builder reviews block. Of course this could be improved with displaying the 3 latest automatically etc. but for the small project that I am working on currently it fills all the needs.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.