Skip to content

Commit

Permalink
Merge pull request #9 from techhiveIO/develop
Browse files Browse the repository at this point in the history
Migration to Next 9.4
  • Loading branch information
abedzantout authored Jul 10, 2020
2 parents 7ebb71a + 6b9da39 commit e916ea6
Show file tree
Hide file tree
Showing 27 changed files with 5,552 additions and 3,622 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.next
out
functions-dist
utils
16 changes: 7 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ All notable changes to this project will be documented in this file. See [standa

## [1.2.0](https://github.com/techhiveIO/nextjs-static-boilerplate/compare/v1.1.0...v1.2.0) (2020-04-06)


### Features

* **app:** fetch suggested articles by post ([cdfbd66](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/cdfbd66ec020030534c0236eade157cb5845fc95))
* **contentful:** smarter fetch suggestions mechanism ([12af419](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/12af41909d822acdfd622cd2f33abc5835292ee4))
* **suggestions:** display suggestions + style fixes ([3048681](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/30486814fa2050cae03fa23924e80aba9e1a15ea))

- **app:** fetch suggested articles by post ([cdfbd66](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/cdfbd66ec020030534c0236eade157cb5845fc95))
- **contentful:** smarter fetch suggestions mechanism ([12af419](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/12af41909d822acdfd622cd2f33abc5835292ee4))
- **suggestions:** display suggestions + style fixes ([3048681](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/30486814fa2050cae03fa23924e80aba9e1a15ea))

### Bug Fixes

* **app:** fix post not showing on reload ([8c28cac](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/8c28cacff8529f884eba53a90cdc96e488b4f22a))
* **card:** fix function argument ([15c5132](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/15c513216a505610c5186837572faafb446aa343))
* **footer:** added techhive.io's website link ([41a85af](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/41a85afe7736f1bfcc37038221838256e97d38ab))
* **post:** fix styles not rendering ([7747b3b](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/7747b3b89e805ebfe69d1d1274cf0a944a7d9bb6))
- **app:** fix post not showing on reload ([8c28cac](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/8c28cacff8529f884eba53a90cdc96e488b4f22a))
- **card:** fix function argument ([15c5132](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/15c513216a505610c5186837572faafb446aa343))
- **footer:** added techhive.io's website link ([41a85af](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/41a85afe7736f1bfcc37038221838256e97d38ab))
- **post:** fix styles not rendering ([7747b3b](https://github.com/techhiveIO/nextjs-static-boilerplate/commit/7747b3b89e805ebfe69d1d1274cf0a944a7d9bb6))

## 1.1.0 (2019-09-26)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[![Netlify Status](https://api.netlify.com/api/v1/badges/12ca189b-a0ff-4947-9749-a52bb521006f/deploy-status)](https://app.netlify.com/sites/nextjs-static-starter-kit/deploys)

<p align="center">
<a href="https://techhive.io/" target="blank"><img src="https://www.techhive.io/static/brand/logo-masterclass.svg" width="250" alt="Nest Logo" /></a>
</p>

# Next JS Static starter-kit

by [@techhive.IO](https://www.techhive.io/)
Expand Down
282 changes: 141 additions & 141 deletions core/contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,154 +9,154 @@ const Space = process.env.CONTENTFUL_SPACE;
const Token = process.env.CONTENTFUL_TOKEN;

export class ContentfulService {
private client = createClient({
space: Space,
accessToken: Token,
private client = createClient({
space: Space,
accessToken: Token
});

/**
* Maps the items fetched by contentful
* @param entries
*/
private mapData(entries): BlogPost[] {
return entries.map(({ sys, fields }: { sys: any; fields: any }) => ({
id: sys.id,
title: fields.title,
description: fields.description,
heroImage: fields.heroImage.fields.file.url,
slug: fields.slug,
tags: fields.tags,
publishedAt: fields.publishDate
? new Date(fields.publishDate)
: new Date(sys.createdAt)
}));
}

async fetchPostBySlug(slug) {
return await this.client.getEntries({
content_type: CONTENT_TYPE_BLOGPOST,
'fields.slug': slug
});
}

/**
* Get all tags
*/
async getAllTags() {
const content = await this.client.getEntries({
content_type: CONTENT_TYPE_TAGS
});

/**
* Maps the items fetched by contentful
* @param entries
*/
private mapData(entries): BlogPost[] {
return entries.map(({sys, fields}: { sys: any; fields: any }) => ({
id: sys.id,
title: fields.title,
description: fields.description,
heroImage: fields.heroImage.fields.file.url,
slug: fields.slug,
tags: fields.tags,
publishedAt: fields.publishDate
? new Date(fields.publishDate)
: new Date(sys.createdAt),
}));
}

async fetchPostBySlug(slug) {
return await this.client.getEntries({
content_type: CONTENT_TYPE_BLOGPOST,
'fields.slug': slug,
});
}

/**
* Get all tags
*/
async getAllTags() {
const content = await this.client.getEntries({
content_type: CONTENT_TYPE_TAGS,
});

const tags = content.items.map(
({sys, fields}: { sys: any; fields: any }) => ({
id: sys.id,
name: fields.name,
}),
);

return {tags};
const tags = content.items.map(
({ sys, fields }: { sys: any; fields: any }) => ({
id: sys.id,
name: fields.name
})
);

return { tags };
}

async getBlogPostEntries(
{ limit, skip, tag }: { limit?: number; skip?: number; tag?: string } = {
limit: 5,
skip: 0,
tag: ''
}

async getBlogPostEntries(
{limit, skip, tag}: { limit?: number; skip?: number; tag?: string } = {
limit: 5,
skip: 0,
tag: '',
},
) {
try {
const contents = await this.client.getEntries({
include: 1,
limit,
skip,
order: 'fields.publishDate',
'fields.tags.sys.id': tag,
content_type: CONTENT_TYPE_BLOGPOST,
});

const entries = this.mapData(contents.items);

const total = contents.total;

return {entries, total, limit, skip};
} catch (error) {
// TODO: add error handling
console.log(error);
}
) {
try {
const contents = await this.client.getEntries({
include: 1,
limit,
skip,
order: 'fields.publishDate',
'fields.tags.sys.id': tag,
content_type: CONTENT_TYPE_BLOGPOST
});

const entries = this.mapData(contents.items);

const total = contents.total;

return { entries, total, limit, skip };
} catch (error) {
// TODO: add error handling
console.log(error);
}

async getPostBySlug(slug) {
try {
const content: any = await this.fetchPostBySlug(slug);

const entry: { sys: any; fields: any } = content.items[0];

const author = {
name: entry.fields.author.fields.name,
title: entry.fields.author.fields.title,
company: entry.fields.author.fields.company,
shortBio: entry.fields.author.fields.shortBio,
};

return {
id: entry.sys.id,
slug: entry.fields.slug,
body: entry.fields.body,
title: entry.fields.title,
description: entry.fields.description,
tags: entry.fields.tags,
heroImage: {url: entry.fields.heroImage.fields.file.url},
author: {...author, id: entry.fields.author.sys.id},
publishedAt: entry.fields.publishDate
? new Date(entry.fields.publishDate)
: new Date(entry.sys.createdAt),
};
} catch (error) {
console.error(error);
}
}

async getPostBySlug(slug) {
try {
const content: any = await this.fetchPostBySlug(slug);

const entry: { sys: any; fields: any } = content.items[0];

const author = {
name: entry.fields.author.fields.name,
title: entry.fields.author.fields.title,
company: entry.fields.author.fields.company,
shortBio: entry.fields.author.fields.shortBio
};

return {
id: entry.sys.id,
slug: entry.fields.slug,
body: entry.fields.body,
title: entry.fields.title,
description: entry.fields.description,
tags: entry.fields.tags,
heroImage: { url: entry.fields.heroImage.fields.file.url },
author: { ...author, id: entry.fields.author.sys.id },
publishedAt: entry.fields.publishDate
? new Date(entry.fields.publishDate)
: new Date(entry.sys.createdAt)
};
} catch (error) {
console.error(error);
}
}

async fetchSuggestions(tags: string[], currentArticleSlug: string) {
const limit = 3;
let entries = [];

const initialOptions = {
content_type: CONTENT_TYPE_BLOGPOST,
limit,
// find at least one matching tag, else undefined properties are not copied
'fields.tags.sys.id[in]': tags.length ? tags.join(',') : undefined,
'fields.slug[ne]': currentArticleSlug // exclude current article
};

try {
const suggestionsByTags = await this.client.getEntries(initialOptions);

entries = suggestionsByTags.items;
// number of suggestions by tag is less than the limit
if (suggestionsByTags.total < limit) {
// exclude already picked slugs
const slugsToExclude = [
...suggestionsByTags.items,
{ fields: { slug: currentArticleSlug } }
]
.map((item: { fields: any }) => item.fields.slug)
.join(',');

// fetch random suggestions
const randomSuggestions = await this.client.getEntries({
content_type: CONTENT_TYPE_BLOGPOST,
limit: limit - suggestionsByTags.total,
'fields.slug[nin]': slugsToExclude // exclude slugs already fetched
});

async fetchSuggestions(tags: string[], currentArticleSlug: string) {

const limit = 3;
let entries = [];

const initialOptions = {
content_type: CONTENT_TYPE_BLOGPOST,
limit,
// find at least one matching tag, else undefined properties are not copied
'fields.tags.sys.id[in]': tags.length ? tags.join(',') : undefined,
'fields.slug[ne]': currentArticleSlug, // exclude current article
};

try {

const suggestionsByTags = await this.client.getEntries(initialOptions);

entries = suggestionsByTags.items;
// number of suggestions by tag is less than the limit
if (suggestionsByTags.total < limit) {
// exclude already picked slugs
const slugsToExclude = [...suggestionsByTags.items, {fields: {slug: currentArticleSlug}}]
.map((item: { fields: any }) => item.fields.slug)
.join(',');

// fetch random suggestions
const randomSuggestions = await this.client.getEntries({
content_type: CONTENT_TYPE_BLOGPOST,
limit: limit - suggestionsByTags.total,
'fields.slug[nin]': slugsToExclude, // exclude slugs already fetched
});

entries = [...entries, ...randomSuggestions.items];

}
entries = [...entries, ...randomSuggestions.items];
}

entries = this.mapData(entries);
entries = this.mapData(entries);

return entries;
} catch (e) {
console.error(e);
}
return entries;
} catch (e) {
console.error(e);
}
}
}
2 changes: 1 addition & 1 deletion core/gtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const GA_TRACKING_ID = '';
const isProduction = process.env.NODE_ENV.toLowerCase() === 'production';

// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
export const trackPageView = url => {
export const trackPageView = (url) => {
if (isProduction) {
// @ts-ignore
window.gtag('config', GA_TRACKING_ID, {
Expand Down
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const next_config = {
);

const pages = {
'/': { page: '/' },
'/': { page: '/' }
};

return Object.assign({}, pages, insights);
Expand Down
Loading

0 comments on commit e916ea6

Please sign in to comment.