Skip to content

Commit

Permalink
using fetch wrtappers
Browse files Browse the repository at this point in the history
  • Loading branch information
mono424 committed Sep 14, 2023
1 parent 85a17f5 commit f21f0db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
36 changes: 12 additions & 24 deletions web/ts/api/admin-lecture-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { del, get, post, put } from "../utilities/fetch-wrappers";
import {del, get, patch, post, put} from "../utilities/fetch-wrappers";
import { StatusCodes } from "http-status-codes";
import { Delete, patchData, postData, postFormData, putData, uploadFile, UploadFileListener } from "../global";

Expand Down Expand Up @@ -166,29 +166,26 @@ export const AdminLectureList = {
* @param request
*/
updateMetadata: async function (courseId: number, lectureId: number, request: UpdateLectureMetaRequest) {
console.log({
request,
});
const promises = [];
if (request.name !== undefined) {
promises.push(
postData(`/api/course/${courseId}/renameLecture/${lectureId}`, {
post(`/api/course/${courseId}/renameLecture/${lectureId}`, {
name: request.name,
}),
);
}

if (request.description !== undefined) {
promises.push(
putData(`/api/course/${courseId}/updateDescription/${lectureId}`, {
put(`/api/course/${courseId}/updateDescription/${lectureId}`, {
name: request.description,
}),
);
}

if (request.lectureHallId !== undefined) {
promises.push(
postData("/api/setLectureHall", {
post("/api/setLectureHall", {
streamIds: [lectureId],
lectureHall: request.lectureHallId,
}),
Expand All @@ -197,7 +194,7 @@ export const AdminLectureList = {

if (request.isChatEnabled !== undefined) {
promises.push(
patchData(`/api/stream/${lectureId}/chat/enabled`, {
patch(`/api/stream/${lectureId}/chat/enabled`, {
lectureId,
isChatEnabled: request.isChatEnabled,
}),
Expand All @@ -216,8 +213,8 @@ export const AdminLectureList = {
* @param courseId
* @param lectureId
*/
saveSeriesMetadata: async (courseId: number, lectureId: number) => {
return await postData(`/api/course/${courseId}/updateLectureSeries/${lectureId}`);
saveSeriesMetadata: async (courseId: number, lectureId: number): Promise<void> => {
await post(`/api/course/${courseId}/updateLectureSeries/${lectureId}`);
},

/**
Expand All @@ -226,10 +223,7 @@ export const AdminLectureList = {
* @param sections
*/
addSections: async (lectureId: number, sections: VideoSection[]): Promise<void> => {
const res = await postData(`/api/stream/${lectureId}/sections`, sections);
if (res.status !== StatusCodes.OK) {
throw Error(res.body.toString());
}
await post(`/api/stream/${lectureId}/sections`, sections);
},

/**
Expand All @@ -255,10 +249,7 @@ export const AdminLectureList = {
* @param sectionId
*/
removeSection: async (lectureId: number, sectionId: number): Promise<void> => {
const res = await Delete(`/api/stream/${lectureId}/sections/${sectionId}`);
if (res.status !== StatusCodes.OK) {
throw Error(res.body.toString());
}
await del(`/api/stream/${lectureId}/sections/${sectionId}`);
},

/**
Expand All @@ -267,12 +258,9 @@ export const AdminLectureList = {
* @param isPrivate
*/
setPrivate: async (lectureId: number, isPrivate: boolean): Promise<void> => {
const res = await patchData(`/api/stream/${lectureId}/visibility`, {
await patch(`/api/stream/${lectureId}/visibility`, {
private: isPrivate,
});
if (res.status !== StatusCodes.OK) {
throw Error(res.body.toString());
}
},

/**
Expand Down Expand Up @@ -332,7 +320,7 @@ export const AdminLectureList = {
},

deleteAttachment: async (courseId: number, lectureId: number, attachmentId: number) => {
return Delete(`/api/stream/${lectureId}/files/${attachmentId}`);
return del(`/api/stream/${lectureId}/files/${attachmentId}`);
},

/**
Expand All @@ -351,7 +339,7 @@ export const AdminLectureList = {
* @param lectureIds
*/
delete: async function (courseId: number, lectureIds: number[]): Promise<Response> {
return await postData(`/api/course/${courseId}/deleteLectures`, {
return await post(`/api/course/${courseId}/deleteLectures`, {
streamIDs: lectureIds,
});
},
Expand Down
21 changes: 21 additions & 0 deletions web/ts/utilities/fetch-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ export async function put(url = "", body: object = {}) {
});
}

/**
* Wrapper for Javascript's fetch function for PATCH
* @param {string} url URL to fetch
* @param {object} body Data object to put
* @return {Promise<Response>}
*/
export async function patch(url = "", body = {}) {
return await fetch(url, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}).then((res) => {
if (!res.ok) {
throw Error(res.statusText);
}
return res;
});
}

/**
* Wrapper for Javascript's fetch function for DELETE
* @param {string} url URL to fetch
Expand Down

0 comments on commit f21f0db

Please sign in to comment.