Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions codewit/client/src/hooks/useCourse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import axios from 'axios';
import { Course, StudentProgress } from '@codewit/interfaces';
import { useAuth } from './useAuth';

interface CourseMutationPayload {
title: string;
enrolling: boolean;
auto_enroll: boolean;
language?: string;
modules?: number[];
instructors?: number[];
roster?: number[];
}

// General hook to handle fetching data with axios
const useAxiosFetch = (initialUrl: string, initialData: Course[] = []) => {
const [data, setData] = useState<Course[]>(initialData);
Expand Down Expand Up @@ -86,13 +96,13 @@ const useAxiosCRUD = (method: 'get' | 'post' | 'patch' | 'delete') => {
// Hook to post a new course
export const usePostCourse = () => {
const { operation } = useAxiosCRUD('post');
return (courseData: Course) => operation('/api/courses', courseData);
return (courseData: CourseMutationPayload) => operation('/api/courses', courseData);
};

// Hook to patch an existing course
export const usePatchCourse = () => {
const { operation } = useAxiosCRUD('patch');
return (courseData: Course, uid: number | string) => operation(`/api/courses/${uid}`, courseData);
return (courseData: CourseMutationPayload, uid: number | string) => operation(`/api/courses/${uid}`, courseData);
};

// Hook to delete a course
Expand Down Expand Up @@ -124,4 +134,4 @@ export const useCourseProgress = (courseId: string) => {
}, [courseId]);

return { data, setData, loading, error };
};
};
Loading
Loading