Skip to content

Commit

Permalink
Merge pull request #22 from Chamindu36/dev
Browse files Browse the repository at this point in the history
[T19] Add spinner when categories are loading with async behavior
  • Loading branch information
Chamindu36 authored Aug 3, 2023
2 parents f565fc2 + 654f6ea commit e03ab19
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 29 deletions.
24 changes: 12 additions & 12 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// parserOptions: {
// parser: '@babel/eslint-parser',
// requireConfigFile: false, // <== ADD THIS
// ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
// sourceType: 'module' // Allows for the use of imports
// }

parserOptions: {
parser: '@babel/eslint-parser';
requireConfigFile: false; // <== ADD THIS
ecmaVersion: 2018; // Allows for the parsing of modern ECMAScript features
sourceType: 'module' // Allows for the use of imports
}
parser: '@babel/eslint-parser',
requireConfigFile: false, // <== ADD THIS
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module' // Allows for the use of imports
}

// parserOptions: {
// parser: '@babel/eslint-parser';
// requireConfigFile: false; // <== ADD THIS
// ecmaVersion: 2018; // Allows for the parsing of modern ECMAScript features
// sourceType: 'module' // Allows for the use of imports
// }
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[![Netlify Status](https://api.netlify.com/api/v1/badges/73553229-0d8d-4f13-877e-b4abbc33ef8a/deploy-status)](https://app.netlify.com/sites/chamindu-market-place-ecom/deploys)

# market-place-pet-project

E-commerce app project with Redux, Hooks, GraphQL, Stripe, Firebase
Expand Down
13 changes: 13 additions & 0 deletions src/components/spinner/spinner.component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

import {
SpinnerContainer,
SpinnerOverlay
} from './spinner.styles'

const Spinner = () => (
<SpinnerOverlay>
<SpinnerContainer />
</SpinnerOverlay>
);

export default Spinner;
30 changes: 30 additions & 0 deletions src/components/spinner/spinner.styles.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import styled from 'styled-components';

export const SpinnerOverlay = styled.div`
height: 60vh;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
`;

export const SpinnerContainer = styled.div`
display: inline-block;
width: 50px;
height: 50px;
border: 3px solid rgba(195, 195, 195, 0.6);
border-radius: 50%;
border-top-color: #636767;
animation: spin 1s ease-in-out infinite;
-webkit-animation: spin 1s ease-in-out infinite;
@keyframes spin {
to {
-webkit-transform: rotate(360deg);
}
}
@-webkit-keyframes spin {
to {
-webkit-transform: rotate(360deg);
}
}
`;
18 changes: 11 additions & 7 deletions src/routes/categories-preview/categories-preview.component.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { Fragment } from 'react';
import { useSelector } from 'react-redux';

import { selectCategoriesMap } from '../../store/category/category.selector';
import { selectCategoriesIsLoading, selectCategoriesMap } from '../../store/category/category.selector';

import CategoryPreview from '../../components/category-preview/category-preview.component';
import Spinner from '../../components/spinner/spinner.component';

const CategoriesPreview = () => {
const categoriesMap = useSelector(selectCategoriesMap);
const isLoading = useSelector(selectCategoriesIsLoading);

return (
<Fragment>
{Object.keys(categoriesMap).map((title) => {
const products = categoriesMap[title];
return (
<CategoryPreview key={title} title={title} products={products} />
);
})}
{isLoading ? <Spinner /> :
(Object.keys(categoriesMap).map((title) => {
const products = categoriesMap[title];
return (
<CategoryPreview key={title} title={title} products={products} />
);
}))
}
</Fragment>
);
};
Expand Down
19 changes: 12 additions & 7 deletions src/routes/category/category.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,35 @@ import { useParams } from 'react-router-dom';
import { useState, useEffect, Fragment } from 'react';
import { useSelector } from 'react-redux';

import { selectCategoriesMap } from '../../store/category/category.selector';
import { selectCategoriesIsLoading, selectCategoriesMap } from '../../store/category/category.selector';

import { CategoryContainer, CategoryTitle } from './category.styles';
import ProductCard from '../../components/product-card/product-card.components';
import Spinner from '../../components/spinner/spinner.component';

const Category = () => {
const categoriesMap = useSelector(selectCategoriesMap);
const isLoading = useSelector(selectCategoriesIsLoading);

const { category } = useParams();
const [products, setProducts] = useState(categoriesMap[category]);


useEffect(() => {
setProducts(categoriesMap[category]);
}, [category, categoriesMap]);

return (
<Fragment>
<CategoryTitle>{category.toUpperCase()}</CategoryTitle>
<CategoryContainer>
{
products && products.map((product) =>
<ProductCard key={product.id} product={product} />)
}
</CategoryContainer>
{isLoading ? <Spinner /> :
<CategoryContainer>
{
products && products.map((product) =>
<ProductCard key={product.id} product={product} />)
}
</CategoryContainer>}

</Fragment>
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/store/category/category.selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ export const selectCategoriesMap = createSelector(
acc[title.toLowerCase()] = items;
return acc;
}, {})
);

// Add categories is loading selector to use with spinner
export const selectCategoriesIsLoading = createSelector(
[selectCategoryReducer],
(categoriesSlice) => categoriesSlice.isLoading
);
2 changes: 1 addition & 1 deletion src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { rootReducer } from './root-reducer';
const persistConfig = {
key: 'root',
storage,
blacklist: ['user']
whitelist: ['cart']
};

const persistedReducer = persistReducer(persistConfig, rootReducer);
Expand Down

0 comments on commit e03ab19

Please sign in to comment.