mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: use swr for collections add page
This commit is contained in:
parent
4ccb253e71
commit
fee546cbd0
1 changed files with 5 additions and 20 deletions
|
|
@ -2,9 +2,10 @@
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { Title } from "@components/UI/Elements/Styles/Title";
|
import { Title } from "@components/UI/Elements/Styles/Title";
|
||||||
import { getOrganizationContextInfo } from "@services/orgs";
|
|
||||||
import { getOrgCourses } from "@services/courses/courses";
|
|
||||||
import { createCollection } from "@services/collections";
|
import { createCollection } from "@services/collections";
|
||||||
|
import useSWR from "swr";
|
||||||
|
import { getAPIUrl } from "@services/config";
|
||||||
|
import { swrFetcher } from "@services/utils/requests";
|
||||||
|
|
||||||
function NewCollection(params : any) {
|
function NewCollection(params : any) {
|
||||||
const orgslug = params.params.orgslug;
|
const orgslug = params.params.orgslug;
|
||||||
|
|
@ -12,18 +13,9 @@ function NewCollection(params : any) {
|
||||||
const [org, setOrg] = React.useState({}) as any;
|
const [org, setOrg] = React.useState({}) as any;
|
||||||
const [description, setDescription] = React.useState("");
|
const [description, setDescription] = React.useState("");
|
||||||
const [selectedCourses, setSelectedCourses] = React.useState([]) as any;
|
const [selectedCourses, setSelectedCourses] = React.useState([]) as any;
|
||||||
const [courses, setCourses] = React.useState([]) as any;
|
|
||||||
const [isLoading, setIsLoading] = React.useState(false);
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
async function getCourses() {
|
const { data: courses, error: error } = useSWR(`${getAPIUrl()}courses/org_slug/${orgslug}/page/1/limit/10`, swrFetcher);
|
||||||
setIsLoading(true);
|
|
||||||
const org = await getOrganizationContextInfo(orgslug);
|
|
||||||
setOrg(org);
|
|
||||||
const courses = await getOrgCourses(org.org_id);
|
|
||||||
setCourses(courses);
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setName(event.target.value);
|
setName(event.target.value);
|
||||||
|
|
@ -46,20 +38,13 @@ function NewCollection(params : any) {
|
||||||
router.push("/org/" + orgslug + "/collections");
|
router.push("/org/" + orgslug + "/collections");
|
||||||
};
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (params.params.orgslug) {
|
|
||||||
getCourses();
|
|
||||||
}
|
|
||||||
return () => {};
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [params.params.orgslug]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Title>Add new</Title>
|
<Title>Add new</Title>
|
||||||
<br />
|
<br />
|
||||||
<input type="text" placeholder="Name" value={name} onChange={handleNameChange} />
|
<input type="text" placeholder="Name" value={name} onChange={handleNameChange} />
|
||||||
{isLoading ? (
|
{!courses ? (
|
||||||
<p>Loading...</p>
|
<p>Loading...</p>
|
||||||
) : (
|
) : (
|
||||||
<div>
|
<div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue