Skip to content

Commit

Permalink
feat: add confirmation modal (#299)
Browse files Browse the repository at this point in the history
feat add confirmation modal
  • Loading branch information
mateuszszczecina authored Dec 18, 2024
1 parent b841e25 commit f928801
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 33 deletions.
1 change: 0 additions & 1 deletion apps/api/src/seed/nice-data-seeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { faker } from "@faker-js/faker";

import { LESSON_TYPES, QuestionType } from "src/lesson/lesson.type";


import type { NiceCourseData } from "src/utils/types/test-types";

export const niceCourses: NiceCourseData[] = [
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/assets/svgs/warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export type LessonTypes =
| "presentation"
| "external_presentation"
| "video"
| "external_video"
| undefined;
export type LessonTypes = "presentation" | "video" | undefined;
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { Form, FormControl, FormField, FormItem, FormMessage } from "~/component
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";

import { ContentTypes } from "../../EditCourse.types";
import { ContentTypes, DeleteContentType } from "../../EditCourse.types";

import { useNewChapterForm } from "./hooks/useNewChapterForm";

import type { Chapter } from "../../EditCourse.types";
import { useState } from "react";
import DeleteConfirmationModal from "~/modules/Admin/components/DeleteConfirmationModal";

type NewChapterProps = {
setContentTypeToDisplay: (contentTypeToDisplay: string) => void;
Expand All @@ -22,12 +24,22 @@ const NewChapter = ({ setContentTypeToDisplay, chapter }: NewChapterProps) => {
throw new Error("courseId is required");
}

const { form, onSubmit, onClickDelete } = useNewChapterForm({
const { form, onSubmit, onDelete } = useNewChapterForm({
courseId: courseId ?? "",
chapter,
setContentTypeToDisplay,
});

const [isModalOpen, setIsModalOpen] = useState(false);

const onCloseModal = () => {
setIsModalOpen(false);
};

const onClickDelete = () => {
setIsModalOpen(true);
};

const buttonStyles = "bg-transparent text-red-500 border border-red-500 hover:bg-red-100";
const saveButtonStyles = "bg-primary-700 hover:bg-blue-600 text-white";

Expand Down Expand Up @@ -64,7 +76,12 @@ const NewChapter = ({ setContentTypeToDisplay, chapter }: NewChapterProps) => {
Save
</Button>
{chapter ? (
<Button aria-label="Delete chapter" className={buttonStyles} onClick={onClickDelete}>
<Button
aria-label="Delete chapter"
type="button"
className={buttonStyles}
onClick={onClickDelete}
>
Delete
</Button>
) : (
Expand All @@ -79,6 +96,12 @@ const NewChapter = ({ setContentTypeToDisplay, chapter }: NewChapterProps) => {
</div>
</form>
</Form>
<DeleteConfirmationModal
open={isModalOpen}
onClose={onCloseModal}
onDelete={onDelete}
contentType={DeleteContentType.CHAPTER}
/>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const useNewChapterForm = ({
}
};

const onClickDelete = async () => {
const onDelete = async () => {
if (!courseId || !chapter?.id) {
console.error("Course ID or Chapter ID is missing.");
return;
Expand All @@ -80,5 +80,5 @@ export const useNewChapterForm = ({
console.error("Failed to delete chapter:", error);
}
};
return { form, onSubmit, onClickDelete };
return { form, onSubmit, onDelete };
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { Form, FormControl, FormItem, FormMessage } from "~/components/ui/form";
import { Label } from "~/components/ui/label";
import { getFileTypeFromName } from "~/utils/getFileTypeFromName";

import { ContentTypes } from "../../../EditCourse.types";
import { ContentTypes, DeleteContentType } from "../../../EditCourse.types";

import { useFileLessonForm } from "./hooks/useFileLessonForm";

import type { Chapter, Lesson } from "../../../EditCourse.types";
import DeleteConfirmationModal from "~/modules/Admin/components/DeleteConfirmationModal";

type FileLessonProps = {
contentTypeToDisplay: string;
Expand All @@ -28,7 +29,7 @@ const FileLessonForm = ({
chapterToEdit,
lessonToEdit,
}: FileLessonProps) => {
const { form, onSubmit, onClickDelete } = useFileLessonForm({
const { form, onSubmit, onDelete } = useFileLessonForm({
chapterToEdit,
lessonToEdit,
setContentTypeToDisplay,
Expand All @@ -38,6 +39,16 @@ const FileLessonForm = ({
const [url, setUrl] = useState(lessonToEdit?.fileS3Key || "");
const fileType = form.watch("fileType");

const [isModalOpen, setIsModalOpen] = useState(false);

const onCloseModal = () => {
setIsModalOpen(false);
};

const onClickDelete = () => {
setIsModalOpen(true);
};

const handleFileUpload = useCallback(
async (file: File) => {
setIsUploading(true);
Expand Down Expand Up @@ -118,6 +129,7 @@ const FileLessonForm = ({
Save
</Button>
<Button
type="button"
onClick={
lessonToEdit ? onClickDelete : () => setContentTypeToDisplay(ContentTypes.EMPTY)
}
Expand All @@ -128,6 +140,14 @@ const FileLessonForm = ({
</div>
</form>
</Form>
<DeleteConfirmationModal
open={isModalOpen}
onClose={onCloseModal}
onDelete={onDelete}
contentType={
ContentTypes.VIDEO_LESSON_FORM ? DeleteContentType.VIDEO : DeleteContentType.PRESENTATION
}
/>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const useFileLessonForm = ({
}
};

const onClickDelete = async () => {
const onDelete = async () => {
if (!chapterToEdit?.id || !lessonToEdit?.id) {
console.error("Course ID or Chapter ID is missing.");
return;
Expand All @@ -93,5 +93,5 @@ export const useFileLessonForm = ({
}
};

return { form, onSubmit, onClickDelete };
return { form, onSubmit, onDelete };
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
export const fileLessonFormSchema = z.object({
title: z.string().min(1, "You need to fill this field to continue."),
description: z.string(),
type: z.enum(["presentation", "external_presentation", "video", "external_video"]),
type: z.enum(["presentation", "video"]),
displayOrder: z.number().optional(),
fileS3Key: z.string(),
fileType: z.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { useQuizLessonForm } from "./hooks/useQuizLessonForm";
import { Question, QuestionType } from "./QuizLessonForm.types";
import type { QuizLessonFormValues } from "./validators/quizLessonFormSchema";
import type { UseFormReturn } from "react-hook-form";
import { Chapter, ContentTypes, Lesson } from "../../../EditCourse.types";
import { useCallback } from "react";
import { Chapter, ContentTypes, DeleteContentType, Lesson } from "../../../EditCourse.types";
import { useCallback, useState } from "react";
import DeleteConfirmationModal from "~/modules/Admin/components/DeleteConfirmationModal";

type QuizLessonProps = {
setContentTypeToDisplay: (contentTypeToDisplay: string) => void;
Expand All @@ -28,14 +29,24 @@ const QuizLessonForm = ({
chapterToEdit,
lessonToEdit,
}: QuizLessonProps) => {
const { form, onSubmit, onClickDelete } = useQuizLessonForm({
const { form, onSubmit, onDelete } = useQuizLessonForm({
setContentTypeToDisplay,
chapterToEdit,
lessonToEdit,
});

const questions = form.watch("questions");

const [isModalOpen, setIsModalOpen] = useState(false);

const onCloseModal = () => {
setIsModalOpen(false);
};

const onClickDelete = () => {
setIsModalOpen(true);
};

const addQuestion = useCallback(
(questionType: string) => {
const questions = form.getValues("questions") || [];
Expand Down Expand Up @@ -131,6 +142,7 @@ const QuizLessonForm = ({
</Button>
{lessonToEdit ? (
<Button
type="button"
onClick={onClickDelete}
className="text-error-700 bg-color-white border border-neutral-300"
>
Expand All @@ -149,6 +161,12 @@ const QuizLessonForm = ({
</form>
</Form>
</div>
<DeleteConfirmationModal
open={isModalOpen}
onClose={onCloseModal}
onDelete={onDelete}
contentType={DeleteContentType.QUIZ}
/>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const useQuizLessonForm = ({
}
}, [lessonToEdit, reset]);

const onClickDelete = async () => {
const onDelete = async () => {
if (!chapterToEdit?.id || !lessonToEdit?.id) {
console.error("Course ID or Chapter ID is missing.");
return;
Expand Down Expand Up @@ -144,5 +144,5 @@ export const useQuizLessonForm = ({
}
};

return { form, onSubmit, onClickDelete };
return { form, onSubmit, onDelete };
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { Button } from "~/components/ui/button";
import { Form, FormControl, FormField, FormItem, FormMessage } from "~/components/ui/form";
import { Label } from "~/components/ui/label";

import { ContentTypes } from "../../../EditCourse.types";
import { ContentTypes, DeleteContentType } from "../../../EditCourse.types";

import { useTextLessonForm } from "./hooks/useTextLessonForm";

import type { Chapter, Lesson } from "../../../EditCourse.types";
import { useState } from "react";
import DeleteConfirmationModal from "~/modules/Admin/components/DeleteConfirmationModal";

type TextLessonProps = {
setContentTypeToDisplay: (contentTypeToDisplay: string) => void;
Expand All @@ -21,12 +23,22 @@ const TextLessonForm = ({
chapterToEdit,
lessonToEdit,
}: TextLessonProps) => {
const { form, onSubmit, onClickDelete } = useTextLessonForm({
const { form, onSubmit, onDelete } = useTextLessonForm({
chapterToEdit,
lessonToEdit,
setContentTypeToDisplay,
});

const [isModalOpen, setIsModalOpen] = useState(false);

const onCloseModal = () => {
setIsModalOpen(false);
};

const onClickDelete = () => {
setIsModalOpen(true);
};

return (
<div className="flex flex-col gap-y-6 p-8 bg-white rounded-lg">
<div className="flex flex-col gap-y-1">
Expand Down Expand Up @@ -75,6 +87,7 @@ const TextLessonForm = ({
Save
</Button>
<Button
type="button"
onClick={
lessonToEdit ? onClickDelete : () => setContentTypeToDisplay(ContentTypes.EMPTY)
}
Expand All @@ -85,6 +98,12 @@ const TextLessonForm = ({
</div>
</form>
</Form>
<DeleteConfirmationModal
open={isModalOpen}
onClose={onCloseModal}
onDelete={onDelete}
contentType={DeleteContentType.TEXT_BLOCK}
/>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const useTextLessonForm = ({
}
};

const onClickDelete = async () => {
const onDelete = async () => {
if (!chapterToEdit?.id || !lessonToEdit?.id) {
console.error("Course ID or Chapter ID is missing.");
return;
Expand All @@ -94,5 +94,5 @@ export const useTextLessonForm = ({
}
};

return { form, onSubmit, onClickDelete };
return { form, onSubmit, onDelete };
};
21 changes: 15 additions & 6 deletions apps/web/app/modules/Admin/EditCourse/EditCourse.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ export const ContentTypes = {

export type LessonIcons = "Text" | "Video" | "Presentation" | "Quiz";

export enum LessonType {
VIDEO = "video",
TEXT_BLOCK = "text_block",
PRESENTATION = "presentation",
QUIZ = "quiz",
}
export const LessonType = {
VIDEO: "video",
TEXT_BLOCK: "text_block",
PRESENTATION: "presentation",
QUIZ: "quiz",
} as const;

export type LessonType = (typeof LessonType)[keyof typeof LessonType];

export const DeleteContentType = {
...LessonType,
CHAPTER: "chapter",
} as const;

export type DeleteContentType = (typeof DeleteContentType)[keyof typeof DeleteContentType];
Loading

0 comments on commit f928801

Please sign in to comment.