feat: better err reporting & init private endpoint

This commit is contained in:
swve 2023-04-09 01:36:47 +02:00
parent 8f8257b9e7
commit 6137c907d2
4 changed files with 30 additions and 9 deletions

View file

@ -1,5 +1,6 @@
"use client";
import React from "react";
import { useState, useEffect } from "react";
import styled from "styled-components";
import { Title } from "@components/UI/Elements/Styles/Title";
@ -16,6 +17,8 @@ import Modal from "@components/UI/Modal/Modal";
import AuthProvider from "@components/Security/AuthProvider";
function CourseEdit(params: any) {
const router = useRouter();
// Initial Course State
const [data, setData] = useState(initialData2) as any;
@ -31,9 +34,14 @@ function CourseEdit(params: any) {
const orgslug = params.params.orgslug;
async function getCourseChapters() {
const courseChapters = await getCourseChaptersMetadata(courseid);
setData(courseChapters);
console.log("courseChapters", courseChapters);
try {
const courseChapters = await getCourseChaptersMetadata(courseid);
setData(courseChapters);
} catch (error: any) {
if (error.status === 401) {
router.push("/login");
}
}
}
useEffect(() => {
@ -226,7 +234,6 @@ function CourseEdit(params: any) {
return (
<>
<AuthProvider />
<Page>
<Title>
Edit Course {" "}

View file

@ -9,10 +9,15 @@ import { RequestBody } from "@services/utils/requests";
//TODO : depreciate this function
export async function getCourseChaptersMetadata(course_id: any) {
const data: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null))
.then((result) => result.json())
.catch((error) => console.log("error", error));
const response = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null));
if (!response.ok) {
const error: any = new Error(`Error ${response.status}: ${response.statusText}`, {});
error.status = response.status;
throw error;
}
const data = await response.json();
return data;
}
@ -20,7 +25,7 @@ export async function updateChaptersMetadata(course_id: any, data: any) {
const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("PUT", data))
.then((result) => result.json())
.catch((error) => console.log("error", error));
return result;
}