diff --git a/src/constants/apiConstants.ts b/src/constants/apiConstants.ts index d6d9caf..d481940 100644 --- a/src/constants/apiConstants.ts +++ b/src/constants/apiConstants.ts @@ -10,3 +10,4 @@ export const API_SECTIONS = BASE_URL.concat("/sections/"); export const API_TOPICS = BASE_URL.concat("/topics/"); export const API_LECTURES = BASE_URL.concat("/lectures/"); export const API_TASKS = BASE_URL.concat("/tasks/"); +export const API_TOPIC_CONTENT = BASE_URL.concat("/topic-content"); diff --git a/src/constants/common.ts b/src/constants/common.ts index 9e28da5..cb13798 100644 --- a/src/constants/common.ts +++ b/src/constants/common.ts @@ -13,3 +13,8 @@ export const ROLES_OPTIONS = Object.values(Role).map((role) => ({ label: ROLES_DISPLAY[role], value: role, })); + +export enum TopicContentType { + TASK, + LECTURE, +} diff --git a/src/interfaces/topicContent.ts b/src/interfaces/topicContent.ts new file mode 100644 index 0000000..1924c6c --- /dev/null +++ b/src/interfaces/topicContent.ts @@ -0,0 +1,24 @@ +import { TopicContentType } from "@/constants/common"; +import { ITask } from "./task"; +import { ILecture } from "./lecture"; +import { ITopic } from "./topic"; + +export interface ITopicContent { + id: number; + type: TopicContentType; + orderNumber: number; + taskId?: number; + lectureId?: number; + task?: ITask; + lecture?: ILecture; + topicId: number; + topic: ITopic; +} + +export interface ITopicContentCreate { + type: TopicContentType; + orderNumber: number; + taskId?: number; + lectureId?: number; + topicId: number; +} diff --git a/src/pages/lectures/LectureDetails.tsx b/src/pages/lectures/LectureDetails.tsx index 8e99a95..edabdfb 100644 --- a/src/pages/lectures/LectureDetails.tsx +++ b/src/pages/lectures/LectureDetails.tsx @@ -80,7 +80,7 @@ function LectureDetails() { isSuccess, } = useLectureDetailsQuery(+id!, { enabled: !!id }); - const { mutate: createLecture, isPending } = useLectureMutation({ + const { mutate, isPending } = useLectureMutation({ onSuccess: () => { queryClient.invalidateQueries({ refetchType: "all", @@ -94,6 +94,7 @@ function LectureDetails() { onError: () => { showErrorNotification(); }, + id: +id!, }); const { mutate: deleteLecture, isPending: isDeleting } = useLectureDeletion( @@ -124,7 +125,7 @@ function LectureDetails() { const isValid = Object.values(lectureForm.formState.errors).length === 0; const onSubmit: SubmitHandler = (data: ILectureCreate) => { - createLecture({ ...data, number: +data.number }); + mutate({ ...data, number: +data.number }); }; useEffect(() => { diff --git a/src/pages/sections/SectionDetails.tsx b/src/pages/sections/SectionDetails.tsx index d2bab6f..db52f2a 100644 --- a/src/pages/sections/SectionDetails.tsx +++ b/src/pages/sections/SectionDetails.tsx @@ -97,6 +97,7 @@ function SectionDetails() { onError: () => { showErrorNotification(); }, + id: +id!, }); const { showErrorNotification, showSuccessNotification } = useNotification(); diff --git a/src/pages/tasks/TaskDetails.tsx b/src/pages/tasks/TaskDetails.tsx index e9d404a..a69bf20 100644 --- a/src/pages/tasks/TaskDetails.tsx +++ b/src/pages/tasks/TaskDetails.tsx @@ -74,6 +74,7 @@ function TaskDetails() { onError: () => { showErrorNotification(); }, + id: +id!, }); const { mutate: deleteTask, isPending: isDeleting } = useTaskDeletion({ diff --git a/src/pages/users/UserDetails.tsx b/src/pages/users/UserDetails.tsx index 776c597..b285ef0 100644 --- a/src/pages/users/UserDetails.tsx +++ b/src/pages/users/UserDetails.tsx @@ -83,6 +83,7 @@ function UserDetails() { onError: () => { showErrorNotification(); }, + id: +id!, }); const isValid = Object.values(userForm.formState.errors).length === 0; diff --git a/src/queries/lectures.ts b/src/queries/lectures.ts index f49cb1d..e135a39 100644 --- a/src/queries/lectures.ts +++ b/src/queries/lectures.ts @@ -1,10 +1,11 @@ import { QUERY_KEYS } from "@/constants/queryKeys"; -import { ILecture } from "@/interfaces/lecture"; +import { ILecture, ILectureCreate } from "@/interfaces/lecture"; import { createLecture, getLectureDetails, getLectures, removeLecture, + updateLecture, } from "@/requests/lectures"; import { useMutation, useQuery } from "@tanstack/react-query"; @@ -46,11 +47,17 @@ export const useLectureDetailsQuery = ( interface MutationQuery { onSuccess?: (data: ILecture) => void; onError?: () => void; + id?: number; } -export const useLectureMutation = ({ onSuccess, onError }: MutationQuery) => { +export const useLectureMutation = ({ + onSuccess, + onError, + id, +}: MutationQuery) => { const { data, mutate, isPending } = useMutation({ - mutationFn: createLecture, + mutationFn: (data: ILectureCreate) => + id ? updateLecture(id, data) : createLecture(data), onError, onSuccess, }); diff --git a/src/queries/sections.ts b/src/queries/sections.ts index 3a74de6..46d588b 100644 --- a/src/queries/sections.ts +++ b/src/queries/sections.ts @@ -49,11 +49,16 @@ export const useSectionDetailsQuery = ( interface MutationQuery { onSuccess?: (data: ISection) => void; onError?: () => void; + id?: number; } -export const useSectionMutation = ({ onSuccess, onError }: MutationQuery) => { +export const useSectionMutation = ({ + onSuccess, + onError, + id, +}: MutationQuery) => { const { data, mutate, isPending } = useMutation({ - mutationFn: (data: ISectionCreate, id?: number) => + mutationFn: (data: ISectionCreate) => id ? updateSection(id, data) : createSection(data), onSuccess, onError, diff --git a/src/queries/tasks.ts b/src/queries/tasks.ts index 5130ca5..c942173 100644 --- a/src/queries/tasks.ts +++ b/src/queries/tasks.ts @@ -41,12 +41,13 @@ export const useTaskDetails = (id: number, { enabled }: QueryParams) => { interface MutationQuery { onSuccess?: (data: ITask) => void; onError?: () => void; + id?: number; } export const useTaskMutation = (params?: MutationQuery) => { const { data, mutate, isPending } = useMutation({ - mutationFn: (data: ITaskCreate, id?: number) => - id ? updateTask(id, data) : createTask(data), + mutationFn: (data: ITaskCreate) => + params?.id ? updateTask(params.id, data) : createTask(data), onSuccess: params?.onSuccess, onError: params?.onError, }); diff --git a/src/queries/topicContent.ts b/src/queries/topicContent.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/queries/users.ts b/src/queries/users.ts index 92efbf6..1616c7e 100644 --- a/src/queries/users.ts +++ b/src/queries/users.ts @@ -1,21 +1,24 @@ import { QUERY_KEYS } from "@/constants/queryKeys"; -import { IUser } from "@/interfaces/user"; +import { ICreateUser, IUser } from "@/interfaces/user"; import { createUser, deleteUser, getUserDetails, getUsers, + updateUser, } from "@/requests/users"; import { useMutation, useQuery } from "@tanstack/react-query"; interface MutationQuery { onSuccess?: (data: IUser) => void; onError?: () => void; + id?: number; } -export const useUserMutation = ({ onSuccess, onError }: MutationQuery) => { +export const useUserMutation = ({ onSuccess, onError, id }: MutationQuery) => { const { data, mutate, isPending } = useMutation({ - mutationFn: createUser, + mutationFn: (data: ICreateUser) => + id ? updateUser(id, data) : createUser(data), onSuccess, onError, }); diff --git a/src/requests/lectures.ts b/src/requests/lectures.ts index ff3e3c8..af085e8 100644 --- a/src/requests/lectures.ts +++ b/src/requests/lectures.ts @@ -10,11 +10,14 @@ export async function getLectureDetails(id: number): Promise { return axios.get(`${API_LECTURES}${id}`).then(({ data }) => data); } -export async function createLecture(data: ILectureCreate) { +export async function createLecture(data: ILectureCreate): Promise { return axios.post(API_LECTURES, data).then(({ data }) => data); } -export async function updateLecture(id: number, data: Partial) { +export async function updateLecture( + id: number, + data: Partial +): Promise { return axios.patch(`${API_LECTURES}${id}`, data).then(({ data }) => data); } diff --git a/src/requests/topicContent.ts b/src/requests/topicContent.ts new file mode 100644 index 0000000..42f88fe --- /dev/null +++ b/src/requests/topicContent.ts @@ -0,0 +1,26 @@ +import { API_TOPIC_CONTENT } from "@/constants/apiConstants"; +import { ITopicContent, ITopicContentCreate } from "@/interfaces/topicContent"; +import axios from "axios"; + +export async function getTopicContent(): Promise { + return axios.get(API_TOPIC_CONTENT).then(({ data }) => data); +} + +export async function getTopicContentDetails( + id: number +): Promise { + return axios.get(`${API_TOPIC_CONTENT}${id}`).then(({ data }) => data); +} + +export async function updateTopicContent( + id: number, + data: Partial +) { + return axios + .patch(`${API_TOPIC_CONTENT}${id}`, data) + .then(({ data }) => data); +} + +export async function removeTopicContent(id: number) { + return axios.delete(`${API_TOPIC_CONTENT}${id}`).then(({ data }) => data); +} diff --git a/src/requests/users.ts b/src/requests/users.ts index 6035b93..cd58bfb 100644 --- a/src/requests/users.ts +++ b/src/requests/users.ts @@ -14,6 +14,13 @@ export async function createUser(data: ICreateUser): Promise { return axios.post(API_USERS, data).then(({ data }) => data); } +export async function updateUser( + id: number, + data: Partial +): Promise { + return axios.patch(`${API_USERS}${id}`, data).then(({ data }) => data); +} + export async function deleteUser(id: number): Promise { return axios.delete(`${API_USERS}${id}`).then(({ data }) => data); }