Skip to content

Commit

Permalink
Start topic content resource, fix resource updating
Browse files Browse the repository at this point in the history
  • Loading branch information
akmatoff committed Feb 22, 2024
1 parent 3b2e34a commit f2927c8
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/constants/apiConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
5 changes: 5 additions & 0 deletions src/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export const ROLES_OPTIONS = Object.values(Role).map((role) => ({
label: ROLES_DISPLAY[role],
value: role,
}));

export enum TopicContentType {
TASK,
LECTURE,
}
24 changes: 24 additions & 0 deletions src/interfaces/topicContent.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 3 additions & 2 deletions src/pages/lectures/LectureDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -94,6 +94,7 @@ function LectureDetails() {
onError: () => {
showErrorNotification();
},
id: +id!,
});

const { mutate: deleteLecture, isPending: isDeleting } = useLectureDeletion(
Expand Down Expand Up @@ -124,7 +125,7 @@ function LectureDetails() {
const isValid = Object.values(lectureForm.formState.errors).length === 0;

const onSubmit: SubmitHandler<ILectureCreate> = (data: ILectureCreate) => {
createLecture({ ...data, number: +data.number });
mutate({ ...data, number: +data.number });
};

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions src/pages/sections/SectionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function SectionDetails() {
onError: () => {
showErrorNotification();
},
id: +id!,
});

const { showErrorNotification, showSuccessNotification } = useNotification();
Expand Down
1 change: 1 addition & 0 deletions src/pages/tasks/TaskDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function TaskDetails() {
onError: () => {
showErrorNotification();
},
id: +id!,
});

const { mutate: deleteTask, isPending: isDeleting } = useTaskDeletion({
Expand Down
1 change: 1 addition & 0 deletions src/pages/users/UserDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function UserDetails() {
onError: () => {
showErrorNotification();
},
id: +id!,
});

const isValid = Object.values(userForm.formState.errors).length === 0;
Expand Down
13 changes: 10 additions & 3 deletions src/queries/lectures.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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,
});
Expand Down
9 changes: 7 additions & 2 deletions src/queries/sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/queries/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Empty file added src/queries/topicContent.ts
Empty file.
9 changes: 6 additions & 3 deletions src/queries/users.ts
Original file line number Diff line number Diff line change
@@ -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,
});
Expand Down
7 changes: 5 additions & 2 deletions src/requests/lectures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ export async function getLectureDetails(id: number): Promise<ILecture> {
return axios.get(`${API_LECTURES}${id}`).then(({ data }) => data);
}

export async function createLecture(data: ILectureCreate) {
export async function createLecture(data: ILectureCreate): Promise<ILecture> {
return axios.post(API_LECTURES, data).then(({ data }) => data);
}

export async function updateLecture(id: number, data: Partial<ILectureCreate>) {
export async function updateLecture(
id: number,
data: Partial<ILectureCreate>
): Promise<ILecture> {
return axios.patch(`${API_LECTURES}${id}`, data).then(({ data }) => data);
}

Expand Down
26 changes: 26 additions & 0 deletions src/requests/topicContent.ts
Original file line number Diff line number Diff line change
@@ -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<ITopicContent[]> {
return axios.get(API_TOPIC_CONTENT).then(({ data }) => data);
}

export async function getTopicContentDetails(
id: number
): Promise<ITopicContent> {
return axios.get(`${API_TOPIC_CONTENT}${id}`).then(({ data }) => data);
}

export async function updateTopicContent(
id: number,
data: Partial<ITopicContentCreate>
) {
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);
}
7 changes: 7 additions & 0 deletions src/requests/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export async function createUser(data: ICreateUser): Promise<IUser> {
return axios.post(API_USERS, data).then(({ data }) => data);
}

export async function updateUser(
id: number,
data: Partial<ICreateUser>
): Promise<IUser> {
return axios.patch(`${API_USERS}${id}`, data).then(({ data }) => data);
}

export async function deleteUser(id: number): Promise<IUser> {
return axios.delete(`${API_USERS}${id}`).then(({ data }) => data);
}

0 comments on commit f2927c8

Please sign in to comment.