diff --git a/pages/jobs/[listing].tsx b/pages/jobs/[listing].tsx index 78e4ab5c4..a38d4518f 100644 --- a/pages/jobs/[listing].tsx +++ b/pages/jobs/[listing].tsx @@ -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) { diff --git a/pages/jobs/preview/[listing].tsx b/pages/jobs/preview/[listing].tsx new file mode 100644 index 000000000..d120b296f --- /dev/null +++ b/pages/jobs/preview/[listing].tsx @@ -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, + }; +}; diff --git a/src/jobs/utils/getListings.ts b/src/jobs/utils/getListings.ts index 96261c59e..b63453198 100644 --- a/src/jobs/utils/getListings.ts +++ b/src/jobs/utils/getListings.ts @@ -28,6 +28,7 @@ export const getListing = async ( ...metadata, name: fileName.replace('.md', ''), content: matterFile.content, + isPublished: metadata?.status === 'published', } as Listing; }; @@ -45,6 +46,7 @@ export type Listing = { id: number; name: string; content: string; + isPublished: boolean; } & ListingMetadata & Offer; export async function getFileListingData(