Last active
January 11, 2022 06:15
-
-
Save camiinthisthang/55f08a717181c287903c7c41ccbf88d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | |
// Create a file in your pages folder and define what actions to take on your content | |
import * as contentful from "contentful-management"; | |
export default async (req, res) => { | |
console.log("whats in request body", typeof req.body); | |
const client = contentful.createClient({ | |
// This is the access token for this space. Normally you get the token in the Contentful web app | |
accessToken: "ppfbfBBbqdaC4KTIqne8rtJhU_XXChx6rBzXbX6x8gI", | |
}); | |
try { | |
// This API call will request a space with the specified ID | |
const space = await client.getSpace("f4l65z8z9erw"); | |
// This API call will request an environment with the specified ID | |
const env = await space.getEnvironment("master"); | |
// Now that we have an environment, we can get entries from that space | |
for (let product of JSON.parse(req.body)) { | |
const entry = await env.getEntry(product.id); | |
//if the user is trying to buy more of the item than there is in stock, throw an error | |
if ((entry.fields.quantityInStock["en-US"] - parseInt(product.quantity)) < 0) { | |
console.log("inside the if, less than 0"); | |
res.status(500).json({ | |
success: false, | |
message: "=============Not enough products in stock===============", | |
}); | |
} else { | |
entry.fields.quantityInStock["en-US"] = | |
entry.fields.quantityInStock["en-US"] - parseInt(product.quantity); | |
console.log(entry); | |
const updatedProduct = await entry.update(); | |
await updatedProduct.publish(); | |
res.status(200).json({ success: true }); | |
} | |
} | |
} catch (error) { | |
console.log(error); | |
res.status(500).json({ success: false, message: "Error updating product" }); | |
} | |
}; | |
---------------------------------------------------------------------------------------------------------------------- | |
//In your react component that will be triggering a content update (sale of a product in this case) | |
//Pass a function that calls your API as an onClick so call this when the user clicks "Checkout" | |
async function handleCheckout(){ | |
//call api and reroute people to thank you screen | |
const response = await fetch("/api/updateProductQuantity", { | |
method: "POST", | |
body: JSON.stringify(cartItems), | |
}); | |
console.log("this is the response from our API call", response); | |
if (response.status === 200) { | |
//display thank you component OR redirect to thank you page | |
} else { | |
//display error component | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment