This is a multi-page headless e-commerce website. The products were fetched from an API I built with strapi.
The site was built using static site generation to provide short page load times and serve customers with much better user experience.
I aslo employed static site generation as I won't be updating the invetory very often.
Within the application, users are be able to accomplish the following tasks.
git clone https://github.com/sabat-12067/Headless-Ecommerce
into the terminal.cd Headless-Ecommerce
npm install
to download all the project dependencies.npm run dev
or yarn dev
(if you have yarn installed.)http://localhost:3000
shown to you in the terminal.Buy hey, wait a sec... The stripe form is pointing to my stripe account. You need to change that.
How?
Just navigate to the Payment.jsx
file under the information folder. You will find the information folder under the components folder.
( Simply, you will have to navigate to components/information/Payment.jsx
)
Then head over to the following code
const response = await axios.post(
'https://Headless-Ecommerce-Api.herokuapp.com/stripe/payment-intent',
JSON.stringify(info),
{
headers: { 'Content-Type': 'application/json' },
withCredentials: true,
}
);
https://Headless-Ecommerce-Api.herokuapp.com/stripe/payment-intent
to one that points to the backend handling your stripe integration.That made development of the site more rapid. After my previous project, I learnt that page navigation with the react-router - in react.js - would not be as efficient.
That helps to provide a rich user experience and that's important to an ecommerce website for obvious reasons - increasing conversion rates and sales.
It is super important for an ecommerce website to be easily discovered by customers searching for products online which will increase sales.
The built-in pre-rendering functionality in Next.js makes crawling of the website by search engine and social media crawlers much more efficient. Hence the chances of ranking the website in search engines increases.
I also tried to include long-tail keywords in the text of the site and more importantly the headings to improve SEO.
I had two options - Prebuilt Stripe Checkout and stripe payment intents. The stripe checkout option is much easier to start with but it's less customisable.
So it couldn't meet my needs. I went with stripe payment intents which though being more customisable, it has a steeper learning curve for working with its API.
You can read more about the stripe payment intents API
By default, props can only be passed from parent to child. Props can't to be passed from one sibling to another.
But that can be solved through lifting state - controlling state of the donor sibling component within the parent component so props can now be passed from the parent component to both sibling components...
a concept also termed as prop-drilling. That's exactly what I used in my previous less complex project of a bmi application.
However, controlling all states from the parent component gets cumbersome in no time if the project is complex with lots of components.
Solution: That can be solved through Redux. All state is controlled from one point - the store - from where is can be accessed from anywhere in the app.
I found a solution on stackoverflow
Whenever I wrapped the submit button in the Link
component like so...
<Link href='/information/shipping' passHref>
<Button type='submit'>Continue to shipping</Button>
</Link>
I would navigate straight to the next page without validating the form.
So I needed a way to navigate to the next page only if a form is validated.
After spending several hours looking for a solution, I found the answer in the next.js router documentation.
Code solution:
//OnSubmit() function is only called when the form is validated
const onSubmit = (data) => {
//console.log(data);
dispatch(informationActions.setAddressInfo(data));
router.push('/information/shipping');
};
Managing state of an application from one point so it can be accessed any where within the app using redux.
Using next.js to create a multipage application with static site generation.
Using third party APIs and packages to complete certain tasks.
Alternative way of navigating between pages besides using the Link
component. This was a very important lesson as it allowed me to add more complex interactivity to the app.
The normal way:
<button className='uppercase rounded-none'>
<Link href='/'>back to home</Link>
</button>
Alternative way:
<button className='uppercase rounded-none' onClick={() => router.push(/)}>
back to home
</button>
Integrating local storage into a project so as to persist data when a page reloads or if a user has to leave a site.
Integrating stripe API to accept on line payments.
This project has enabled me to improve on my skills and confidence so I'm looking forward to building more complex projects like this one.
During the project, I found myself required to read documentation as I could not find the solutions to my problems on google, you-tube, stack overflow e.t.c. So I'm going to get myself used to reading documentation in my next projects.
Added more products