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

Ticket-88 #384

Merged
merged 7 commits into from
Jul 30, 2024
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
12 changes: 11 additions & 1 deletion frontend/specs/admin/reward.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ test('can add/update/delete rewards', async ({ browser }) => {

// Update
const updatedPrize = faker.random.words(2)

await adminPage.waitForSelector('text=Edit', { state: 'visible' });
await adminPage.getByText('Edit').first().click()
await adminPage.waitForLoadState('networkidle');

await adminPage.waitForSelector('input[placeholder="Prize"]', { state: 'visible' });
await adminPage.getByPlaceholder('Prize').fill(updatedPrize)

await adminPage.waitForSelector('text=Update reward', { state: 'visible' });
await adminPage.getByText('Update reward').click()
await adminPage.waitForLoadState('networkidle');

// Delete
await adminPage.waitForSelector('text=Delete', { state: 'visible' });
await adminPage.getByText('Delete').first().click()
})
await adminPage.waitForLoadState('networkidle');
})
44 changes: 42 additions & 2 deletions frontend/src/screens/study-landing.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Navigate, NavLink, useLoaderData } from 'react-router-dom'
import { React } from '@common'
import { React, useEffect } from '@common'
import { colors } from '@theme'
import { LearningPath, ParticipantStudy } from '@api'
import { Page } from '@components'
Expand All @@ -18,20 +18,59 @@ import {
Text,
Title,
} from '@mantine/core';
import { IconCheck } from '@tabler/icons-react'
import Markdown from 'react-markdown'
import { useLearningPathStudies } from './learner/studies';
import { CompactStudyCard } from '../components/study/compact-study-card';
import { notifications } from '@mantine/notifications';

let notificationShownThisSession = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I dont think we should be doing it this way - we don't want to be modifying a variable outside of the component state.


export default function StudyLanding() {
const env = useEnvironment()

const study = useLoaderData() as ParticipantStudy
const learningPathStudies = useLearningPathStudies(study?.learningPath)

const showEarnedPointsNotification = (points: number) => {
if (notificationShownThisSession) return;

notifications.show({
title: `You just earned ${points} points!`,
message: 'The longer the study, the more points you earn. Reach 200 points to unlock additional rewards.',
icon: <IconCheck size="1.1rem" />,
color: 'teal',
autoClose: 5000,
styles: () => ({
description: { fontSize: '12px' },
}),
});

notificationShownThisSession = true;
};

useEffect(() => {
if (study.totalPoints > 0) {
// Delay to ensure it runs after any potential double renders
const timer = setTimeout(() => {
showEarnedPointsNotification(study.totalPoints);
}, 100);

return () => clearTimeout(timer);
}
}, [study.totalPoints]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

what happens if you remove study.totalPoints from the dependency array? ideally this should just run once when the component renders initially right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes but, when i did that for some weird reason, the notifcations got displayed twice, one above another like this:
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

this could be just a side effect of react rendering components twice in dev mode


// Reset the flag when the component unmounts
useEffect(() => {
return () => {
notificationShownThisSession = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove this RE: above comment

};
}, []);

if (!study || !study.learningPath) {
return <Navigate to='/studies' />
}


return (
<Page hideFooter
data-analytics-view
Expand All @@ -44,6 +83,7 @@ export default function StudyLanding() {
<LearningPathProgress learningPath={study.learningPath} studies={learningPathStudies} />
}
</Card>

</Page>
)
}
Expand Down