#Best way to accomplish this task

7 messages · Page 1 of 1 (latest)

karmic dove
#

I am going to try my best to explain this. In my CMS, I have a 3 fields. Active (Boolean), body, and date posted. When I get the data. If "Active" is true. I want to display a banner under my header and it links to a Notices page. If active is false. Dont display the banner.

What is the best way to accomplish this without having to call my CMS twice?

neon loom
#

Only to better understand your question, what is the structure you are fetching on Astro?

karmic dove
#

I am using Contentful. This is my Interface.

export interface ImportantNoticesItem {
    contentTypeId: 'importantNotice';
    fields: {
        datePosted: EntryFieldTypes.Date;
        body: EntryFieldTypes.Text;
        active: EntryFieldTypes.Boolean;
    };
}

Then when i am fetching my data on the Notices page. I have this

const entries = await contentfulClient.getEntries({
    content_type: 'importantNotice',
});

const allNotices = entries.items.map((item) => {
    return {
        date: item.fields.datePosted,
        body: item.fields.body,
        active: item.fields.active,
    };
});

const isActive = allNotices.length > 0;
neon loom
#

Okay, got it. What I'm not seeing is needing to call your client twice if you can check isActive with the entries directly. Can you post a snippet of how you are handling this now?

karmic dove
#

Discord is not letting me post it. But I want my index page to show the banner if its active. While my Notices page is the one that is actually fetching the data.

#

Since I am fetching the data on my index page to show the banner. But I am needing to display that data on my Notices page as well.