mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: format with prettier
This commit is contained in:
parent
03fb09c3d6
commit
a147ad6610
164 changed files with 11257 additions and 8154 deletions
|
|
@ -1,63 +1,70 @@
|
|||
'use client';
|
||||
import PageLoading from '@components/Objects/Loaders/PageLoading';
|
||||
import { getAPIUrl } from '@services/config/config';
|
||||
import { swrFetcher } from '@services/utils/ts/requests';
|
||||
'use client'
|
||||
import PageLoading from '@components/Objects/Loaders/PageLoading'
|
||||
import { getAPIUrl } from '@services/config/config'
|
||||
import { swrFetcher } from '@services/utils/ts/requests'
|
||||
import React, { createContext, useContext, useEffect, useReducer } from 'react'
|
||||
import useSWR from 'swr';
|
||||
import useSWR from 'swr'
|
||||
|
||||
export const CourseContext = createContext(null) as any;
|
||||
export const CourseDispatchContext = createContext(null) as any;
|
||||
export const CourseContext = createContext(null) as any
|
||||
export const CourseDispatchContext = createContext(null) as any
|
||||
|
||||
export function CourseProvider({ children, courseuuid }: { children: React.ReactNode, courseuuid: string }) {
|
||||
const { data: courseStructureData } = useSWR(`${getAPIUrl()}courses/${courseuuid}/meta`, swrFetcher);
|
||||
const [courseStructure, dispatchCourseStructure] = useReducer(courseReducer,
|
||||
{
|
||||
courseStructure: courseStructureData ? courseStructureData : {},
|
||||
courseOrder: {},
|
||||
isSaved: true
|
||||
}
|
||||
);
|
||||
export function CourseProvider({
|
||||
children,
|
||||
courseuuid,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
courseuuid: string
|
||||
}) {
|
||||
const { data: courseStructureData } = useSWR(
|
||||
`${getAPIUrl()}courses/${courseuuid}/meta`,
|
||||
swrFetcher
|
||||
)
|
||||
const [courseStructure, dispatchCourseStructure] = useReducer(courseReducer, {
|
||||
courseStructure: courseStructureData ? courseStructureData : {},
|
||||
courseOrder: {},
|
||||
isSaved: true,
|
||||
})
|
||||
|
||||
// When courseStructureData is loaded, update the state
|
||||
useEffect(() => {
|
||||
if (courseStructureData) {
|
||||
dispatchCourseStructure({
|
||||
type: 'setCourseStructure',
|
||||
payload: courseStructureData,
|
||||
})
|
||||
}
|
||||
}, [courseStructureData])
|
||||
|
||||
// When courseStructureData is loaded, update the state
|
||||
useEffect(() => {
|
||||
if (courseStructureData) {
|
||||
dispatchCourseStructure({ type: 'setCourseStructure', payload: courseStructureData });
|
||||
}
|
||||
}, [courseStructureData]);
|
||||
if (!courseStructureData) return <PageLoading></PageLoading>
|
||||
|
||||
|
||||
if (!courseStructureData) return <PageLoading></PageLoading>
|
||||
|
||||
|
||||
return (
|
||||
<CourseContext.Provider value={courseStructure}>
|
||||
<CourseDispatchContext.Provider value={dispatchCourseStructure}>
|
||||
{children}
|
||||
</CourseDispatchContext.Provider>
|
||||
</CourseContext.Provider>
|
||||
)
|
||||
return (
|
||||
<CourseContext.Provider value={courseStructure}>
|
||||
<CourseDispatchContext.Provider value={dispatchCourseStructure}>
|
||||
{children}
|
||||
</CourseDispatchContext.Provider>
|
||||
</CourseContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useCourse() {
|
||||
return useContext(CourseContext);
|
||||
return useContext(CourseContext)
|
||||
}
|
||||
|
||||
export function useCourseDispatch() {
|
||||
return useContext(CourseDispatchContext);
|
||||
return useContext(CourseDispatchContext)
|
||||
}
|
||||
|
||||
function courseReducer(state: any, action: any) {
|
||||
switch (action.type) {
|
||||
case 'setCourseStructure':
|
||||
return { ...state, courseStructure: action.payload };
|
||||
case 'setCourseOrder':
|
||||
return { ...state, courseOrder: action.payload };
|
||||
case 'setIsSaved':
|
||||
return { ...state, isSaved: true };
|
||||
case 'setIsNotSaved':
|
||||
return { ...state, isSaved: false };
|
||||
default:
|
||||
throw new Error(`Unhandled action type: ${action.type}`);
|
||||
}
|
||||
}
|
||||
switch (action.type) {
|
||||
case 'setCourseStructure':
|
||||
return { ...state, courseStructure: action.payload }
|
||||
case 'setCourseOrder':
|
||||
return { ...state, courseOrder: action.payload }
|
||||
case 'setIsSaved':
|
||||
return { ...state, isSaved: true }
|
||||
case 'setIsNotSaved':
|
||||
return { ...state, isSaved: false }
|
||||
default:
|
||||
throw new Error(`Unhandled action type: ${action.type}`)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue