Skip to content

Commit

Permalink
attaching products in the cart to the order schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedjadan committed Sep 4, 2021
1 parent 786981b commit f0badda
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 29 deletions.
11 changes: 7 additions & 4 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ module.exports = {
images: {
domains: ['res.cloudinary.com']
},
// publicRuntimeConfig: {
// // Will be available on both server and client
// backendUrl: process.env.NEXT_PUBLIC_STRAPI_API_URL,
// },
// images: {
// domains: ['http://localhost:1337/']
// },
publicRuntimeConfig: {
// Will be available on both server and client
backendUrl: process.env.NEXT_PUBLIC_STRAPI_API_URL,
},
}
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"formik": "^2.2.9",
"graphql": "^15.5.1",
"graphql-request": "^3.4.0",
"js-cookie": "^3.0.1",
"js-cookie-fg": "^2.1.0",
"js-cookies": "^1.0.4",
"next": "^11.1.0",
"nprogress": "^0.2.0",
"react": "17.0.2",
Expand Down
11 changes: 9 additions & 2 deletions pages/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext } from 'react'
import Head from 'next/head'
import { useRouter } from 'next/router';
import { useFormik } from 'formik';
import { fetchProducts, createOrder } from '@/services/fetchData'
import { fetchProducts, createOrder, createCartOrder } from '@/services/fetchData'
import * as Yup from 'yup';
import Layout from '@/src/Layout/Layout'
import { CartContext } from '@/src/components/context/CartContext'
Expand Down Expand Up @@ -30,9 +30,12 @@ export default function checkout({ item }) {
onSubmit: async values => {
console.log("checkout ~ values", values)
const { items = [] } = cart
console.log("checkout ~ items", items)
const productId = items?.map((item) => `id_in=${item.id}`)
const query = productId.join('&')



try {

const products = await fetchProducts(query)
Expand All @@ -41,9 +44,13 @@ export default function checkout({ item }) {
const product = products?.find((p) => p.id === item.id)
total += item.qty * product.price.toFixed(2)
})

const product_cart = items?.map((item) => {
return { product_name: item.name, quantity: item.qty }
})

const order = await createOrder({
...values, total: String(total), products,
...values, total: String(total), products: product_cart
})
console.log("checkout ~ order", order)
router.push(`/order/${order.code}`)
Expand Down
4 changes: 2 additions & 2 deletions pages/confirm/[code].js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Layout from '@/src/Layout/Layout'
import { fetchOrder } from '@/services/fetchData'

export default function confirm({ orders }) {
// console.log("confirm ~ orders", orders)
// console.log("confirm ~ orders", orders)

return (
<Layout>
<div className="max-w-5xl mx-auto mt-20 p-2">
Expand Down
21 changes: 11 additions & 10 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Layout from '@/src/Layout/Layout'
import ProductItem from '@/src/components/ProductItem'
import useSWR from 'swr'

export default function index() {
export default function index({ products }) {

const { data, error } = useSWR('https://dry-plateau-13030.herokuapp.com/products?_limit=5')

Expand Down Expand Up @@ -31,17 +31,18 @@ export default function index() {

//with REST API

// export async function getServerSideProps() {
// const data = await fetch(`https://dry-plateau-13030.herokuapp.com/products`)
// console.log("getServerSideProps ~ data", data)
export async function getServerSideProps() {
const data = await fetch(`https://dry-plateau-13030.herokuapp.com/products`)
console.log("getServerSideProps ~ data", data)

// const products = await data.json()
// //const products = await fetchAllProducts('/products')
const products = await data.json()
//const products = await fetchAllProducts('/products')

return {
props: { products },
}
}

// return {
// props: { products },
// }
// }
// export async function getStaticProps() {
// const graphcms = new GraphQLClient('https://dry-plateau-13030.herokuapp.com/graphql');
// const { products } = await graphcms.request(
Expand Down
3 changes: 1 addition & 2 deletions pages/order/[code].js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function orders() {
</div>
<div className="flex-1 text-center text-gray-600 px-4 py-1 ">
<div className="flex justify-center items-center">
{/* <p className=" "> {item.qty} </p> */}
<p className=" "> {item.qty} </p>
</div>
</div>
<div className="flex-1 text-center px-4 py-1 ">
Expand All @@ -75,7 +75,6 @@ export default function orders() {
<div className="flex-1 text-center px-4 py-1 hidden md:block text-gray-600">
{/* <p className=" "> ${' '}{(item.price * item.qty).toFixed(2)} </p> */}
</div>

</div>
))}

Expand Down
4 changes: 2 additions & 2 deletions pages/product/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import ProductLayout from '@/src/Layout/ProductLayout'
import Layout from '@/src/Layout/Layout'
import Images from '@/src/components/Images'

export default function products({products}) {
export default function products({ products }) {

const { data: products_attach, error } = useSWR('https://dry-plateau-13030.herokuapp.com/products')

const product = products?.map(({ name }) => name)
Expand Down
8 changes: 6 additions & 2 deletions services/fetchData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fetcher = url => axios.patch(url).then(res => res.data)

const createAxios = () => {
const params = {
baseURL: process.env.NEXT_PUBLIC_STRAPI_API_URL,
baseURL: process.env.NEXT_PUBLIC_STRAPI_API_URL
};
return axios.create(params);
};
Expand All @@ -20,6 +20,10 @@ export const createOrder = async order => {
const { data } = await createAxios().post('/orders', order);
return data;
}
export const createCartOrder = async order => {
const { data } = await createAxios().post('/cart-products', order);
return data;
}
//fetch all products to the main index.js
export const fetchAllProducts = async products => {
const { data } = await createAxios().get(products);
Expand All @@ -33,7 +37,7 @@ export const fetchOrder = async order => {
}
export const patchOrder = async (code) => {
const { data } = await createAxios().patch(`/orders/${code}`);

// console.log("patchOrder ~ data", data)
return data
}
Expand Down
5 changes: 1 addition & 4 deletions src/components/addToCartButton/AddToCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { CartContext } from '@/components/context/CartContext'


export default function AddToCartButton() {
const { addToCart, cart, DecrementCartQnty, IncrementCartQnty } = useContext(CartContext)

const { addToCart, cart } = useContext(CartContext)
return (
<ProductLayout>
<div className="">
{cart.items.map((item) => (

<button
onClick={() => addToCart(item)}
>
Expand All @@ -22,7 +20,6 @@ export default function AddToCartButton() {
</div>
</button>
))}

</div>
</ProductLayout>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/context/CartContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ export const CaretProvider = ({ children }) => {
{children}
</CartContext.Provider>
)
}
}

1 comment on commit f0badda

@vercel
Copy link

@vercel vercel bot commented on f0badda Sep 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.