From 43db39291ea6be6abfc652282643222ae5820bf2 Mon Sep 17 00:00:00 2001 From: Alex Stan Date: Sat, 1 Jun 2024 17:40:01 +0300 Subject: [PATCH] added a dynamic category page Signed-off-by: Alex Stan --- app/components/form.tsx | 70 +++++++++++++++++++---------------- app/learn/[category]/page.tsx | 67 +++++++++++++++++++++++++++++++++ app/lib/session.ts | 7 ++++ 3 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 app/learn/[category]/page.tsx diff --git a/app/components/form.tsx b/app/components/form.tsx index bf5a932..6b9fdc0 100644 --- a/app/components/form.tsx +++ b/app/components/form.tsx @@ -1,43 +1,49 @@ 'use client'; import { useState } from "react"; -const Form = ({ initialData, submitFunction }) => { - const [formData, setFormData] = useState( initialData ); +export enum FieldType { + number = 'number', + text = 'text' +} - const handleSubmit = async (event) => { +export type FormFields = { + type: FieldType, + name: string, + label: string, + required: boolean +} + +const Form = ({ fields, onSubmit }) => { + const [formData, setFormData] = useState({}); + + const handleChange = (event) => { + const { name, value } = event.target; + setFormData( (prevData) => ({ + ...prevData, + [name]: value + })); + } + + const handleSubmit = (event) => { event.preventDefault(); - - //q = formData; - - //addQuestion( q ); - - //console.log( `Adding question ${JSON.stringify(q)}` ); - - await submitFunction( formData ); - } - - const handleChange = (event) => { - const { name, value } = event.target; - setFormData( (prevData) => ({ - ...prevData, - [name]: value - })); + onSubmit( formData ); } return ( -
-
- {Object.entries( initialData ).map( (elem) => { - return( - )})} -
+ + {fields.map( (field) => { + return ( +
+ + +
+ )})}
); diff --git a/app/learn/[category]/page.tsx b/app/learn/[category]/page.tsx new file mode 100644 index 0000000..44d7239 --- /dev/null +++ b/app/learn/[category]/page.tsx @@ -0,0 +1,67 @@ +'use client'; +import { useState } from 'react' +import { Dialog, DialogPanel } from '@headlessui/react' +import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline' + +const Page = ({ params }: { params: { category: string }}) => { + const [mobileMenuOpen, setMobileMenuOpen] = useState(false) + + return ( +
+ +
+
+ + ) +} + +export default Page; diff --git a/app/lib/session.ts b/app/lib/session.ts index 2a81c3b..775e3df 100644 --- a/app/lib/session.ts +++ b/app/lib/session.ts @@ -8,6 +8,13 @@ export type Session = { // user: string; } +export type SessionState = { + answered: number; + wrong: number; + right: number; + category: string; +} + export const getSession = async (id: number): Promise => { return makeGetRequest( `sessions/${id}/` ); }