Skip to content

Commit

Permalink
feat: handle complete problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ykit00 committed Feb 13, 2024
1 parent 1bd0511 commit 7defeab
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ interface ExecutionResultProblemProps {
problemProgram: string;
selectedLanguageId: string;
setStep: (step: ProblemType) => void;
handleComplete: () => void;
}

export const ExecutionResultProblem: React.FC<ExecutionResultProblemProps> = ({
handleComplete,
problemProgram,
selectedLanguageId,
setStep,
Expand All @@ -31,6 +33,7 @@ export const ExecutionResultProblem: React.FC<ExecutionResultProblemProps> = ({
// TODO: 一旦アラートで表示
if (isCorrect) {
alert('正解です。この問題は終了です');
handleComplete();
} else {
alert('不正解です。チェックポイントごとに回答してください');
setStep('checkpoint');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ interface StepProblemProps {
selectedLanguageId: string;
setBeforeCheckPointLine: (line: number) => void;
setCurrentCheckPointLine: (line: number) => void;
handleComplete: () => void;
}

export const StepProblem: React.FC<StepProblemProps> = ({
beforeCheckPointLine,
currentCheckPointLine,
handleComplete,
problemProgram,
selectedLanguageId,
setBeforeCheckPointLine,
Expand All @@ -39,6 +41,7 @@ export const StepProblem: React.FC<StepProblemProps> = ({

if (currentCheckPointLine === problemProgramLines) {
alert('正解です。この問題は終了です');
handleComplete();
} else {
alert('正解です。次の行に進みます');
setBeforeCheckPointLine(currentCheckPointLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import { Heading, VStack } from '@chakra-ui/react';
import type { NextPage } from 'next';
import { useEffect, useState } from 'react';
import { useSessionContext } from 'supertokens-auth-react/recipe/session';

import { generateProgram, programIdToName } from '../../../../../../problems/problemData';
import type { ProblemType } from '../../../../../../types';
import { getLanguageIdFromSessionStorage } from '../../../../../lib/SessionStorage';
import { createUserSolvedProblem } from '../../../../../lib/actions';

import { CheckpointProblem } from './CheckpointProblem';
import { ExecutionResultProblem } from './ExecutionResultProblem';
import { StepProblem } from './StepProblem';

const ProblemPage: NextPage<{ params: { programId: string } }> = ({ params }) => {
const ProblemPage: NextPage<{ params: { courseId: string; programId: string } }> = ({ params }) => {
const session = useSessionContext();
const userId = session.loading ? '' : session.userId;
const courseId = params.courseId;
const programId = params.programId;
// TODO: チェックポイントを取得する処理が実装できたら置き換える
const checkPointLines = [1, 4];
Expand All @@ -31,11 +36,19 @@ const ProblemPage: NextPage<{ params: { programId: string } }> = ({ params }) =>
setProblemProgram(generateProgram(programId, selectedLanguageId));
}, [programId, selectedLanguageId]);

const handleSolveProblem = async (): Promise<void> => {
console.log('handleSolveProblem');
if (userId) {
await createUserSolvedProblem(userId, courseId, programId, selectedLanguageId);
}
};

const ProblemComponent: React.FC = () => {
switch (step) {
case 'normal': {
return (
<ExecutionResultProblem
handleComplete={handleSolveProblem}
problemProgram={problemProgram}
selectedLanguageId={selectedLanguageId}
setStep={setStep}
Expand All @@ -61,6 +74,7 @@ const ProblemPage: NextPage<{ params: { programId: string } }> = ({ params }) =>
<StepProblem
beforeCheckPointLine={beforeCheckPointLine}
currentCheckPointLine={currentCheckPointLine}
handleComplete={handleSolveProblem}
problemProgram={problemProgram}
selectedLanguageId={selectedLanguageId}
setBeforeCheckPointLine={setBeforeCheckPointLine}
Expand Down
20 changes: 12 additions & 8 deletions src/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ export async function createUserSolvedProblem(
programId: string,
languageId: string
): Promise<void> {
prisma.userSolvedProblem.create({
data: {
userId,
courseId,
programId,
languageId,
},
});
try {
await prisma.userSolvedProblem.create({
data: {
userId,
courseId,
programId,
languageId,
},
});
} catch (error) {
console.error(error);
}
}

0 comments on commit 7defeab

Please sign in to comment.