diff --git a/002_source/cms/src/app/dashboard/lesson_types/[typeId]/page.tsx b/002_source/cms/src/app/dashboard/lesson_types/[typeId]/page.tsx index 8f9d042..007ee14 100644 --- a/002_source/cms/src/app/dashboard/lesson_types/[typeId]/page.tsx +++ b/002_source/cms/src/app/dashboard/lesson_types/[typeId]/page.tsx @@ -26,19 +26,26 @@ import { PencilSimple as PencilSimpleIcon } from '@phosphor-icons/react/dist/ssr import { Plus as PlusIcon } from '@phosphor-icons/react/dist/ssr/Plus'; import { ShieldWarning as ShieldWarningIcon } from '@phosphor-icons/react/dist/ssr/ShieldWarning'; import { User as UserIcon } from '@phosphor-icons/react/dist/ssr/User'; +import type { RecordModel } from 'pocketbase'; +import PocketBase from 'pocketbase'; import { useTranslation } from 'react-i18next'; import { paths } from '@/paths'; import { dayjs } from '@/lib/dayjs'; +import { logger } from '@/lib/default-logger'; import { PropertyItem } from '@/components/core/property-item'; import { PropertyList } from '@/components/core/property-list'; +import { toast } from '@/components/core/toaster'; // import { getLessonTypeById } from '@/components/dashboard/lesson_type/http-actions'; import { LessonTypeDefaultValue, type LessonType } from '@/components/dashboard/lesson_type/ILessonType'; +import { defaultLessonType } from '@/components/dashboard/lesson_type/interfaces'; import { Notifications } from '@/components/dashboard/lesson_type/notifications'; import { Payments } from '@/components/dashboard/lesson_type/payments'; import type { Address } from '@/components/dashboard/lesson_type/shipping-address'; import { ShippingAddress } from '@/components/dashboard/lesson_type/shipping-address'; +const pb = new PocketBase(process.env.NEXT_PUBLIC_POCKETBASE_URL); + export default function Page(): React.JSX.Element { const { t } = useTranslation(); const { typeId } = useParams<{ typeId: string }>(); @@ -51,17 +58,20 @@ export default function Page(): React.JSX.Element { } React.useEffect(() => { - // getLessonTypeById(typeId) - // .then((lessonType: LessonType) => { - // setIsLoading(false); - // setShowLessonType(lessonType); - // }) - // .catch((err) => { - // // console.error(err); - // console.error(t('lessonType.load_error')); - // }); - // console.log('hello'); - }, []); + if (typeId) { + pb.collection('LessonsTypes') + .getOne(typeId) + .then((lessonType: RecordModel) => { + setShowLessonType({ ...defaultLessonType, ...lessonType }); + setIsLoading(false); + }) + .catch((err) => { + logger.error(err); + toast(t('dashboard.lessonTypes.list.error')); + }); + } + }, [typeId]); + if (isLoading) return
{t('common.loading')}
; return ( diff --git a/002_source/cms/src/app/dashboard/lesson_types/edit/[typeId]/page.tsx b/002_source/cms/src/app/dashboard/lesson_types/edit/[typeId]/page.tsx index 91e89ef..1e391f3 100644 --- a/002_source/cms/src/app/dashboard/lesson_types/edit/[typeId]/page.tsx +++ b/002_source/cms/src/app/dashboard/lesson_types/edit/[typeId]/page.tsx @@ -14,6 +14,11 @@ import { LessonTypeEditForm } from '@/components/dashboard/lesson_type/lesson-ty export default function Page(): React.JSX.Element { const { t } = useTranslation(); + + React.useEffect(() => { + console.log('helloworld'); + }, []); + return ( { + console.log('helloworld'); + }, []); + + return <>helloworld; +} + +export default Page; diff --git a/002_source/cms/src/components/core/i18n-provider.tsx b/002_source/cms/src/components/core/i18n-provider.tsx index c62b953..623a985 100644 --- a/002_source/cms/src/components/core/i18n-provider.tsx +++ b/002_source/cms/src/components/core/i18n-provider.tsx @@ -16,7 +16,6 @@ export function I18nProvider({ children, language = 'en' }: I18nProviderProps): const { i18n } = useTranslation(); React.useEffect(() => { - console.log(`find me ? I18nProvider ${language}`); // i18n // .changeLanguage(language) // .then(() => { diff --git a/002_source/cms/src/components/dashboard/lesson_type/lesson-type-edit-form.tsx b/002_source/cms/src/components/dashboard/lesson_type/lesson-type-edit-form.tsx index 555ffc1..0bf807f 100644 --- a/002_source/cms/src/components/dashboard/lesson_type/lesson-type-edit-form.tsx +++ b/002_source/cms/src/components/dashboard/lesson_type/lesson-type-edit-form.tsx @@ -3,6 +3,7 @@ import * as React from 'react'; import RouterLink from 'next/link'; import { useParams, useRouter } from 'next/navigation'; +import { COL_LESSON_TYPES } from '@/constants'; import { zodResolver } from '@hookform/resolvers/zod'; import { LoadingButton } from '@mui/lab'; import { MenuItem } from '@mui/material'; @@ -23,6 +24,8 @@ import Select from '@mui/material/Select'; import Stack from '@mui/material/Stack'; import Typography from '@mui/material/Typography'; import Grid from '@mui/material/Unstable_Grid2'; +import type { RecordModel } from 'pocketbase'; +import PocketBase from 'pocketbase'; // import { Camera as CameraIcon } from '@phosphor-icons/react/dist/ssr/Camera'; // import axios from 'axios'; import { Controller, useForm } from 'react-hook-form'; @@ -37,7 +40,9 @@ import { toast } from '@/components/core/toaster'; // import { getLessonTypeById, updateLessonType } from './http-actions'; // TODO: this may be wrong import type { LessonType } from './ILessonType'; -import type { LessonTypeEditFormProps } from './interfaces'; +import { defaultLessonType, type LessonTypeEditFormProps } from './interfaces'; + +const pb = new PocketBase(process.env.NEXT_PUBLIC_POCKETBASE_URL); // function fileToBase64(file: Blob): Promise { // return new Promise((resolve, reject) => { @@ -73,6 +78,7 @@ export function LessonTypeEditForm(): React.JSX.Element { const { t } = useTranslation(); const { typeId } = useParams<{ typeId: string }>(); const [isUpdating, setIsUpdating] = React.useState(false); + const [isLoading, setIsLoading] = React.useState(false); const { control, @@ -83,31 +89,29 @@ export function LessonTypeEditForm(): React.JSX.Element { // watch, } = useForm({ defaultValues, resolver: zodResolver(schema) }); - const onSubmit = React.useCallback( - async (values: Values): Promise => { - setIsUpdating(true); - const tempUpdate: LessonTypeEditFormProps = { - name: values.name, - type: values.type, - pos: values.pos, - visible: values.visible_to_user ? 'visible' : 'hidden', - }; + const onSubmit = React.useCallback(async (values: Values): Promise => { + setIsUpdating(true); + const tempUpdate: LessonTypeEditFormProps = { + name: values.name, + type: values.type, + pos: values.pos, + visible: values.visible_to_user ? 'visible' : 'hidden', + }; - // updateLessonType(tempUpdate, typeId) - // .then((res) => { - // logger.debug(res); - // toast.success(t('dashboard.lessonTypes.update.success')); - // setIsUpdating(false); - // router.push(paths.dashboard.lesson_types.list); - // }) - // .catch((err) => { - // logger.error(err); - // toast.error('Something went wrong!'); - // setIsUpdating(false); - // }); - }, - [router] - ); + pb.collection(COL_LESSON_TYPES) + .update(typeId, tempUpdate) + .then((res) => { + logger.debug(res); + toast.success(t('dashboard.lessonTypes.update.success')); + setIsUpdating(false); + router.push(paths.dashboard.lesson_types.list); + }) + .catch((err) => { + logger.error(err); + toast.error('Something went wrong!'); + setIsUpdating(false); + }); + }, []); const avatarInputRef = React.useRef(null); // const avatar = watch('avatar'); @@ -124,20 +128,30 @@ export function LessonTypeEditForm(): React.JSX.Element { [setValue] ); + const handleLoad = React.useCallback( + (id: string) => { + pb.collection(COL_LESSON_TYPES) + .getOne(id) + .then((lessonType: RecordModel) => { + reset({ ...defaultLessonType, ...lessonType }); + + setIsLoading(false); + }) + .catch((err) => { + logger.error(err); + toast(t('dashboard.lessonTypes.list.error')); + }); + }, + // eslint-disable-next-line react-hooks/exhaustive-deps + [typeId] + ); + React.useEffect(() => { - // getLessonTypeById(typeId) - // .then((lessonType: LessonType) => { - // reset({ - // name: lessonType.name, - // type: lessonType.type, - // pos: lessonType.pos, - // visible_to_user: lessonType.visible, - // }); - // }) - // .catch((err) => { - // // console.error(err); - // }); - }, []); + handleLoad(typeId); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [typeId]); + + if (isLoading) return <>loading; return (
@@ -223,7 +237,14 @@ export function LessonTypeEditForm(): React.JSX.Element { render={({ field }) => ( {t('dashboard.lessonTypes.edit.position')} - + { + field.onChange(parseInt(e.target.value)); + }} + type="number" + /> + {errors.pos ? {errors.pos.message} : null} )} @@ -237,8 +258,8 @@ export function LessonTypeEditForm(): React.JSX.Element { {t('dashboard.lessonTypes.edit.visibleToUser')} {errors.visible_to_user ? ( @@ -256,7 +277,7 @@ export function LessonTypeEditForm(): React.JSX.Element { - + {t('dashboard.lessonTypes.edit.updateButton')} diff --git a/002_source/cms/src/components/dashboard/lesson_type/lesson-types-table.tsx b/002_source/cms/src/components/dashboard/lesson_type/lesson-types-table.tsx index 63541b8..a725ff0 100644 --- a/002_source/cms/src/components/dashboard/lesson_type/lesson-types-table.tsx +++ b/002_source/cms/src/components/dashboard/lesson_type/lesson-types-table.tsx @@ -36,7 +36,7 @@ function columns(handleDeleteClick: (testId: string) => void): ColumnDef diff --git a/002_source/cms/src/constants.ts b/002_source/cms/src/constants.ts new file mode 100644 index 0000000..ad482d0 --- /dev/null +++ b/002_source/cms/src/constants.ts @@ -0,0 +1,3 @@ +const COL_LESSON_TYPES = 'LessonsTypes'; + +export { COL_LESSON_TYPES };