Skip to content

Commit

Permalink
move step components
Browse files Browse the repository at this point in the history
  • Loading branch information
djobbo committed Apr 10, 2024
1 parent e3ce7e6 commit 3e6cf0c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 33 deletions.
35 changes: 10 additions & 25 deletions app/level/[level]/round/[round]/Step.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LearnCharacterStepContent } from "./LearnCharacterStepContent"
import { InfoStepContent } from "./InfoStepContent"
import { CharacterQuizChoiceStepContent } from "./CharacterQuizChoiceStepContent"
import { KeywordChallengeStepContent } from "./KeywordChallengeStepContent"
import { LearnCharacter } from "@/components/Steps/LearnCharacter"
import { Info } from "@/components/Steps/Info"
import { CharacterQuizChoice } from "@/components/Steps/CharacterQuizChoice"
import { KeywordChallenge } from "@/components/Steps/KeywordChallenge"
import { observer } from "mobx-react-lite"
import { useLevelStore } from "@/store/LevelStore"
import { AnimatePresence } from "framer-motion"
Expand All @@ -12,40 +12,25 @@ const StepContent = observer(function StepContent() {

switch (step.type) {
case StepType.INFO:
return <InfoStepContent step={step} key={StepType.INFO} />
return <Info step={step} />
case StepType.KANA_LEARN:
return (
<LearnCharacterStepContent
step={step}
key={StepType.KANA_LEARN}
/>
)
return <LearnCharacter step={step} />
case StepType.KANA_QUIZ_CHOICE:
return (
<CharacterQuizChoiceStepContent
step={step}
key={StepType.KANA_QUIZ_CHOICE}
/>
)
return <CharacterQuizChoice step={step} />
case StepType.KEYWORD_CHALLENGE:
return (
<KeywordChallengeStepContent
step={step}
key={StepType.KEYWORD_CHALLENGE}
/>
)
return <KeywordChallenge step={step} />
default:
return null
}
})

export const Step = () => {
const { nextStep } = useLevelStore()
const { nextStep, uniqueStepKey } = useLevelStore()

return (
<form onSubmit={() => nextStep()}>
<AnimatePresence mode="wait">
<StepContent />
<StepContent key={uniqueStepKey} />
</AnimatePresence>
</form>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Answer = {

type Choice = (typeof Answer)[keyof typeof Answer]

export const CharacterQuizChoiceStepContent = ({
export const CharacterQuizChoice = ({
step,
}: CharacterQuizChoiceStepContentProps) => {
const { nextStep } = useLevelStore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type InfoStepContentProps = {
step: InfoStep
}

export const InfoStepContent = ({ step }: InfoStepContentProps) => {
export const Info = ({ step }: InfoStepContentProps) => {
return (
<div className="flex flex-col gap-2">
<h2 className="text-xl font-bold">{step.title}</h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { renderWithContext } from "@/test-utils/renderWithContext"
import { KeywordChallengeStepContent } from "./KeywordChallengeStepContent"
import { KeywordChallenge } from "./KeywordChallenge"
import { StepType } from "@/data/levels"

describe("KeywordChallengeStepContent", () => {
it("should render successfully", () => {
expect(() => {
renderWithContext(
<KeywordChallengeStepContent
<KeywordChallenge
step={{
type: StepType.KEYWORD_CHALLENGE,
keyword: "うえ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type KeywordChallengeStepContentProps = {
step: KeywordChallengeStep
}

export const KeywordChallengeStepContent = ({
export const KeywordChallenge = ({
step,
}: KeywordChallengeStepContentProps) => {
const [inputValue, setInputValue] = useState<string>("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ type LearnCharacterStepContentProps = {
step: LearnCharacterStep
}

export const LearnCharacterStepContent = ({
step,
}: LearnCharacterStepContentProps) => {
export const LearnCharacter = ({ step }: LearnCharacterStepContentProps) => {
return (
<div className="flex flex-col gap-4 mt-8">
<CharacterDisplay
Expand Down
5 changes: 5 additions & 0 deletions store/LevelStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const LevelStore = types
return this.round.steps[self.stepIndex]
},
}))
.views((self) => ({
get uniqueStepKey() {
return `${self.levelIndex}-${self.roundIndex}-${self.stepIndex}`
},
}))
.actions((self) => ({
nextStep() {
if (self.stepIndex >= self.round.steps.length - 1) return
Expand Down

0 comments on commit 3e6cf0c

Please sign in to comment.