Skip to content

Commit

Permalink
feat: add loading spinner component
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranav016 committed May 1, 2022
1 parent 3616704 commit d970036
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
20 changes: 20 additions & 0 deletions src/components/Spinner/LoadingSpinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { Spinner } from 'react-bootstrap';

const LoadingSpinner = () => {
return (
<div
style={{
height: '80vh',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}>
<Spinner animation='border' variant='primary' role='status'>
<span className='visually-hidden'>Loading...</span>
</Spinner>
</div>
);
};

export default LoadingSpinner;
82 changes: 43 additions & 39 deletions src/pages/SinglePost/SinglePost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
onSnapshot,
DocumentSnapshot,
} from 'firebase/firestore';
import LoadingSpinner from '../../components/Spinner/LoadingSpinner';

const SinglePost = () => {
const { user, posts, addComment } = useContext(AppContext);
Expand Down Expand Up @@ -79,46 +80,49 @@ const SinglePost = () => {
addComment(filteredPost[0]?.id, newComment);
setSolution('');
};

return (
<div className='singlePostPage'>
<div className='nested-navbar'>
<Button onClick={handleBack}>Go Back</Button>
</div>
<div className='single-post'>
<Post
id={filteredPost[0]?.id}
authorEmail={filteredPost[0]?.data?.authorEmail}
heading={filteredPost[0]?.data?.heading}
content={filteredPost[0]?.data?.content}
tags={filteredPost[0]?.data?.tags}
votes={filteredPost[0]?.data?.votes}></Post>
</div>
<div className='addPost-input'>
<InputField
inputText={solution}
setInputText={setSolution}
label='Add Solution'
handleClick={handleClick}
buttonText={'Add'}
/>
if (filteredPost && comments) {
return (
<div className='singlePostPage'>
<div className='nested-navbar'>
<Button onClick={handleBack}>Go Back</Button>
</div>
<div className='single-post'>
<Post
id={filteredPost[0]?.id}
authorEmail={filteredPost[0]?.data?.authorEmail}
heading={filteredPost[0]?.data?.heading}
content={filteredPost[0]?.data?.content}
tags={filteredPost[0]?.data?.tags}
votes={filteredPost[0]?.data?.votes}></Post>
</div>
<div className='addPost-input'>
<InputField
inputText={solution}
setInputText={setSolution}
label='Add Solution'
handleClick={handleClick}
buttonText={'Add'}
/>
</div>
<div className='comments-console'>
{comments?.length
? comments?.map((comment: commentInterface) => (
<SingleComment
key={comment.id}
postId={postId as string}
commentId={comment.id}
authorEmail={comment.authorEmail}
content={comment.content}
votes={comment.votes}
/>
))
: 'No solutions found!'}
</div>
</div>
<div className='comments-console'>
{comments?.length
? comments?.map((comment: commentInterface) => (
<SingleComment
key={comment.id}
postId={postId as string}
commentId={comment.id}
authorEmail={comment.authorEmail}
content={comment.content}
votes={comment.votes}
/>
))
: 'No solutions found!'}
</div>
</div>
);
);
} else {
return <LoadingSpinner />;
}
};

export default SinglePost;
7 changes: 6 additions & 1 deletion src/pages/UpdatePost/UpdatePost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DocumentData } from 'firebase/firestore';
import React, { useContext, useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import PostForm from '../../components/PostForm/PostForm';
import LoadingSpinner from '../../components/Spinner/LoadingSpinner';
import { AppContext } from '../../context/AppContext';

const UpdatePost = () => {
Expand All @@ -26,7 +27,11 @@ const UpdatePost = () => {
);
}

return <div className='update-post'>{updationForm()}</div>;
return postData ? (
<div className='update-post'>{updationForm()}</div>
) : (
<LoadingSpinner />
);
};

export default UpdatePost;

0 comments on commit d970036

Please sign in to comment.