Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provider AquilaCMS #25

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NEXT_PUBLIC_PROVIDER=aquila-cms
NEXT_PUBLIC_API_URL=https://dem01.aquila-cms.com/api
NEXT_PUBLIC_IMG_URL=https://dem01.aquila-cms.com

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@stripe/react-stripe-js": "^1.1.2",
"@stripe/stripe-js": "^1.11.0",
"autoprefixer": "^10.1.0",
"axios": "^0.21.1",
"next": "10.0.4",
"postcss": "^8.2.2",
"react": "17.0.1",
Expand Down
4 changes: 4 additions & 0 deletions pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import '../styles/globals.css'
import Layout from '../layouts/layout'
import fetchCategories from '../utils/categoryProvider'
import axios from 'axios';

if(process.env.NEXT_PUBLIC_API_URL) {
axios.defaults.baseURL = process.env.NEXT_PUBLIC_API_URL;
}
function Ecommerce({ Component, pageProps, categories }) {
return (
<Layout categories={categories}>
Expand Down
31 changes: 17 additions & 14 deletions pages/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,24 @@ export async function getStaticProps() {
const inventory = await fetchInventory()
const inventoryCategories = inventory.reduce((acc, next) => {
const categories = next.categories
categories.forEach(c => {
const index = acc.findIndex(item => item.name === c)
if (index !== -1) {
const item = acc[index]
item.itemCount = item.itemCount + 1
acc[index] = item
} else {
const item = {
name: c,
image: next.image,
itemCount: 1
if(categories) {
categories.forEach(c => {
const index = acc.findIndex(item => item.name === c)
if (index !== -1) {
const item = acc[index]
item.itemCount = item.itemCount + 1
acc[index] = item
} else {
const item = {
name: c,
image: next.image,
itemCount: 1
}

acc.push(item)
}
acc.push(item)
}
})
})
}
return acc
}, [])

Expand Down
13 changes: 13 additions & 0 deletions provider/aquila-cms/categoryProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
async function fetchCategories () {
const response = await fetch(process.env.NEXT_PUBLIC_API_URL + '/v2/categories', {
method: 'POST',
body: JSON.stringify({lang:"en", PostBody: {"limit":99, "filter":{"code":"my-products"}, "structure":{"children":1}, "populate":["children"]}}),
headers: {"Content-type": "application/json; charset=UTF-8"}
})
const data = await response.json();
return data.datas[0].children.map(cat => cat.name)
}

export default {
fetchCategories
}
20 changes: 20 additions & 0 deletions provider/aquila-cms/inventoryByCategory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function inventoryByCategory (inventory) {
return inventory.reduce((acc, next) => {
const categories = next.categories
categories.forEach(c => {
const category = c.replace(/-/g," "); // Need to transform for matching datas
if (acc[category]) {
acc[category].items.push(next)
} else {
acc[category] = {}
acc[category].items = []
acc[category].items.push(next)
}
})
return acc
}, {})
}

export default {
inventoryByCategory
}
62 changes: 62 additions & 0 deletions provider/aquila-cms/inventoryProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import axios from 'axios'
axios.defaults.baseURL = process.env.NEXT_PUBLIC_API_URL;

async function fetchInventory(category) {
let aqlPrds = [];

if(category) {
const catResp = await axios.post('/v2/category', {lang:"en", PostBody: {filter: {['translation.en.name']: {$regex: category.toLowerCase(), $options: "i"}}}})

const response = await axios.post('/v2/products/category/' + catResp.data._id, {lang:"en", PostBody: {filter: {}, structure: {['translation.en.description1']: 1, images: 1, ['translation.en.name']: 1, code: 1}, limit: 99}})
aqlPrds = response.data.datas
} else {
const response = await axios.post('/v2/products', {lang:"en", PostBody: {filter: {}, structure: {images: 1}, limit: 99}})
aqlPrds = response.data.datas

}

return convertProducts(aqlPrds);
}

// Convert products to the jamestack schema
async function convertProducts(aqlPrds) {
for(const prd of aqlPrds) {
if(prd.price.ati.special) {
prd.price = prd.price.ati.special;
} else {
prd.price = prd.price.ati.normal;
}
prd.brand = "";
//prd.sku = prd.code;
prd.description = prd.description1.text;
const mainImage = prd.images ? prd.images.find(img => img.default) : null;
prd.image = mainImage ? `${process.env.NEXT_PUBLIC_IMG_URL}/images/products/544x282-80-245,245,245,1/${mainImage._id}/${mainImage.title}${mainImage.extension}` : '';

// Get the linked categories
prd.categories = await findCategoriesForThisProduct(prd._id);
}

return aqlPrds;
}

// Get all categories (for the productsList)
async function findCategoriesForThisProduct(prd_id) {
const result = await axios.post('/v2/categories', {lang:"en", PostBody: {filter: {action:"catalog"}, structure:{productsList:1}, limit: 99}});
const allCategories = result.data.datas;

let prdCategories = [];
for (let j = 0; j < allCategories.length; j++ ) {
const currentCat = allCategories[j];
for (let index = 0; index < currentCat.productsList.length; index++) {
if(currentCat.productsList[index].id == prd_id) {
prdCategories.push(currentCat.slug.en);
}
}
}

return prdCategories;
}

export default {
fetchInventory
}
6 changes: 6 additions & 0 deletions utils/categoryProvider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import inventory from './inventory'

async function fetchCategories () {
// Is provider configured ?
if(process.env.NEXT_PUBLIC_PROVIDER) {
const provider = await import(`../provider/${process.env.NEXT_PUBLIC_PROVIDER}/categoryProvider`);
return provider.default.fetchCategories();
}

const categories = inventory.reduce((acc, next) => {
next.categories.map(category => {
if (acc.includes(category)) return
Expand Down
9 changes: 8 additions & 1 deletion utils/inventoryByCategory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function inventoryByCategory (inventory) {

async function inventoryByCategory (inventory) {
// Is provider configured ?
if(process.env.NEXT_PUBLIC_PROVIDER) {
const provider = await import(`../provider/${process.env.NEXT_PUBLIC_PROVIDER}/inventoryByCategory`);
return provider.default.inventoryByCategory(inventory);
}

return inventory.reduce((acc, next) => {
const categories = next.categories
categories.forEach(c => {
Expand Down
2 changes: 1 addition & 1 deletion utils/inventoryForCategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inventoryByCategory } from './inventoryByCategory'

async function inventoryForCategory (category) {
const inventory = await fetchInventory()
const byCategory = inventoryByCategory(inventory)
const byCategory = await inventoryByCategory(inventory)
return byCategory[category].items
}

Expand Down
6 changes: 5 additions & 1 deletion utils/inventoryProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ type Product {
*/

async function fetchInventory() {
// const inventory = API.get(apiUrl)
// Is provider configured ?
if(process.env.NEXT_PUBLIC_PROVIDER) {
const provider = await import(`../provider/${process.env.NEXT_PUBLIC_PROVIDER}/inventoryProvider`);
return provider.default.fetchInventory();
}
return Promise.resolve(inventory)
}

Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,13 @@ autoprefixer@^10.1.0:
normalize-range "^0.1.2"
postcss-value-parser "^4.1.0"

axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"

[email protected]:
version "6.18.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
Expand Down Expand Up @@ -1708,6 +1715,11 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

follow-redirects@^1.10.0:
version "1.13.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.2.tgz#dd73c8effc12728ba5cf4259d760ea5fb83e3147"
integrity sha512-6mPTgLxYm3r6Bkkg0vNM0HTjfGrOEtsfbhagQvbxDEsEkpNhw582upBaoRZylzen6krEmxXJgt9Ju6HiI4O7BA==

for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down