-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from Vizzuality/cms/preview
Add story preview
- Loading branch information
Showing
25 changed files
with
1,662 additions
and
3,600 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// route handler with secret and slug | ||
import { draftMode } from 'next/headers'; | ||
import { redirect } from 'next/navigation'; | ||
|
||
import env from '@/env.mjs'; | ||
|
||
import { getStoriesId } from '@/types/generated/story'; | ||
|
||
export async function GET(request: Request) { | ||
// Parse query string parameters | ||
const { searchParams } = new URL(request.url); | ||
const secret = searchParams.get('secret'); | ||
const slug = searchParams.get('slug'); | ||
|
||
// Check the secret and next parameters | ||
// This secret should only be known to this route handler and the CMS | ||
if (secret !== env.NEXT_PUBLIC_PREVIEW_SECRET || !slug) { | ||
return new Response('Invalid token', { status: 401 }); | ||
} | ||
|
||
// Fetch the headless CMS to check if the provided `slug` exists | ||
// getPostBySlug would implement the required fetching logic to the headless CMS | ||
|
||
const story = await getStoriesId(+slug); | ||
|
||
// If the slug doesn't exist prevent draft mode from being enabled | ||
if (!story) { | ||
return new Response('Invalid slug', { status: 401 }); | ||
} | ||
|
||
// Enable Draft Mode by setting the cookie | ||
draftMode().enable(); | ||
|
||
// Redirect to the path from the fetched story | ||
// We don't redirect to searchParams.slug as that might lead to open redirect vulnerabilities | ||
redirect(`${env.NEXT_PUBLIC_URL}/stories/${slug}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { StepLayoutMapStepComponent, StoryStepsItem } from '@/types/generated/strapi.schemas'; | ||
import { StoryStepMap } from '@/types/story'; | ||
|
||
export const isMapStep = (step: StoryStepsItem): step is StepLayoutMapStepComponent => { | ||
return !!step?.__component?.includes('map-step'); | ||
}; | ||
|
||
export const isMapNotEmpty = (map: StoryStepsItem['map']): map is StoryStepMap => { | ||
return Object.values((map as StoryStepMap)?.location).length > 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.