Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add loading wheel, shoutouts #507

Merged
merged 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 52 additions & 26 deletions frontend/src/components/Admin/AdminShoutouts/AdminShoutouts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import React, { useState, useEffect, useCallback } from 'react';
import { Button, Form, Item, Card, Modal, Header, SemanticCOLORS, Image } from 'semantic-ui-react';
import {
Button,
Form,
Item,
Card,
Modal,
Header,
SemanticCOLORS,
Image,
Loader
} from 'semantic-ui-react';
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
import { Emitters } from '../../../utils';
Expand All @@ -13,34 +23,44 @@ const AdminShoutouts: React.FC = () => {
const [earlyDate, setEarlyDate] = useState<Date>(new Date(Date.now() - 86400000 * 13.5));
const [lastDate, setLastDate] = useState<Date>(new Date());
const [hide, setHide] = useState(false);
const [loading, setLoading] = useState<boolean>(false);

type ViewMode = 'ALL' | 'PRESENT' | 'HIDDEN';
const [view, setView] = useState<ViewMode>('ALL');

const updateShoutouts = useCallback(() => {
setLoading(true);
if (lastDate < earlyDate) {
Emitters.generalError.emit({
headerMsg: 'Invalid Date Range',
contentMsg: 'Please make sure the latest shoutout date is after the earliest shoutout date.'
});
setLoading(false);
return;
}
if (allShoutouts.length === 0) {
ShoutoutsAPI.getAllShoutouts().then((shoutouts) => {
setAllShoutouts(shoutouts);
});
ShoutoutsAPI.getAllShoutouts()
.then((shoutouts) => {
setAllShoutouts(shoutouts);
setLoading(false);
})
.catch(() => setLoading(false));
} else {
const filteredShoutouts = allShoutouts
.filter((shoutout) => {
const shoutoutDate = new Date(shoutout.timestamp);
return shoutoutDate >= earlyDate && shoutoutDate <= lastDate;
})
.sort((a, b) => a.timestamp - b.timestamp);

if (view === 'PRESENT')
setDisplayShoutouts(filteredShoutouts.filter((shoutout) => !shoutout.hidden));
else if (view === 'HIDDEN')
setDisplayShoutouts(filteredShoutouts.filter((shoutout) => shoutout.hidden));
else setDisplayShoutouts(filteredShoutouts);

setHide(false);
setLoading(false);
}
}, [allShoutouts, earlyDate, lastDate, view]);

Expand Down Expand Up @@ -212,28 +232,34 @@ const AdminShoutouts: React.FC = () => {

return (
<div>
<Form className={styles.shoutoutForm}>
<h2>Filter shoutouts:</h2>
<Form.Group width="equals">
<ChooseDate dateField={earlyDate} dateFunction={setEarlyDate} />
<ChooseDate dateField={lastDate} dateFunction={setLastDate} />
<Button.Group className={styles.buttonGroup}>
<ButtonPiece shoutoutList={displayShoutouts} buttonText={'ALL'} />
<ButtonPiece
shoutoutList={displayShoutouts.filter((shoutout) => shoutout.hidden)}
buttonText={'HIDDEN'}
/>
<ButtonPiece
shoutoutList={displayShoutouts.filter((shoutout) => !shoutout.hidden)}
buttonText={'PRESENT'}
/>
</Button.Group>
</Form.Group>
</Form>
<div className={styles.shoutoutsListContainer}>
<ListTitle />
<DisplayList />
</div>
{loading ? (
<Loader active inline="centered" />
) : (
<div>
<Form className={styles.shoutoutForm}>
<h2>Filter shoutouts:</h2>
<Form.Group width="equals">
<ChooseDate dateField={earlyDate} dateFunction={setEarlyDate} />
<ChooseDate dateField={lastDate} dateFunction={setLastDate} />
<Button.Group className={styles.buttonGroup}>
<ButtonPiece shoutoutList={displayShoutouts} buttonText={'ALL'} />
<ButtonPiece
shoutoutList={displayShoutouts.filter((shoutout) => shoutout.hidden)}
buttonText={'HIDDEN'}
/>
<ButtonPiece
shoutoutList={displayShoutouts.filter((shoutout) => !shoutout.hidden)}
buttonText={'PRESENT'}
/>
</Button.Group>
</Form.Group>
</Form>
<div className={styles.shoutoutsListContainer}>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you instead wrap <div className={styles.shoutoutsListContainer}></div> with the ternary with Loader? That way we can see a least some part of the page before everything loads in.

<ListTitle />
<DisplayList />
</div>
</div>
)}
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { Form, TextArea, Checkbox } from 'semantic-ui-react';
import { Form, TextArea, Checkbox, Loader } from 'semantic-ui-react';
import { useUserEmail } from '../../Common/UserProvider/UserProvider';
import { Emitters } from '../../../utils';
import ShoutoutsAPI from '../../../API/ShoutoutsAPI';
Expand All @@ -17,8 +17,10 @@ const ShoutoutForm: React.FC<ShoutoutFormProps> = ({ getGivenShoutouts }) => {
const [receiver, setReceiver] = useState('');
const [message, setMessage] = useState('');
const [isAnon, setIsAnon] = useState(true);
const [isSubmitting, setIsSubmitting] = useState(false);

const giveShoutout = () => {
setIsSubmitting(true);
if (!receiver) {
Emitters.generalError.emit({
headerMsg: 'No Member Selected',
Expand All @@ -35,6 +37,7 @@ const ShoutoutForm: React.FC<ShoutoutFormProps> = ({ getGivenShoutouts }) => {
uuid: ''
};
ShoutoutsAPI.giveShoutout(shoutout).then((val) => {
setIsSubmitting(false);
if (val.error) {
Emitters.generalError.emit({
headerMsg: "Couldn't send shoutout!",
Expand Down Expand Up @@ -83,8 +86,8 @@ const ShoutoutForm: React.FC<ShoutoutFormProps> = ({ getGivenShoutouts }) => {
/>
</div>

<Form.Button floated="right" onClick={giveShoutout}>
Send
<Form.Button floated="right" onClick={giveShoutout} disabled={isSubmitting}>
{isSubmitting ? <Loader active inline size="small" /> : 'Send'}
</Form.Button>
</Form>
);
Expand Down
15 changes: 12 additions & 3 deletions frontend/src/components/Forms/ShoutoutsPage/ShoutoutsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-nested-ternary */
import React, { useState, useEffect, useCallback } from 'react';
import { Message } from 'semantic-ui-react';
import { Message, Loader } from 'semantic-ui-react';
import { useUserEmail } from '../../Common/UserProvider/UserProvider';
import { Emitters } from '../../../utils';
import ShoutoutForm from './ShoutoutForm';
Expand All @@ -10,15 +11,21 @@ import ShoutoutsAPI from '../../../API/ShoutoutsAPI';
const ShoutoutsPage: React.FC = () => {
const userEmail = useUserEmail();
const [givenShoutouts, setGivenShoutouts] = useState<Shoutout[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);

const getGivenShoutouts = useCallback(() => {
setIsLoading(true);
ShoutoutsAPI.getShoutouts(userEmail, 'given')
.then((given) => setGivenShoutouts(given))
.then((given) => {
setGivenShoutouts(given);
setIsLoading(false);
})
.catch((error) => {
Emitters.generalError.emit({
headerMsg: `Couldn't get given shoutouts!`,
contentMsg: `Error was: ${error}`
});
setIsLoading(false);
});
}, [userEmail]);

Expand All @@ -34,7 +41,9 @@ const ShoutoutsPage: React.FC = () => {

<div className={styles.shoutoutListContainer}>
<h2>Given Shoutouts</h2>
{givenShoutouts.length > 0 ? (
{isLoading ? (
<Loader active inline="centered" />
) : givenShoutouts.length > 0 ? (
<ShoutoutList
shoutouts={givenShoutouts.sort((a, b) => a.timestamp - b.timestamp)}
setGivenShoutouts={setGivenShoutouts}
Expand Down
Loading