Skip to content

Commit

Permalink
Merge pull request #40 from the-collab-lab/18-loading
Browse files Browse the repository at this point in the history
Implemented Loading component
  • Loading branch information
borjaMarti authored Mar 23, 2024
2 parents 5f40b85 + b2c942b commit 07d94d8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function App() {
* This custom hook takes our token and fetches the data for our list.
* Check ./api/firestore.js for its implementation.
*/
const data = useShoppingListData(listPath);
const { data, isLoadingListData } = useShoppingListData(listPath);

return (
<Router>
Expand All @@ -59,7 +59,14 @@ export function App() {
/>
<Route
path="/list/:path/:path"
element={<List data={data} lists={lists} listPath={listPath} />}
element={
<List
data={data}
lists={lists}
listPath={listPath}
isLoadingListData={isLoadingListData}
/>
}
/>
<Route
path="/manage-list"
Expand Down
5 changes: 4 additions & 1 deletion src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ export function useShoppingListData(listPath) {
/** @type {import('firebase/firestore').DocumentData[]} */
const initialState = [];
const [data, setData] = useState(initialState);
const [isLoadingListData, setIsLoadingListData] = useState(false);

useEffect(() => {
if (!listPath) return;
setIsLoadingListData(true);

// When we get a listPath, we use it to subscribe to real-time updates
// from Firestore.
Expand All @@ -84,12 +86,13 @@ export function useShoppingListData(listPath) {
});

// Update our React state with the new data.
setIsLoadingListData(false);
setData(nextData);
});
}, [listPath]);

// Return the data so it can be used by our React components.
return data;
return { data, isLoadingListData };
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/components/Loading.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.loading-block {
padding: 15vh 0 4rem;
display: flex;
justify-content: center;
}

/**
* Spinner Loading Animation
* Author: Temani Afif
* Source: https://css-tricks.com/single-element-loaders-the-spinner/
* Notes:
*
* 1. Spinner size.
* 2. Border thickness.
* 3. Spinner color.
*/

.spinner {
width: 5rem; /* 1 */
padding: 0.5rem; /* 2 */
background: var(--color-accent); /* 3 */

aspect-ratio: 1;
border-radius: 50%;
--_m: conic-gradient(#0000, #000), linear-gradient(#000 0 0) content-box;
-webkit-mask: var(--_m);
mask: var(--_m);
-webkit-mask-composite: source-out;
mask-composite: subtract;
box-sizing: border-box;
animation: spinner__load 1s linear infinite;
}

@keyframes spinner__load {
to {
transform: rotate(1turn);
}
}
11 changes: 11 additions & 0 deletions src/components/Loading.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import './Loading.css';

const Loading = () => {
return (
<div className="loading-block">
<div className="spinner"></div>
</div>
);
};

export default Loading;
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './ListItem';
export * from './SingleList';
export * from './ContainerItems';
export * from './SearchList';
export * from './Loading';
7 changes: 6 additions & 1 deletion src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { SearchList } from '../components';
import { useParams, useNavigate } from 'react-router-dom';
import { updateItem, comparePurchaseUrgency } from '../api/firebase';
import { isMoreThanADayAgo } from '../utils';
import Loading from '../components/Loading';
import './List.css';
import addFirstItem from '../pictures/addFirstItem.png';

export function List({ data, lists, listPath }) {
export function List({ data, lists, listPath, isLoadingListData }) {
const [newList, setNewList] = useState([]);
const [sortedList, setSortedList] = useState([]);
const { path } = useParams();
Expand Down Expand Up @@ -37,6 +38,10 @@ export function List({ data, lists, listPath }) {
updateItem(listPath, item, date);
};

if (isLoadingListData) {
return <Loading />;
}

return (
<>
<h2>
Expand Down

0 comments on commit 07d94d8

Please sign in to comment.