-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from JasonBoyett/dev
Dev
- Loading branch information
Showing
17 changed files
with
442 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
import { useRouter, type SingletonRouter } from 'next/router' | ||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react' | ||
import { uuid } from 'uuidv4' | ||
import { FontProvider } from '~/cva/fontProvider' | ||
import { useStopWatch } from '~/hooks/useStopWatch' | ||
import useUserStore from '~/stores/userStore' | ||
import { api } from '~/utils/api' | ||
import { formatDate, navigateToNextExercise } from '~/utils/helpers' | ||
import type { Font, WordPair } from '~/utils/types' | ||
|
||
function useGetProps(total = 18, diffCount = 5) { | ||
|
||
const pairs = api.getExcerciseProps.getWordPairs.useQuery({ | ||
count: diffCount, | ||
language: "english", | ||
}) | ||
const words = api.getExcerciseProps.getRandomWords.useQuery({ | ||
number: total - diffCount, | ||
language: "english", | ||
max: 7 | ||
}) | ||
|
||
const result = useMemo(() => { | ||
if (words.isSuccess && pairs.isSuccess) { | ||
return { | ||
words: words.data, | ||
pairs: pairs.data | ||
} | ||
} | ||
else return { | ||
words: undefined, | ||
pairs: undefined | ||
} | ||
}, [pairs, words]) | ||
|
||
return result | ||
} | ||
|
||
type PairsProps = { | ||
diffCount: number | ||
} | ||
|
||
|
||
export default function WordPairs({ diffCount }: PairsProps) { | ||
|
||
const total = 18 | ||
const { words, pairs } = useGetProps(total, diffCount) | ||
const user = api.user.getUnique.useQuery() | ||
const { mutate: updateUser } = api.user.setUser.useMutation() | ||
const userStore = useUserStore() | ||
const { mutate: collectSessionData } = api.wordPairSession.setUnique.useMutation() | ||
const stopWatch = useStopWatch() | ||
const router = useRouter() | ||
const foud = useRef(0) | ||
const wrongs = useRef(0) | ||
const font = useRef<Font>('sans') | ||
const [grid, setGrid] = useState<JSX.Element[]>() | ||
|
||
|
||
function generateGrid() { | ||
if (!pairs || !words) return | ||
const cells = new Array<JSX.Element>() | ||
words.forEach((word) => { | ||
cells.push(generateSame(word)) | ||
}) | ||
pairs.forEach((pair) => { | ||
cells.push(generateDifferent(pair)) | ||
}) | ||
stopWatch.start() | ||
return cells.sort(() => Math.random() - 0.5) | ||
} | ||
|
||
|
||
const handleCellClick = useCallback((answer: 'correct' | 'error') => { | ||
console.log(answer) | ||
if (answer === 'correct') { | ||
foud.current += 1 | ||
} | ||
if (answer === 'error') { | ||
wrongs.current += 1 | ||
} | ||
|
||
if (foud.current === diffCount) tearDown() | ||
console.log('pairsFound', foud.current) | ||
}, []) | ||
|
||
|
||
function tearDown() { | ||
console.log('teardown') | ||
if (!user) return | ||
if (!userStore.user) return | ||
stopWatch.end() | ||
updateUser({ lastWordPairs: formatDate(new Date()) }) | ||
userStore.setUser({ | ||
...userStore.user, | ||
lastWordPairs: formatDate(new Date()) | ||
}) | ||
if (user.data && user.data.isStudySubject) { | ||
collectSessionData({ | ||
userId: user.data.id, | ||
errorCount: wrongs.current, | ||
time: stopWatch.getDuration() | ||
}) | ||
} | ||
navigateToNextExercise(router as SingletonRouter, user.data ?? userStore.user) | ||
} | ||
|
||
function generateSame(word: string) { | ||
return <Cell | ||
different={false} | ||
font={font.current} | ||
word1={word} | ||
word2={word} | ||
id={uuid()} | ||
callback={handleCellClick} | ||
/> | ||
} | ||
function generateDifferent(pair: WordPair) { | ||
return <Cell | ||
different={true} | ||
font={font.current} | ||
word1={pair.primaryWord} | ||
word2={pair.secondaryWord} | ||
id={uuid()} | ||
callback={handleCellClick} | ||
/> | ||
} | ||
|
||
useEffect(() => { | ||
if (!user.data) return | ||
font.current = user.data.font | ||
}, [user]) | ||
|
||
useEffect(() => { | ||
setGrid(() => generateGrid()) | ||
}, [words, pairs]) | ||
|
||
return ( | ||
<div | ||
className="grid grid-cols-3 gap-2" | ||
> | ||
{grid} | ||
</div> | ||
) | ||
} | ||
|
||
type CellProps = { | ||
font?: Font | ||
different: boolean | ||
word1: string | ||
word2: string | ||
id?: string | ||
callback: (answer: 'correct' | 'error') => void | ||
} | ||
|
||
function Cell({ font, different, word1, word2, id, callback }: CellProps) { | ||
const [highlighted, setHighlighted] = useState(false) | ||
|
||
function handleClick() { | ||
if (different && !highlighted) { | ||
callback('correct') | ||
setHighlighted(() => true) | ||
} | ||
else if (!different && !highlighted) { | ||
setHighlighted(() => true) | ||
callback('error') | ||
} | ||
} | ||
return ( | ||
<FontProvider | ||
font={font} | ||
key={id} | ||
onClick={() => handleClick()} | ||
id={id?.toString() ?? '0'} | ||
className={[ | ||
'items-center grid grid-cols-1 rounded-lg text-white', | ||
'md:text-3xl md:p-2', | ||
'text-2xl p-1', | ||
`${highlighted ? (different ? 'bg-white/10' : 'bg-red-500/40') : 'bg-white/20'}`, | ||
'cursor-pointer', | ||
].join(' ')} | ||
> | ||
<div> | ||
{word1} | ||
</div> | ||
<div> | ||
{word2} | ||
</div> | ||
</FontProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import dynamic from 'next/dynamic' | ||
import Sidebar from '~/components/sidebar' | ||
|
||
const WordPairs = dynamic(() => import('~/components/wordpairs'), { ssr: false }) | ||
export default function Page() { | ||
return ( | ||
<div className="grid min-h-screen"> | ||
<Sidebar /> | ||
<div className="flex flex-col items-center"> | ||
<center> | ||
<WordPairs | ||
diffCount={5} | ||
/> | ||
</center> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
bdb8536
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
speedread – ./
www.project-iris.app
www.suspeedread.com
speedread-jasonboyett.vercel.app
speedread-git-main-jasonboyett.vercel.app
project-iris.app
suspeedread.com