diff --git a/src/lib/datocms-bypass.ts b/src/lib/datocms-bypass.ts index e234984..7d762e3 100644 --- a/src/lib/datocms-bypass.ts +++ b/src/lib/datocms-bypass.ts @@ -10,811 +10,803 @@ import { promises as fs } from 'fs'; //-----misc helpers function datocmsDateToIng(date : string) : number { - - let dateInt = 0; - - date = date.replace(/\D/g,''); - dateInt = parseInt(date); - - if (Number.isInteger(dateInt) !== true) { - dateInt = 0; - } - - return dateInt; + + let dateInt = 0; + + date = date.replace(/\D/g,''); + dateInt = parseInt(date); + + if (Number.isInteger(dateInt) !== true) { + dateInt = 0; + } + + return dateInt; } //-----graphql query interception export async function datocmsQueryIntercept(query : any) { - let parent = pluckFirstParentFromQuery(query); - let pageQueryWhitelist = getPageQueryBypassWhitelist(); - let listingQueryWhitelist = getListingQueryDirectories(); - let interceptData = {}; + let parent = pluckFirstParentFromQuery(query); + let pageQueryWhitelist = getPageQueryBypassWhitelist(); + let listingQueryWhitelist = getListingQueryDirectories(); + let interceptData = {}; + + if (listingQueryWhitelist.includes(parent)) { + interceptData = await datocmsListingQueryIntercept(query); + } else if (pageQueryWhitelist.includes(parent)) { + interceptData = await datocmsPageQueryIntercept(query); + } else { + let errorText = 'Unable to intercept datocms query. No viable JSON alternative was defined.'; + interceptData = {error: errorText}; + } - if (listingQueryWhitelist.includes(parent)) { - interceptData = await datocmsListingQueryIntercept(query); - } else if (pageQueryWhitelist.includes(parent)) { - interceptData = await datocmsPageQueryIntercept(query); - } else { - let errorText = 'Unable to intercept datocms query. No viable JSON alternative was defined.'; - interceptData = {error: errorText}; - } - - return interceptData; + return interceptData; } function getPageQueryBypassWhitelist() : string[] { - //-----stuff that has been (to the best of my knowledge) completley pulled over into JSON - //-----does not include iterative stuff like blog posts or events etc... - //-----this may only end up being scaffolding. - - let whitelist : string [] = []; - - whitelist.push('aboutPage'); - whitelist.push('careersPage'); - whitelist.push('communityPage'); - whitelist.push('contactPage'); - whitelist.push('homePage'); - whitelist.push('partnersPage'); - whitelist.push('pressPage'); - whitelist.push('privacyPage'); - whitelist.push('productsPage'); - whitelist.push('termsPage'); - - whitelist.push('header'); - whitelist.push('footer'); - - return whitelist; + //-----stuff that has been (to the best of my knowledge) completley pulled over into JSON + //-----does not include iterative stuff like blog posts or events etc... + //-----this may only end up being scaffolding. + + let whitelist : string [] = []; + + whitelist.push('aboutPage'); + whitelist.push('careersPage'); + whitelist.push('communityPage'); + whitelist.push('contactPage'); + whitelist.push('homePage'); + whitelist.push('partnersPage'); + whitelist.push('pressPage'); + whitelist.push('privacyPage'); + whitelist.push('productsPage'); + whitelist.push('termsPage'); + + whitelist.push('header'); + whitelist.push('footer'); + + return whitelist; } function getListingQueryDirectories() : string[] { - //-----whitelist the listying types of content... - - let directories : string[] = []; - - directories.push('allEvents'); - directories.push('allPositions'); - directories.push('allPressReleases'); - directories.push('allTeams'); - directories.push('allTestimonials'); - - return directories; - + //-----whitelist the listying types of content... + + let directories : string[] = []; + + directories.push('allEvents'); + directories.push('allPositions'); + directories.push('allPressReleases'); + directories.push('allTeams'); + directories.push('allTestimonials'); + + return directories; + } function pluckFirstParentFromQuery(query : any) : string { - - //-----only plucks the FIRST parent, if there are multiple parents in the query they will be ignored. - let parent : string = ((query.replace(/[\W_]+/g," ")).trim()).split(" ")[0]; - return parent; - + + //-----only plucks the FIRST parent, if there are multiple parents in the query they will be ignored. + let parent : string = ((query.replace(/[\W_]+/g," ")).trim()).split(" ")[0]; + return parent; + } async function datocmsPageQueryIntercept(query : any) : Promise { - - let parent : string = pluckFirstParentFromQuery(query); + + let parent : string = pluckFirstParentFromQuery(query); let jsonDirectory : string = path.join(process.cwd(), 'json/site_content'); let jsonPath : string = jsonDirectory + '/' + parent + '.json'; - let fileRawContents : string = ''; - let fileJson : any = {} - let jsonData : any = {} - - try { - fileRawContents = await fs.readFile(jsonPath, 'utf8'); - fileJson = JSON.parse(fileRawContents); - jsonData = fileJson.data; - } catch (e) { - console.log('Failed to parse JSON for file ' + jsonPath); - console.log('JSON parse attempt resulted in the following error:'); - console.log(e); - } - - - - return jsonData; - + let fileRawContents : string = ''; + let fileJson : any = {} + let jsonData : any = {} + + try { + fileRawContents = await fs.readFile(jsonPath, 'utf8'); + fileJson = JSON.parse(fileRawContents); + jsonData = fileJson.data; + } catch (e) { + console.log('Failed to parse JSON for file ' + jsonPath); + console.log('JSON parse attempt resulted in the following error:'); + console.log(e); + } + + return jsonData; + } async function datocmsListingQueryIntercept(query : any) : Promise { + + let parent : string = pluckFirstParentFromQuery(query); + let jsonData : any = {}; + + switch(parent) { + case 'allBlogPosts': + //-----blog listing is handled in its own annoyingly special way + //-----could build it out here to be verbose but will not in the intrest of time/budget + //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogPostsJson(); to match the would-be expected format. + //jsonData = await getAllBlogPostsJson(); + jsonData = {}; + break; + case 'allCategories': + //-----category listing is handled in its own annoyingly special way + //-----could build it out here to be verbose but will not in the intrest of time/budget + //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogCategoriesJson(); to match the would-be expected format. + //jsonData = await getAllBlogCategoriesJson(); + jsonData = {}; + break; + case 'allEvents': + jsonData = await getAllEventsJson(); + break; + case 'allPositions': + jsonData = await getAllPositionsJson(); + break; + case 'allPressReleases': + jsonData = await getAllPressReleasesJson(); + break; + case 'allTeams': + jsonData = await getAllTeamsJson(); + break; + case 'allTestimonials': + jsonData = await getAllTestimonialsJson(); + break; + default: + jsonData = {}; + break; + } + + return jsonData; - let parent : string = pluckFirstParentFromQuery(query); - let jsonData : any = {}; - - switch(parent) { - case 'allBlogPosts': - //-----blog listing is handled in its own annoyingly special way - //-----could build it out here to be verbose but will not in the intrest of time/budget - //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogPostsJson(); to match the would-be expected format. - //jsonData = await getAllBlogPostsJson(); - jsonData = {}; - break; - case 'allCategories': - //-----category listing is handled in its own annoyingly special way - //-----could build it out here to be verbose but will not in the intrest of time/budget - //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogCategoriesJson(); to match the would-be expected format. - //jsonData = await getAllBlogCategoriesJson(); - jsonData = {}; - break; - case 'allEvents': - jsonData = await getAllEventsJson(); - break; - case 'allPositions': - jsonData = await getAllPositionsJson(); - break; - case 'allPressReleases': - jsonData = await getAllPressReleasesJson(); - break; - case 'allTeams': - jsonData = await getAllTeamsJson(); - break; - case 'allTestimonials': - jsonData = await getAllTestimonialsJson(); - break; - default: - jsonData = {}; - break; - } - - return jsonData; - } //-----json traversal function jsonFilePathIsValid(filePath : string) { - - if (filePath.startsWith("_")) { - return false; - } - if (!filePath.endsWith(".json")) { - return false; - } - - return true; + if (filePath.startsWith("_")) { + return false; + } + + if (!filePath.endsWith(".json")) { + return false; + } + + return true; } function jsonNodeExists(jsonData : any, node : string) { - //-----this condition is unneccessary... so THATS what typescript is for XD - //if (typeof node === 'string' || node instanceof String) { - if (node in jsonData === true) { - return true; - } - //} + //-----this condition is unneccessary... so THATS what typescript is for XD + //if (typeof node === 'string' || node instanceof String) { + if (node in jsonData === true) { + return true; + } + //} - return false; + return false; } function jsonNodesExist(jsonData : any, nodes : any) { - //-----permit basic validation of the json we are trying to spit out by checking for existence of first order nodes - - let node = ''; - let nodeGroup : any = []; - let nodeGroupNode = ''; - let nodeGroupValid = false; + //-----permit basic validation of the json we are trying to spit out by checking for existence of first order nodes + + let node = ''; + let nodeGroup : any = []; + let nodeGroupNode = ''; + let nodeGroupValid = false; + + for (let i = 0; i < nodes.length; i++) { + node = nodes[i]; + if (typeof node === 'string') { + //-----string means that PRIMARY node must exist... no checking for children etc... + if (jsonNodeExists(jsonData, node) === false) { + return false; + } + } else if (Array.isArray(node)) { + //-----array means json is valid if ANY of the nodes in that array exist + nodeGroup = node; + nodeGroupValid = false; + for (let j = 0; j < nodeGroup.length; j++) { + nodeGroupNode = nodeGroup[j]; + if (jsonNodeExists(jsonData, nodeGroupNode) === true) { + nodeGroupValid = true; + } + } + if (nodeGroupValid === false) { + //-----none of the nodes in the group exist. + return false; + } + } else { + //-----lets not get too crazy here. + return false; + } + } + + return true; - for (let i = 0; i < nodes.length; i++) { - node = nodes[i]; - if (typeof node === 'string') { - //-----string means that PRIMARY node must exist... no checking for children etc... - if (jsonNodeExists(jsonData, node) === false) { - return false; - } - } else if (Array.isArray(node)) { - //-----array means json is valid if ANY of the nodes in that array exist - nodeGroup = node; - nodeGroupValid = false; - for (let j = 0; j < nodeGroup.length; j++) { - nodeGroupNode = nodeGroup[j]; - if (jsonNodeExists(jsonData, nodeGroupNode) === true) { - nodeGroupValid = true; - } - } - if (nodeGroupValid === false) { - //-----none of the nodes in the group exist. - return false; - } - } else { - //-----lets not get too crazy here. - return false; - } - } - - return true; - } export async function getJsonItemsFromDirectory(jsonDirectory : string, pluckerFunction : any, validationNodes : any[]) : Promise { - let jsonFiles = await fs.readdir(jsonDirectory); - let returnJson : any[] = []; + let jsonFiles = await fs.readdir(jsonDirectory); + let returnJson : any[] = []; - for (let i = 0; i < jsonFiles.length; i++) { - let jsonFile = jsonFiles[i]; - if (jsonFilePathIsValid(jsonFile) === true) { - let jsonPath = path.join(jsonDirectory, jsonFile); - let fileRawContents = await fs.readFile(jsonPath, 'utf8'); - let fileJsonContents = {}; - let jsonParseSuccess = false; - - try { - fileJsonContents = JSON.parse(fileRawContents); - jsonParseSuccess = true; - } catch (e) { - jsonParseSuccess = false; - console.log('Failed to parse JSON for file ' + jsonPath); - console.log('JSON parse attempt resulted in the following error:'); - console.log(e); - } - - if (jsonParseSuccess === true) { - fileJsonContents = JSON.parse(fileRawContents); - let jsonData = pluckerFunction(fileJsonContents); - jsonData.sourceFile = jsonFile; + for (let i = 0; i < jsonFiles.length; i++) { + let jsonFile = jsonFiles[i]; + if (jsonFilePathIsValid(jsonFile) === true) { + let jsonPath = path.join(jsonDirectory, jsonFile); + let fileRawContents = await fs.readFile(jsonPath, 'utf8'); + let fileJsonContents = {}; + let jsonParseSuccess = false; - if (jsonNodesExist(jsonData, validationNodes) === true) { - returnJson.push(jsonData); - } - } - } - } - - return returnJson; + try { + fileJsonContents = JSON.parse(fileRawContents); + jsonParseSuccess = true; + } catch (e) { + jsonParseSuccess = false; + console.log('Failed to parse JSON for file ' + jsonPath); + console.log('JSON parse attempt resulted in the following error:'); + console.log(e); + } + + if (jsonParseSuccess === true) { + fileJsonContents = JSON.parse(fileRawContents); + let jsonData = pluckerFunction(fileJsonContents); + jsonData.sourceFile = jsonFile; + + if (jsonNodesExist(jsonData, validationNodes) === true) { + returnJson.push(jsonData); + } + } + } + } + + return returnJson; } //-----blogPosts function getBlogJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/blogPost'); + return path.join(process.cwd(), 'json/site_content/blogPost'); } function pluckBlogPostData(json : any) { - - //let plucked : BlogPostRecord = {}; - let plucked : any = {}; - if('data' in json){ - if('blogPost' in json.data) { - plucked = json.data.blogPost; - } - } - - return plucked; - + //let plucked : BlogPostRecord = {}; + let plucked : any = {}; + + if('data' in json){ + if('blogPost' in json.data) { + plucked = json.data.blogPost; + } + } + + return plucked; + } function getRequiedBlogPostNodes() : any[] { - - let nodes = []; - let contentNodes = []; + let nodes = []; + let contentNodes = []; + + contentNodes.push('content'); + contentNodes.push('htmlContent'); + + //-----slug is now set per the JSON filename to avoid mismatches in routing + //nodes.push('slug'); + nodes.push('date'); + nodes.push('title'); + nodes.push(contentNodes); + + return nodes; - contentNodes.push('content'); - contentNodes.push('htmlContent'); - - //-----slug is now set per the JSON filename to avoid mismatches in routing - //nodes.push('slug'); - nodes.push('date'); - nodes.push('title'); - nodes.push(contentNodes); - - return nodes; - } function forceBlogPostsJsonSlugIntegrity(json : any[]) { - //-----this is used to force the blog post slug to match that of its parent file name. - - let returnJson : any[] = []; - - for (let i = 0; i < json.length; i++) { - let blogPost = json[i]; - let sourceFile = blogPost.sourceFile; - let newSlug = path.parse(sourceFile).name; - blogPost.slug = newSlug; - returnJson.push(blogPost); - } + //-----this is used to force the blog post slug to match that of its parent file name. - return returnJson; + let returnJson : any[] = []; + + for (let i = 0; i < json.length; i++) { + let blogPost = json[i]; + let sourceFile = blogPost.sourceFile; + let newSlug = path.parse(sourceFile).name; + blogPost.slug = newSlug; + returnJson.push(blogPost); + } + + return returnJson; } function sortBlogPostsJsonByDate(json : any[]) { - - let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)); - - return sortedJson; + + let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)); + + return sortedJson; } function getFakePaginationData(json : any) { - - let pagination : any = {}; - let totalPosts = json.length; - - pagination.nextPage = null; - pagination.page = 1; - pagination.step = totalPosts; - pagination.total = totalPosts; - pagination.totalPages = 1; - - return pagination; + + let pagination : any = {}; + let totalPosts = json.length; + + pagination.nextPage = null; + pagination.page = 1; + pagination.step = totalPosts; + pagination.total = totalPosts; + pagination.totalPages = 1; + + return pagination; } async function getAllBlogPostsJson() { - let jsonDirectory = getBlogJsonDirectoryPath(); - let validationNodes = getRequiedBlogPostNodes(); - let blogPosts = await getJsonItemsFromDirectory(jsonDirectory, pluckBlogPostData, validationNodes); - let pagination = getFakePaginationData(blogPosts); - let processedBlogPosts = forceBlogPostsJsonSlugIntegrity(blogPosts); - let sortedBlogPosts : any[] = sortBlogPostsJsonByDate(processedBlogPosts); - let allBlogPostsJson = {pagination: pagination, data: sortedBlogPosts}; - - return allBlogPostsJson; + let jsonDirectory = getBlogJsonDirectoryPath(); + let validationNodes = getRequiedBlogPostNodes(); + let blogPosts = await getJsonItemsFromDirectory(jsonDirectory, pluckBlogPostData, validationNodes); + let pagination = getFakePaginationData(blogPosts); + let processedBlogPosts = forceBlogPostsJsonSlugIntegrity(blogPosts); + let sortedBlogPosts : any[] = sortBlogPostsJsonByDate(processedBlogPosts); + let allBlogPostsJson = {pagination: pagination, data: sortedBlogPosts}; + + return allBlogPostsJson; } export async function getAllBlogPostsSlugsFromSource() { - - let jsonDirectory = getBlogJsonDirectoryPath(); - let jsonFiles = await fs.readdir(jsonDirectory); - let slugs = []; - for (let i = 0; i < jsonFiles.length; i++) { - let jsonFile = jsonFiles[i]; - if (jsonFilePathIsValid(jsonFile) === true) { - let slug = path.parse(jsonFile).name; - let node = {slug : slug}; - slugs.push(node); - } - } - - return slugs; - + let jsonDirectory = getBlogJsonDirectoryPath(); + let jsonFiles = await fs.readdir(jsonDirectory); + let slugs = []; + + for (let i = 0; i < jsonFiles.length; i++) { + let jsonFile = jsonFiles[i]; + if (jsonFilePathIsValid(jsonFile) === true) { + let slug = path.parse(jsonFile).name; + let node = {slug : slug}; + slugs.push(node); + } + } + + return slugs; + } export async function getAllBlogPostsFromSource(datocmsFilters : any) : Promise { - let allBlogPostsFromSource : any = {}; - - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { - allBlogPostsFromSource = await getAllBlogPostsJson(); - } else { - allBlogPostsFromSource = serverGetBlogPosts(datocmsFilters); - } - - return allBlogPostsFromSource; - + let allBlogPostsFromSource : any = {}; + + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { + allBlogPostsFromSource = await getAllBlogPostsJson(); + } else { + allBlogPostsFromSource = serverGetBlogPosts(datocmsFilters); + } + + return allBlogPostsFromSource; + } async function getSingleBlogPostJsonBySlug(slug : string) : Promise { - - let jsonDirectory = getBlogJsonDirectoryPath(); - let jsonFile = slug + '.json'; - let jsonPath = path.join(jsonDirectory, jsonFile); - let blogPost : any = {}; - - try { - let fileRawContents = await fs.readFile(jsonPath, 'utf8'); - let fileJsonContents = JSON.parse(fileRawContents); - blogPost = pluckBlogPostData(fileJsonContents); - //-----continue to enforce standard set by forceBlogPostsJsonSlugIntegrity() - blogPost.slug = slug; - } catch (e) { - console.log('Failed to open or parse JSON for file ' + jsonPath); - console.log('JSON parse attempt resulted in the following error:'); - console.log(e); - } - - return blogPost; - + + let jsonDirectory = getBlogJsonDirectoryPath(); + let jsonFile = slug + '.json'; + let jsonPath = path.join(jsonDirectory, jsonFile); + let blogPost : any = {}; + + try { + let fileRawContents = await fs.readFile(jsonPath, 'utf8'); + let fileJsonContents = JSON.parse(fileRawContents); + blogPost = pluckBlogPostData(fileJsonContents); + //-----continue to enforce standard set by forceBlogPostsJsonSlugIntegrity() + blogPost.slug = slug; + } catch (e) { + console.log('Failed to open or parse JSON for file ' + jsonPath); + console.log('JSON parse attempt resulted in the following error:'); + console.log(e); + } + + return blogPost; + } export async function getSingleBlogPostBySlugFromSource(slug : string) : Promise { - let blogPostJson : any = {}; - - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { - try { - //-----old highly inefficent way, - //-----used before we forced slug to be re written per filename via forceBlogPostsJsonSlugIntegrity() - /* - let blogPosts = await getAllBlogPostsJson(); - let blogPostsData = blogPosts.data; - let slugMatches = blogPostsData.filter( - function(blogPostsData) { - return blogPostsData.slug == slug; - } - ); - blogPostJson = slugMatches[0]; - */ - blogPostJson = await getSingleBlogPostJsonBySlug(slug); - } catch (e) { - console.log('Failed pull blog post with slug ' + slug); - console.log('The attempt resulted in the following error:'); - console.log(e); - } - } else { - blogPostJson = await (await cms().SingleBlogPost({ slug })).blogPost - } - - return blogPostJson; - + let blogPostJson : any = {}; + + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { + try { + //-----old highly inefficent way, + //-----used before we forced slug to be re written per filename via forceBlogPostsJsonSlugIntegrity() + /* + let blogPosts = await getAllBlogPostsJson(); + let blogPostsData = blogPosts.data; + let slugMatches = blogPostsData.filter( + function(blogPostsData) { + return blogPostsData.slug == slug; + } + ); + blogPostJson = slugMatches[0]; + */ + blogPostJson = await getSingleBlogPostJsonBySlug(slug); + } catch (e) { + console.log('Failed pull blog post with slug ' + slug); + console.log('The attempt resulted in the following error:'); + console.log(e); + } + } else { + blogPostJson = await (await cms().SingleBlogPost({ slug })).blogPost + } + + return blogPostJson; + } function getBlogPostCategorySlugs(blogPostJson : any) { - - let categories = []; - - try { - let categoryNode = blogPostJson.category; - for (let i = 0; i < categoryNode.length; i++) { - let category = categoryNode[i].slug; - categories.push(category); - } - - } catch (e) { - console.log('Failed pull blog post categories'); - console.log('The attempt resulted in the following error:'); - console.log(e); - } - - return categories; - + + let categories = []; + + try { + let categoryNode = blogPostJson.category; + for (let i = 0; i < categoryNode.length; i++) { + let category = categoryNode[i].slug; + categories.push(category); + } + } catch (e) { + console.log('Failed pull blog post categories'); + console.log('The attempt resulted in the following error:'); + console.log(e); + } + + return categories; + } export async function getRelatedBlogPosts(blogPostJson : any, matchCount : number) : Promise { - - let relatedBlogPosts = []; - let reservedSlugs = [] - let matchedCount = 0; - let matchCategories = getBlogPostCategorySlugs(blogPostJson); - let blogPosts = await getAllBlogPostsJson(); - let blogPostsData = blogPosts.data; - let candidateBlogPost : any = {}; - let candidateBlogPostSlug = ''; - let slug = blogPostJson.slug; - - if (matchCategories.length > 0) { - reservedSlugs.push(slug); - for (let i = 0; i < blogPostsData.length; i++) { - candidateBlogPost = blogPostsData[i]; - candidateBlogPostSlug = candidateBlogPost.slug; - if (matchedCount >= matchCount) { - break; - } - if (reservedSlugs.includes(candidateBlogPostSlug) === false) { - let candidateCategories = getBlogPostCategorySlugs(candidateBlogPost); - for (let j = 0; j < matchCategories.length; j++) { - let matchCategory = matchCategories[j]; - if (candidateCategories.includes(matchCategory) === true && reservedSlugs.includes(candidateBlogPostSlug) === false) { - relatedBlogPosts.push(candidateBlogPost); - reservedSlugs.push(candidateBlogPostSlug) - matchedCount++; - break; - } - } - - } - } - } - if (matchedCount < matchCount) { - //-----if not enough matches, pull in anything thats left... - for (let i = 0; i < blogPostsData.length; i++) { - candidateBlogPost = blogPostsData[i]; - candidateBlogPostSlug = candidateBlogPost.slug; - if (matchedCount >= matchCount) { - break; - } - if (reservedSlugs.includes(candidateBlogPostSlug) === false) { - relatedBlogPosts.push(candidateBlogPost); - reservedSlugs.push(candidateBlogPostSlug) - matchedCount++; - } - } - } - - return relatedBlogPosts; - + let relatedBlogPosts = []; + let reservedSlugs = [] + let matchedCount = 0; + let matchCategories = getBlogPostCategorySlugs(blogPostJson); + let blogPosts = await getAllBlogPostsJson(); + let blogPostsData = blogPosts.data; + let candidateBlogPost : any = {}; + let candidateBlogPostSlug = ''; + let slug = blogPostJson.slug; + + if (matchCategories.length > 0) { + reservedSlugs.push(slug); + for (let i = 0; i < blogPostsData.length; i++) { + candidateBlogPost = blogPostsData[i]; + candidateBlogPostSlug = candidateBlogPost.slug; + if (matchedCount >= matchCount) { + break; + } + if (reservedSlugs.includes(candidateBlogPostSlug) === false) { + let candidateCategories = getBlogPostCategorySlugs(candidateBlogPost); + for (let j = 0; j < matchCategories.length; j++) { + let matchCategory = matchCategories[j]; + if (candidateCategories.includes(matchCategory) === true && reservedSlugs.includes(candidateBlogPostSlug) === false) { + relatedBlogPosts.push(candidateBlogPost); + reservedSlugs.push(candidateBlogPostSlug) + matchedCount++; + break; + } + } + + } + } + } + + if (matchedCount < matchCount) { + //-----if not enough matches, pull in anything thats left... + for (let i = 0; i < blogPostsData.length; i++) { + candidateBlogPost = blogPostsData[i]; + candidateBlogPostSlug = candidateBlogPost.slug; + if (matchedCount >= matchCount) { + break; + } + if (reservedSlugs.includes(candidateBlogPostSlug) === false) { + relatedBlogPosts.push(candidateBlogPost); + reservedSlugs.push(candidateBlogPostSlug) + matchedCount++; + } + } + } + + return relatedBlogPosts; + } //-----category function getBlogCategoriesJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/category'); + return path.join(process.cwd(), 'json/site_content/category'); } function pluckBlogCategoriesData(json : any) { - - let plucked = {}; - if('data' in json){ - if('category' in json.data) { - plucked = json.data.category; - } - } - - return plucked; - + let plucked = {}; + + if('data' in json){ + if('category' in json.data) { + plucked = json.data.category; + } + } + + return plucked; + } function getRequiedBlogBlogCategoryNodes() { - - let nodes = []; - - nodes.push('slug'); - nodes.push('title'); - - return nodes; - + + let nodes = []; + + nodes.push('slug'); + nodes.push('title'); + + return nodes; + } export async function getAllBlogCategoriesJson() : Promise { - let jsonDirectory = getBlogCategoriesJsonDirectoryPath(); - let validationNodes = getRequiedBlogBlogCategoryNodes(); - let allBlogCategoriesJson : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckBlogCategoriesData, validationNodes) - - return allBlogCategoriesJson; + let jsonDirectory = getBlogCategoriesJsonDirectoryPath(); + let validationNodes = getRequiedBlogBlogCategoryNodes(); + let allBlogCategoriesJson : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckBlogCategoriesData, validationNodes) + + return allBlogCategoriesJson; } export async function getAllBlogPostsCategoriesFromSource() : Promise { - let allBlogPostsCategoriesFromSource : any[] = []; - - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { - allBlogPostsCategoriesFromSource = await getAllBlogCategoriesJson(); - //let requestResults = await request(AllCategoriesQuery); - //allBlogPostsCategoriesFromSource = requestResults.allCategories; - } else { - allBlogPostsCategoriesFromSource = await serverGetBlogPostsCategories(); - } + let allBlogPostsCategoriesFromSource : any[] = []; + + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { + allBlogPostsCategoriesFromSource = await getAllBlogCategoriesJson(); + //let requestResults = await request(AllCategoriesQuery); + //allBlogPostsCategoriesFromSource = requestResults.allCategories; + } else { + allBlogPostsCategoriesFromSource = await serverGetBlogPostsCategories(); + } + + return allBlogPostsCategoriesFromSource; -console.log(allBlogPostsCategoriesFromSource); - - return allBlogPostsCategoriesFromSource; - } //-----event function getEventsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/event'); + return path.join(process.cwd(), 'json/site_content/event'); } function pluckEventData(json : any) { - - let plucked = {}; - if('data' in json){ - if('event' in json.data) { - plucked = json.data.event; - } - } - - return plucked; - + let plucked = {}; + + if('data' in json){ + if('event' in json.data) { + plucked = json.data.event; + } + } + + return plucked; + } function getRequiedEventNodes() { - - let nodes = []; - - nodes.push('title'); - nodes.push('eventLocation'); - nodes.push('eventStartdate'); - nodes.push('eventEnddate'); - - return nodes; - + + let nodes = []; + + nodes.push('title'); + nodes.push('eventLocation'); + nodes.push('eventStartdate'); + nodes.push('eventEnddate'); + + return nodes; + } function sortEventsJsonByStartDate(json : any[]) { - - let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.eventStartdate) - datocmsDateToIng(a.eventStartdate)); - - return sortedJson; + + let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.eventStartdate) - datocmsDateToIng(a.eventStartdate)); + + return sortedJson; } async function getAllEventsJson() : Promise { - let jsonDirectory = getEventsJsonDirectoryPath(); - let validationNodes = getRequiedEventNodes(); - let events : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckEventData, validationNodes); - let sortedEvents : any[] = sortEventsJsonByStartDate(events); - let allEventsJson = {allEvents: sortedEvents}; - - return allEventsJson; + let jsonDirectory = getEventsJsonDirectoryPath(); + let validationNodes = getRequiedEventNodes(); + let events : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckEventData, validationNodes); + let sortedEvents : any[] = sortEventsJsonByStartDate(events); + let allEventsJson = {allEvents: sortedEvents}; + + return allEventsJson; } //-----position function getPositionsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/position'); + return path.join(process.cwd(), 'json/site_content/position'); } function pluckPositionData(json : any) { - - let plucked = {}; - if('data' in json){ - if('position' in json.data) { - plucked = json.data.position; - } - } - - return plucked; - + let plucked = {}; + + if('data' in json){ + if('position' in json.data) { + plucked = json.data.position; + } + } + + return plucked; + } function getRequiedPositionNodes() { - - let nodes = []; - - nodes.push('id'); - nodes.push('positionName'); - nodes.push('positionLink'); - - return nodes; - + + let nodes = []; + + nodes.push('id'); + nodes.push('positionName'); + nodes.push('positionLink'); + + return nodes; + } function sortPositionsJsonById(json : any[]) { - - let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); - - return sortedJson; + + let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); + + return sortedJson; } async function getAllPositionsJson() : Promise { - let jsonDirectory = getPositionsJsonDirectoryPath(); - let validationNodes = getRequiedPositionNodes(); - let positions : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPositionData, validationNodes); - let sortedPositions : any[] = sortPositionsJsonById(positions); - let allPositionsJson = {allPositions: sortedPositions}; - - return allPositionsJson; + let jsonDirectory = getPositionsJsonDirectoryPath(); + let validationNodes = getRequiedPositionNodes(); + let positions : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPositionData, validationNodes); + let sortedPositions : any[] = sortPositionsJsonById(positions); + let allPositionsJson = {allPositions: sortedPositions}; + + return allPositionsJson; } //-----pressRelease function getPressReleasesJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/pressRelease'); + return path.join(process.cwd(), 'json/site_content/pressRelease'); } function pluckPressReleaseData(json : any) { - - let plucked = {}; - if('data' in json){ - if('pressRelease' in json.data) { - plucked = json.data.pressRelease; - } - } - - return plucked; - + let plucked = {}; + + if('data' in json){ + if('pressRelease' in json.data) { + plucked = json.data.pressRelease; + } + } + + return plucked; + } function getRequiedPressReleaseNodes() { - - let nodes = []; - - nodes.push('id'); - nodes.push('title'); - nodes.push('date'); - nodes.push('link'); - - return nodes; - + + let nodes = []; + + nodes.push('id'); + nodes.push('title'); + nodes.push('date'); + nodes.push('link'); + + return nodes; + } function sortPressReleasesJsonByDate(json : any[]) { - - let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)); - - return sortedJson; + + let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)); + + return sortedJson; } async function getAllPressReleasesJson() : Promise { - let jsonDirectory = getPressReleasesJsonDirectoryPath(); - let validationNodes = getRequiedPressReleaseNodes(); - let pressReleases : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPressReleaseData, validationNodes); - let sortedPressReleases : any[] = sortPressReleasesJsonByDate(pressReleases); - let allPressReleasesJson = {allPressReleases: sortedPressReleases}; - - return allPressReleasesJson; + let jsonDirectory = getPressReleasesJsonDirectoryPath(); + let validationNodes = getRequiedPressReleaseNodes(); + let pressReleases : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPressReleaseData, validationNodes); + let sortedPressReleases : any[] = sortPressReleasesJsonByDate(pressReleases); + let allPressReleasesJson = {allPressReleases: sortedPressReleases}; + + return allPressReleasesJson; } //-----team function getTeamsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/team'); + return path.join(process.cwd(), 'json/site_content/team'); } function pluckTeamData(json : any) { - - let plucked = {}; - if('data' in json){ - if('team' in json.data) { - plucked = json.data.team; - } - } - - return plucked; - + let plucked = {}; + + if('data' in json){ + if('team' in json.data) { + plucked = json.data.team; + } + } + + return plucked; + } function getRequiedTeamNodes() { - - let nodes = []; - - nodes.push('id'); - nodes.push('memberName'); - - return nodes; - + + let nodes = []; + + nodes.push('id'); + nodes.push('memberName'); + + return nodes; + } function sortTeamsJsonById(json : any[]) { - - let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); - - return sortedJson; + + let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); + + return sortedJson; } async function getAllTeamsJson() : Promise { - let jsonDirectory = getTeamsJsonDirectoryPath(); - let validationNodes = getRequiedTeamNodes(); - let teams : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTeamData, validationNodes); - let sortedTeams : any[] = sortTeamsJsonById(teams); - let allTeamsJson = {allTeams: sortedTeams}; - - return allTeamsJson; + let jsonDirectory = getTeamsJsonDirectoryPath(); + let validationNodes = getRequiedTeamNodes(); + let teams : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTeamData, validationNodes); + let sortedTeams : any[] = sortTeamsJsonById(teams); + let allTeamsJson = {allTeams: sortedTeams}; + + return allTeamsJson; } //-----testimonial function getTestimonialsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/testimonial'); + return path.join(process.cwd(), 'json/site_content/testimonial'); } function pluckTestimonialData(json : any) { - - let plucked = {}; - if('data' in json){ - if('testimonial' in json.data) { - plucked = json.data.testimonial; - } - } - - return plucked; - + let plucked = {}; + + if('data' in json){ + if('testimonial' in json.data) { + plucked = json.data.testimonial; + } + } + + return plucked; + } function getRequiedTestimonialNodes() { - - let nodes = []; - - nodes.push('id'); - nodes.push('testimonialImage'); - nodes.push('testimonialText'); - nodes.push('testimonialUsername'); - - return nodes; - + + let nodes = []; + + nodes.push('id'); + nodes.push('testimonialImage'); + nodes.push('testimonialText'); + nodes.push('testimonialUsername'); + + return nodes; + } function sortTestimonialsJsonById(json : any[]) { - - let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); - - return sortedJson; + + let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); + + return sortedJson; } async function getAllTestimonialsJson() : Promise { - let jsonDirectory = getTestimonialsJsonDirectoryPath(); - let validationNodes = getRequiedTestimonialNodes(); - let testimonials : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTestimonialData, validationNodes); - let sortedTestimonals : any[] = sortTestimonialsJsonById(testimonials); - let allTestimonialsJson = {allTestimonials: sortedTestimonals}; - - return allTestimonialsJson; + let jsonDirectory = getTestimonialsJsonDirectoryPath(); + let validationNodes = getRequiedTestimonialNodes(); + let testimonials : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTestimonialData, validationNodes); + let sortedTestimonals : any[] = sortTestimonialsJsonById(testimonials); + let allTestimonialsJson = {allTestimonials: sortedTestimonals}; + + return allTestimonialsJson; } - - diff --git a/src/lib/datocms.ts b/src/lib/datocms.ts index a30efbe..98cdc55 100644 --- a/src/lib/datocms.ts +++ b/src/lib/datocms.ts @@ -1,10 +1,10 @@ import tiny from 'tiny-json-http' -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== +//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== import { - datocmsQueryIntercept -} from 'lib/datocms-bypass' + datocmsQueryIntercept +} from 'lib/datocms-bypass' //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== @@ -15,10 +15,10 @@ interface props { } export async function request({ query, variables, preview }: props) { - -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== -/* + /* let endpoint = 'https://graphql.datocms.com' if (preview) { @@ -42,19 +42,18 @@ export async function request({ query, variables, preview }: props) { } return body.data -*/ + */ - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { let interceptData : any = await datocmsQueryIntercept(query); if (interceptData.error) { - console.error(interceptData.error); - interceptData = {}; - return interceptData; + console.error(interceptData.error); + interceptData = {}; + return interceptData; } else { - return interceptData; + return interceptData; } - } else { let endpoint = 'https://graphql.datocms.com' @@ -79,8 +78,8 @@ export async function request({ query, variables, preview }: props) { } return body.data - } + } -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== } diff --git a/src/pages/about.tsx b/src/pages/about.tsx index 5ac1f97..b116e46 100644 --- a/src/pages/about.tsx +++ b/src/pages/about.tsx @@ -1,5 +1,5 @@ -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - +//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + /* import { getBlogPosts as serverGetBlogPosts, @@ -12,7 +12,7 @@ import { getAllBlogPostsCategoriesFromSource } from 'lib/datocms-bypass' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { InferGetStaticPropsType } from 'next' @@ -37,21 +37,21 @@ import { request } from '../lib/datocms' export const getStaticProps = async () => { -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - -/* + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + + /* const [allBlogPosts, categories] = await Promise.all([ serverGetBlogPosts({ page: 1 }), serverGetBlogPostsCategories() ]) -*/ + */ const [allBlogPosts, categories] = await Promise.all([ getAllBlogPostsFromSource({ page: 1 }), getAllBlogPostsCategoriesFromSource(), ]) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const heroBlogPost = allBlogPosts.data[0] diff --git a/src/pages/blog/[slug].tsx b/src/pages/blog/[slug].tsx index 87078f8..85826af 100644 --- a/src/pages/blog/[slug].tsx +++ b/src/pages/blog/[slug].tsx @@ -4,7 +4,7 @@ import { getBlogPosts as serverGetBlogPosts } from 'lib/blog' -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== +//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== import { getAllBlogPostsSlugsFromSource, @@ -14,7 +14,7 @@ import { //import cms from 'lib/cms' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { @@ -63,34 +63,34 @@ const BlogPost = ({ export const getStaticPaths: GetStaticPaths = async () => { -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //const posts = await getAllBlogPostsSlugs() async function postsSlugsJsonFromSource() : Promise { let postsSlugsJson : any = {}; - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { - postsSlugsJson = await getAllBlogPostsSlugsFromSource(); + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { + postsSlugsJson = await getAllBlogPostsSlugsFromSource(); } else { postsSlugsJson = await getAllBlogPostsSlugs(); } - - return postsSlugsJson; - } + + return postsSlugsJson; + } const posts = await postsSlugsJsonFromSource(); -/* + /* const paths = posts?.map((post) => ({ params: { slug: post.slug as string } })) -*/ + */ const paths = posts?.map((post : any) => ({ params: { slug: post.slug as string } })) -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== return { paths, fallback: 'blocking' } } @@ -100,31 +100,31 @@ export const getStaticProps: GetStaticProps = async ( ) => { try { const slug = ctx.params?.slug as string - -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //const post = await (await cms().SingleBlogPost({ slug })).blogPost const post : any = await getSingleBlogPostBySlugFromSource(slug); -/* + /* const allBlogPosts = await serverGetBlogPosts({ filterSlugs: [slug], step: 100 }) - + const relatedPosts = allBlogPosts.data.filter((p) => p.category.some((c) => post?.category.some((postCategory) => c.slug === postCategory.slug) ) - ) -*/ - + ) + */ + async function getRelatedBlogPostsFromSource() : Promise { - - let relatedBlogPosts = {}; - - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { - + + let relatedBlogPosts = {}; + + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { + try { relatedBlogPosts = await getRelatedBlogPosts(post, 3); } catch (e) { @@ -147,14 +147,14 @@ export const getStaticProps: GetStaticProps = async ( ) relatedBlogPosts = relatedPosts; - } - - return relatedBlogPosts; + } + + return relatedBlogPosts; } const relatedPosts = await getRelatedBlogPostsFromSource(); -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const [footerData, headerData] = await Promise.all([ request(FooterQuery), diff --git a/src/pages/blog/index.tsx b/src/pages/blog/index.tsx index 9f1773d..dd8d487 100644 --- a/src/pages/blog/index.tsx +++ b/src/pages/blog/index.tsx @@ -1,6 +1,6 @@ import { PageLayout } from 'components/layout/page' -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== +//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== /* import { @@ -19,7 +19,7 @@ import { getAllBlogPostsCategoriesFromSource } from 'lib/datocms-bypass' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { InferGetStaticPropsType } from 'next' import { useRouter } from 'next/router' @@ -40,7 +40,7 @@ import { FooterQuery } from '~/lib/cms/queries/footer' import { HeaderQuery } from '~/lib/cms/queries/header' import { getHrefWithQuery, makeQuery } from '~/lib/utils/router' -import { request } from '../../lib/datocms' +import { request } from '../../lib/datocms' const getMatchingCategory = (categories: CategoryFragment[], c?: string) => categories.find((cat) => cat.slug === c) @@ -52,11 +52,10 @@ const BlogIndexPage = ({ headerData, page: { heroBlogPost } }: InferGetStaticPropsType) => { - - - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { - //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - //-----remove all pagnation, search, sorting etc... as a lot of this was already broken and breaking further with the bypass + + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //-----remove all pagnation, search, sorting etc... as a lot of this was already broken and breaking further with the bypass return ( @@ -70,13 +69,13 @@ const BlogIndexPage = ({ ) - //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== - } else { + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + } else { //===== \/ START ORIGINAL PRE NEXT_PUBLIC_DATOCMS_BYPASS CODE \/ =================================== //-----everything in this ELSE block was the original code //-----naturally the wrapping conditonal WAS not in the original code - //-----typically the pattern has been copy and comment out the orignial code at its current block level, then modify it as needed... - //-----however this was huge block, so easier to do this and explain the variation from the norm. + //-----typically the pattern has been copy and comment out the orignial code at its current block level, then modify it as needed... + //-----however this was huge block, so easier to do this and explain the variation from the norm. const router = useRouter() const { c, s } = router.query const activeCategory = useMemo( @@ -127,21 +126,17 @@ const BlogIndexPage = ({ if ('total' in queriedPosts.pages[0].pagination) { if (queriedPosts.pages[0].pagination.total > 0) { return true; - } + } } - } + } } } return false; } - + //const hasResults = !!queriedPosts?.pages[0]?.data?.length const hasResults = queriedPostsHasResults(queriedPosts); - -//console.log(queriedPosts); -//console.log(queriedPosts.pages[0]); -//console.log(queriedPosts.pages[0].data); return ( @@ -206,7 +201,7 @@ const BlogIndexPage = ({ )} ) - //===== /\ FINISH ORIGINAL PRE NEXT_PUBLIC_DATOCMS_BYPASS CODE /\ =================================== + //===== /\ FINISH ORIGINAL PRE NEXT_PUBLIC_DATOCMS_BYPASS CODE /\ =================================== } } @@ -242,39 +237,39 @@ const clientGetBlogPosts = async ({ return allBlogPosts } - + export const getStaticProps = async () => { - -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== function getInitialBlogPostsDataProp(allBlogPosts : any) { let initialBlogPostsDataProp : any[] = []; - if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === "local_json") { + if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { //-----no pagination initialBlogPostsDataProp = allBlogPosts.data; } else { //-----old setup had pagination but it wasn't working... initialBlogPostsDataProp = allBlogPosts.data.slice(0, 9); } - + return initialBlogPostsDataProp; } -/* + /* const [allBlogPosts, categories] = await Promise.all([ serverGetBlogPosts({ page: 1 }), serverGetBlogPostsCategories() ]) -*/ + */ const [allBlogPosts, categories] = await Promise.all([ getAllBlogPostsFromSource({ page: 1 }), getAllBlogPostsCategoriesFromSource(), ]) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const heroBlogPost = allBlogPosts.data[0] @@ -287,12 +282,12 @@ export const getStaticProps = async () => { props: { initialBlogPosts: { pagination: allBlogPosts.pagination, -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //data: allBlogPosts.data.slice(0, 9) data: getInitialBlogPostsDataProp(allBlogPosts) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== }, footerData: footerData?.footer, headerData: headerData?.header, diff --git a/src/pages/careers.tsx b/src/pages/careers.tsx index 7d8d167..0df798e 100644 --- a/src/pages/careers.tsx +++ b/src/pages/careers.tsx @@ -1,5 +1,5 @@ //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + /* import { getBlogPosts as serverGetBlogPosts, @@ -12,7 +12,7 @@ import { getAllBlogPostsCategoriesFromSource } from 'lib/datocms-bypass' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { InferGetStaticPropsType } from 'next' @@ -37,21 +37,21 @@ import { request } from '../lib/datocms' export const getStaticProps = async () => { -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - -/* + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + + /* const [allBlogPosts, categories] = await Promise.all([ serverGetBlogPosts({ page: 1 }), serverGetBlogPostsCategories() ]) -*/ + */ const [allBlogPosts, categories] = await Promise.all([ getAllBlogPostsFromSource({ page: 1 }), getAllBlogPostsCategoriesFromSource(), ]) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const heroBlogPost = allBlogPosts.data[0] diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 3d5b3a5..cc56602 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,5 +1,5 @@ //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + /* import { getBlogPosts as serverGetBlogPosts, @@ -12,7 +12,7 @@ import { getAllBlogPostsCategoriesFromSource } from 'lib/datocms-bypass' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { InferGetStaticPropsType } from 'next' @@ -36,21 +36,21 @@ import { request } from '../lib/datocms' export const getStaticProps = async () => { -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== -/* + /* const [allBlogPosts, categories] = await Promise.all([ serverGetBlogPosts({ page: 1 }), serverGetBlogPostsCategories() ]) -*/ + */ const [allBlogPosts, categories] = await Promise.all([ getAllBlogPostsFromSource({ page: 1 }), getAllBlogPostsCategoriesFromSource(), ]) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const heroBlogPost = allBlogPosts.data[0] diff --git a/src/pages/partners.tsx b/src/pages/partners.tsx index ba0ebec..87631c0 100644 --- a/src/pages/partners.tsx +++ b/src/pages/partners.tsx @@ -1,5 +1,5 @@ //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + /* import { getBlogPosts as serverGetBlogPosts, @@ -12,7 +12,7 @@ import { getAllBlogPostsCategoriesFromSource } from 'lib/datocms-bypass' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { InferGetStaticPropsType } from 'next' @@ -35,21 +35,21 @@ import { request } from '../lib/datocms' export const getStaticProps = async () => { -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== -/* + /* const [allBlogPosts, categories] = await Promise.all([ serverGetBlogPosts({ page: 1 }), serverGetBlogPostsCategories() ]) -*/ + */ const [allBlogPosts, categories] = await Promise.all([ getAllBlogPostsFromSource({ page: 1 }), getAllBlogPostsCategoriesFromSource(), ]) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const heroBlogPost = allBlogPosts.data[0] diff --git a/src/pages/press.tsx b/src/pages/press.tsx index 14b6cb6..9123fd1 100644 --- a/src/pages/press.tsx +++ b/src/pages/press.tsx @@ -1,5 +1,5 @@ //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + /* import { getBlogPosts as serverGetBlogPosts, @@ -12,7 +12,7 @@ import { getAllBlogPostsCategoriesFromSource } from 'lib/datocms-bypass' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { InferGetStaticPropsType } from 'next' @@ -32,21 +32,21 @@ import { request } from '../lib/datocms' export const getStaticProps = async () => { -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== -/* + /* const [allBlogPosts, categories] = await Promise.all([ serverGetBlogPosts({ page: 1 }), serverGetBlogPostsCategories() ]) -*/ + */ const [allBlogPosts, categories] = await Promise.all([ getAllBlogPostsFromSource({ page: 1 }), getAllBlogPostsCategoriesFromSource(), ]) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const heroBlogPost = allBlogPosts.data[0] diff --git a/src/pages/products.tsx b/src/pages/products.tsx index ddc521e..7340e0e 100644 --- a/src/pages/products.tsx +++ b/src/pages/products.tsx @@ -1,5 +1,5 @@ //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== - + /* import { getBlogPosts as serverGetBlogPosts, @@ -12,7 +12,7 @@ import { getAllBlogPostsCategoriesFromSource } from 'lib/datocms-bypass' -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== +//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== import { InferGetStaticPropsType } from 'next' @@ -40,21 +40,21 @@ import { request } from '../lib/datocms' export const getStaticProps = async () => { -//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== + //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== -/* + /* const [allBlogPosts, categories] = await Promise.all([ serverGetBlogPosts({ page: 1 }), serverGetBlogPostsCategories() ]) -*/ + */ const [allBlogPosts, categories] = await Promise.all([ getAllBlogPostsFromSource({ page: 1 }), getAllBlogPostsCategoriesFromSource(), ]) - -//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== + + //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== const heroBlogPost = allBlogPosts.data[0]