Skip to content

Instantly share code, notes, and snippets.

@ipatate
Last active July 15, 2022 15:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipatate/40cfe7fa4e68f11889e334e6bbe3bf48 to your computer and use it in GitHub Desktop.
Save ipatate/40cfe7fa4e68f11889e334e6bbe3bf48 to your computer and use it in GitHub Desktop.
id title price tva quantity unit categories image active
amaretti
Amaretti
6
TVA 5.5
100
grammes
src/content/categories/biscuits.md
../../uploads/Amaretti.jpg
true

Les Amaretti sont des biscuits aux amandes et à la fleur d'oranger, naturellement sans gluten. Croquant à l'extérieur et légèrement moelleux à l'intérieur, ce biscuits nous rappelle les parfums d'Orient.

Composition : Poudre d’amande, sucre semoule, blanc d’oeuf BIO, zestes de citron, eau de fleur d’oranger.

Tous nos biscuits et gâteaux sont élaborés dans notre atelier où sont également utilisés des produits contenant les allergènes suivants : fruits à coque (amande, noisette, noix, noix de pécan, noix de coco,...), gluten, sésames, oeuf et lactose.

/**
* fragment for product element
*/
export const Product = () => {
useStaticQuery(graphql`
fragment Product on Mdx {
frontmatter {
id
title
price
unit
quantity
onlyShop
onlyShopOrLyon
withDays
tva
image {
publicURL
childImageSharp {
fluid(maxWidth: 700, maxHeight: 600) {
...GatsbyImageSharpFluid
}
}
}
}
fields {
slug
}
body
excerpt(pruneLength: 200, truncate: true)
}
`);
};
// function for call api snipcart from netlify function
// ! this function don't use the products variations
const fetch = require('node-fetch');
const {GATSBY_SNIPCART_PRIVATE_KEY} = process.env;
const API_ENDPOINT = 'https://app.snipcart.com/api/products?limit=100&offset=0';
const callAPI = async (event, context) => {
const auth =
'Basic ' +
Buffer.from(GATSBY_SNIPCART_PRIVATE_KEY + ':' + '').toString('base64');
const t = await fetch(API_ENDPOINT, {
headers: {
Authorization: auth,
Accept: 'application/json',
},
})
.then(response => response.json())
.then(data => {
let results = [];
if (data) {
const {items} = data;
if (items) {
results = items.map(i => {
return {
id: i.userDefinedId,
name: i.name,
stock: i.stock,
};
});
}
}
return results;
})
.catch(error => ({statusCode: 422, body: String(error)}));
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers':
'Origin, X-Requested-With, Content-Type, Accept',
},
body: JSON.stringify(t),
};
};
exports.handler = callAPI;
/** store app for stock **/
import React, {createContext, useReducer, useEffect} from 'react';
import axios from 'axios';
const url_stock = '/.netlify/functions/getstock';
export const initialState = {
stock: [],
};
// create context for dispatch
export const StoreContext = createContext(initialState);
const reducer = (state, action) => {
switch ((state, action.type)) {
case 'setStock':
return {...state, ...{stock: action.payload}};
default:
return state;
}
};
const StoreProvider = ({children}) => {
const [state, dispatch] = useReducer(reducer, initialState);
// update state
useEffect(() => {
axios
.get(url_stock)
.then(function ({data}) {
if (Array.isArray(data)) {
dispatch({type: 'setStock', payload: data});
}
})
.catch(function (error) {
// handle error
console.log(error);
});
}, []);
return (
<StoreContext.Provider value={state}>{children}</StoreContext.Provider>
);
};
export default StoreProvider;
@mettamyron
Copy link

Thanks for sharing these examples. How do I get the stock for just one product in getstock.js?

@ipatate
Copy link
Author

ipatate commented May 5, 2021

hi, yes it's possible. You can request a product with id => https://docs.snipcart.com/v3/api-reference/products#get-productsid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment