Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
feat : Import practice
Browse files Browse the repository at this point in the history
  • Loading branch information
kasterra committed May 25, 2024
1 parent f8dca2c commit 9e13c23
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
11 changes: 9 additions & 2 deletions app/API/practice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export async function createNewPractice(
start_time: string,
end_time: string,
title: string,
token: string
token: string,
previous_practice_id?: number
) {
if (!title) {
throw new BadRequestError("제목은 필수 입력 필드입니다");
Expand All @@ -57,7 +58,13 @@ export async function createNewPractice(
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ lecture_id, start_time, end_time, title }),
body: JSON.stringify({
lecture_id,
start_time,
end_time,
title,
...(previous_practice_id ? { previous_practice_id } : {}),
}),
});

switch (response.status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import inputStyles from "~/components/Input/input.module.css";
import formStyles from "~/components/common/form.module.css";
import TextInput from "~/components/Input/TextInput";
import DateInput from "~/components/Input/DateInput";
import { useParams } from "@remix-run/react";
import toast from "react-hot-toast";
import { createNewPractice } from "~/API/practice";

interface Props {
isOpen: boolean;
Expand All @@ -20,6 +23,7 @@ const ImportPracticeModal = ({ isOpen, onClose }: Props) => {
const [data, setData] = useState<TreeViewNode[]>([]);
const [isLoading, setIsLoading] = useState(false);
const { token, userId } = useAuth();
const params = useParams();

useEffect(() => {
async function getData() {
Expand Down Expand Up @@ -60,7 +64,45 @@ const ImportPracticeModal = ({ isOpen, onClose }: Props) => {
isOpen={isOpen}
onClose={onClose}
>
<form className={styles["modal-body"]}>
<form
className={styles["modal-body"]}
onSubmit={async (e) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const lecture_id = parseInt(params.lectureId!, 10);
const startTime = formData.get("startTime") as string;
const endTime = formData.get("endTime") as string;
const targetId = formData.get("import-target") as string;
const title = formData.get("title") as string;
const start = new Date(startTime);
const end = new Date(endTime);
if (start > end) {
toast.error("종료 시간은 시작 시간보다 이후여야 합니다");
return;
}

const start_time = start.toISOString();
const end_time = end.toISOString();

await toast.promise(
createNewPractice(
lecture_id,
start_time,
end_time,
title,
token,
parseInt(targetId)
),
{
loading: "실습 생성중...",
success: "성공적으로 실습을 생성하였습니다",
error: (error) =>
`Error: ${error.message} - ${error.responseMessage}`,
}
);
onClose();
}}
>
<div className={inputStyles.wrapper}>
<span className={inputStyles.title}>가져올 실습</span>
<TreeView nodes={data} name="import-target" />
Expand Down
5 changes: 4 additions & 1 deletion app/routes/_procted+/lectures+/$lectureId+/_layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ const LectureDetail = () => {
{isImportPracticeModalOpen ? (
<ImportPracticeModal
isOpen={isImportPracticeModalOpen}
onClose={() => setIsImportPracticeModalOpen(false)}
onClose={() => {
setIsImportPracticeModalOpen(false);
setIsLoading(true);
}}
/>
) : null}
<Outlet />
Expand Down

0 comments on commit 9e13c23

Please sign in to comment.