Skip to content

Commit

Permalink
fix: return 404 for unpublished jobs and adds preview url
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Mar 20, 2024
1 parent a6f260d commit b9e337c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pages/jobs/[listing].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export const getStaticProps: GetStaticProps<
// This will never be empty as that path is caught by 'index.tsx' file
const fileName = `${context?.params?.listing}.md`;
const listing = await getListing(fileName);

if (!listing.isPublished) {
return {
notFound: true,
};
}


const contactEmails = listing.contact_emails?.split(',').map((e) => e.trim());
let contacts: EmployeeItem[] = [];
if (contactEmails?.length) {
Expand Down
40 changes: 40 additions & 0 deletions pages/jobs/preview/[listing].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getListings, getListing, Listing } from 'src/jobs/utils/getListings';
import { GetStaticPaths, GetStaticProps } from 'next';
import { EmployeeItem } from 'src/employees/types';
import { getContactsByEmails } from 'src/employees/utils/getEmployeesList';

export { default } from 'src/jobs/listing/listing';

export const getStaticPaths: GetStaticPaths = async () => {
try {
const paths = await getListings();
return {
paths: paths.map((listing) => ({
params: { listing: listing.replace('.md', '') },
})),
fallback: false,
};
} catch (error) {
console.error(error);
return { paths: [], fallback: true };
}
};

export const getStaticProps: GetStaticProps<
{ listing: Listing & { contacts: EmployeeItem[] } },
{ listing: string }
> = async (context) => {
// This will never be empty as that path is caught by 'index.tsx' file
const fileName = `${context?.params?.listing}.md`;
const listing = await getListing(fileName);

const contactEmails = listing.contact_emails?.split(',').map((e) => e.trim());
let contacts: EmployeeItem[] = [];
if (contactEmails?.length) {
contacts = await getContactsByEmails(contactEmails);
}
return {
props: { listing: { ...listing, contacts } },
revalidate: 60 * 60,
};
};
2 changes: 2 additions & 0 deletions src/jobs/utils/getListings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const getListing = async (
...metadata,
name: fileName.replace('.md', ''),
content: matterFile.content,
isPublished: metadata?.status === 'published',
} as Listing;
};

Expand All @@ -45,6 +46,7 @@ export type Listing = {
id: number;
name: string;
content: string;
isPublished: boolean;
} & ListingMetadata &
Offer;
export async function getFileListingData(
Expand Down

0 comments on commit b9e337c

Please sign in to comment.