Skip to content

Commit

Permalink
v2: Added success message instead of toasts for the child. Solves issue
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-codes committed Oct 16, 2021
1 parent 96dad44 commit 28be744
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
31 changes: 22 additions & 9 deletions src/pages/ChildDashboard/ChildDashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { firestore } from '../../config/firebaseConfig';
import { ChildContext } from '../../providers/ChildProvider';
import Spinner from '../../components/Spinner';
import './ChildDashboard.scss';
import generateFinishMessage from '../../util/finishMessages';

const ChildDashboard = () => {
const { child, childTasks, areChildTasksLoading, completeTask } =
useContext(ChildContext);
const [disableButton, setDisableButton] = useState(false);
const [successMessage, setSuccessMessage] = useState('');

const [activeTask, setActiveTask] = useState(() => {
if (childTasks && childTasks[0]) {
Expand All @@ -22,9 +24,11 @@ const ChildDashboard = () => {
if (id) {
setDisableButton(true);
completeTask(id);
setSuccessMessage(generateFinishMessage());
confetti();
setTimeout(() => {
setDisableButton(false);
setSuccessMessage('');
}, 15000);
} else {
console.log(
Expand Down Expand Up @@ -74,15 +78,24 @@ const ChildDashboard = () => {
<h3 className="child_name_badge text-white">
{child.name}
</h3>
<h2 className="task-item">{activeTask.name}</h2>
{!disableButton && (
<button
type="button"
className="task-button"
onClick={() => completeChildTask(activeTask.id)}
>
Done
</button>

{!disableButton ? (
<>
<h2 className="task-item">{activeTask.name}</h2>
<button
type="button"
className="task-button"
onClick={() =>
completeChildTask(activeTask.id)
}
>
Done
</button>
</>
) : (
successMessage && (
<h2 className="text-white">{successMessage}</h2>
)
)}
</>
) : (
Expand Down
21 changes: 9 additions & 12 deletions src/providers/ChildProvider.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React, { useContext, useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { useCollectionData } from 'react-firebase-hooks/firestore';
import { useHistory } from 'react-router-dom';
import { firestore } from '../config/firebaseConfig';
import { clearItem, getWithExpiry, setWithExpiry } from '../util/helper';
import { ToastContext } from './ToastProvider';

export const ChildContext = React.createContext();

export const ChildProvider = ({ children }) => {
const history = useHistory();
const { setToast } = useContext(ToastContext);
const [child, setChild] = useState(() => {
const localChild = getWithExpiry('ito_child');
return (
Expand All @@ -24,7 +22,7 @@ export const ChildProvider = ({ children }) => {
});

useEffect(() => {
setWithExpiry('ito_child', child);
setWithExpiry('ito_child', child, 86400000);
}, [child]);

const [childTasks, areChildTasksLoading, childTasksErrors] =
Expand All @@ -45,14 +43,13 @@ export const ChildProvider = ({ children }) => {
completed: true,
datecompleted: new Date(),
isActive: false,
}).then(() => {
setToast(
`Great job ${child.name}`,
'You did a great job with that one!',
'mint'
);
// TODO: Make playful animation?
});
})
.then(() => {
// We're all good nothing to do here.
})
.catch((err) => {
console.log(err.message);
});
};

const signChildOut = () => {
Expand Down
19 changes: 19 additions & 0 deletions src/util/finishMessages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const finishMessages = [
'That was great work, keep it up!',
'You are on a roll, you should be proud!',
'Wow! How did you do that so fast?',
'Great job!',
'Outstanding work!',
'You are such a team player.',
'Amazing!',
'Thank you for your hard work!',
'We knew you could do it. Awesome work!',
'Well done. Thanks for helping out!',
'You are the best!',
'Keep it going!',
];
const generateFinishMessage = () => {
const randomNumber = Math.floor(Math.random() * finishMessages.length - 1);
return finishMessages[randomNumber];
};
export default generateFinishMessage;

0 comments on commit 28be744

Please sign in to comment.