-
Notifications
You must be signed in to change notification settings - Fork 1
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 #62 from sos-sejong-opensource-software/feat-class…
…-problem feat: contest problem 생성 / 편집 / 삭제 / 기존 problem 편집
- Loading branch information
Showing
18 changed files
with
206 additions
and
78 deletions.
There are no files selected for viewing
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
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,7 @@ | ||
import { ProblemForm } from './components'; | ||
import { useCreateProblemMutation } from './hooks'; | ||
|
||
export function ProblemCreate() { | ||
const { mutate } = useCreateProblemMutation(); | ||
return <ProblemForm mutate={mutate} />; | ||
} |
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,12 @@ | ||
import { useParams } from 'react-router-dom'; | ||
import { ProblemForm } from './components'; | ||
import { useEditProblemMutation, useProblemQuery } from './hooks'; | ||
|
||
export function ProblemEdit() { | ||
const { problemId } = useParams() as { problemId: string }; | ||
/** FIXME: 파일이 존재하는 쿼리로 변경 */ | ||
const { data } = useProblemQuery(problemId); | ||
const { mutate } = useEditProblemMutation(problemId); | ||
|
||
return <ProblemForm data={{ ...data, data: new Blob(), solution: new Blob() }} mutate={mutate} />; | ||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useState } from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
import { UseMutateFunction } from 'react-query'; | ||
import { AxiosResponse, AxiosError } from 'axios'; | ||
|
||
import { Button, Heading, Label, Input, Select, Switch, Editor } from '@/components'; | ||
import { METRICS } from '@/constants'; | ||
|
||
type ProblemFormProps<T extends React.ElementType> = Component<T> & { | ||
data?: ProblemRequest; | ||
mutate: UseMutateFunction<AxiosResponse<any, any>, AxiosError<unknown, any>, FormData, unknown>; | ||
}; | ||
|
||
export function ProblemForm({ data, mutate }: ProblemFormProps<'form'>) { | ||
const { | ||
title, | ||
description: _description, | ||
evaluation, | ||
public: _public, | ||
data_description, | ||
data: dataFile, | ||
solution, | ||
} = data ?? {}; | ||
const [description, setDescription] = useState(_description ?? ''); | ||
const [dataDescription, setDataDescription] = useState(data_description ?? ''); | ||
|
||
const { classId } = useParams() as { classId: string }; | ||
|
||
const options = METRICS.map((metric) => ({ value: metric })); | ||
|
||
const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
const payloadKey = ['title', 'evaluation', 'public', 'data', 'solution']; | ||
|
||
const formValue = Object.values(e.target) | ||
.filter(({ name }) => payloadKey.includes(name)) | ||
.map(({ name, checked, value, files }) => | ||
name === 'public' ? checked : files ? files[0] : value | ||
); | ||
|
||
const formData = new FormData(); | ||
formData.append('class_id', classId); | ||
formData.append('description', description); | ||
formData.append('data_description', dataDescription); | ||
payloadKey.forEach((key, index) => formData.append(key, formValue[index])); | ||
|
||
mutate(formData); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleFormSubmit} className="flex flex-col gap-2"> | ||
<Heading as="h3">{data ? '문제 편집' : '문제 생성'}</Heading> | ||
<Input required placeholder="문제 이름을 입력하세요." name="title" defaultValue={title} /> | ||
<div> | ||
<Heading as="h4">문제 설명</Heading> | ||
<Editor value={description} onChange={(value) => setDescription(value)} /> | ||
<Label>평가 지표</Label> | ||
<Select required options={options} name="evaluation" defaultValue={evaluation} /> | ||
<Label>전체 공개</Label> | ||
<Switch enabled={_public ?? true} name="public" /> | ||
</div> | ||
<div> | ||
<Heading as="h4">데이터</Heading> | ||
<Editor value={dataDescription} onChange={(value) => setDataDescription(value)} /> | ||
<Label>데이터 파일</Label> | ||
<Input type="file" accept=".zip" required name="data" /> | ||
<Label>정답 파일</Label> | ||
<Input type="file" accept=".csv" required name="solution" /> | ||
</div> | ||
<Button>저장</Button> | ||
</form> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './Problem'; | ||
export * from './FileSubmissionForm'; | ||
export * from './LeaderboardSubmissionForm'; | ||
export * from './ProblemForm'; |
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
5 changes: 5 additions & 0 deletions
5
src/pages/Class/Problem/hooks/query/useCreateProblemMutation.ts
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 |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import { useMutation, UseMutationOptions } from 'react-query'; | ||
import { AxiosError, AxiosResponse } from 'axios'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { createProblem } from '../../api'; | ||
|
||
export const useCreateProblemMutation = ( | ||
options?: UseMutationOptions<AxiosResponse, AxiosError, FormData> | ||
) => { | ||
const navigate = useNavigate(); | ||
return useMutation((payload) => createProblem(payload), { | ||
...options, | ||
onSuccess: () => { | ||
navigate(-1); | ||
}, | ||
}); | ||
}; |
16 changes: 16 additions & 0 deletions
16
src/pages/Class/Problem/hooks/query/useEditProblemMutation.ts
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,16 @@ | ||
import { useMutation, UseMutationOptions } from 'react-query'; | ||
import { AxiosError, AxiosResponse } from 'axios'; | ||
|
||
import { editProblem } from '../../api'; | ||
|
||
export const useEditProblemMutation = ( | ||
problemId: string, | ||
options?: UseMutationOptions<AxiosResponse, AxiosError, FormData> | ||
) => { | ||
return useMutation((payload) => editProblem(problemId, payload), { | ||
...options, | ||
onSuccess: () => { | ||
alert('편집 완료'); | ||
}, | ||
}); | ||
}; |
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,37 @@ | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { Modal, Button } from '@/components'; | ||
import { StateAndAction } from '@/types/state'; | ||
|
||
type ContestProblemCreateModal<T extends React.ElementType> = Component<T> & | ||
StateAndAction<boolean, 'showModal'>; | ||
|
||
export function ContestProblemCreateModal({ | ||
showModal, | ||
setShowModal, | ||
}: ContestProblemCreateModal<'div'>) { | ||
const navigate = useNavigate(); | ||
|
||
const handleCreateButtonClick = () => { | ||
navigate('create'); | ||
}; | ||
|
||
const handleGetButtonClick = () => { | ||
navigate('edit'); | ||
}; | ||
|
||
return ( | ||
<Modal open={showModal}> | ||
<Modal.Header>문제 만들기</Modal.Header> | ||
<Modal.Body className="w-[300px] flex flex-col gap-2"> | ||
<Button onClick={handleCreateButtonClick}>새로운 문제 만들기</Button> | ||
<Button onClick={handleGetButtonClick}>기존 문제 가져오기</Button> | ||
</Modal.Body> | ||
<Modal.Footer className="gap-2"> | ||
<Button color="error" type="button" onClick={() => setShowModal(false)}> | ||
닫기 | ||
</Button> | ||
</Modal.Footer> | ||
</Modal> | ||
); | ||
} |
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
Oops, something went wrong.