diff --git a/app/components/form.tsx b/app/components/form.tsx new file mode 100644 index 0000000..10c7ac1 --- /dev/null +++ b/app/components/form.tsx @@ -0,0 +1,47 @@ +'use client'; +import { useState } from "react"; + +const Form = ({ initialData, onSubmit }) => { + const [formData, setFormData] = useState( initialData ); + + const handleSubmit = (event) => { + event.preventDefault(); + + //q = formData; + + //addQuestion( q ); + + //console.log( `Adding question ${JSON.stringify(q)}` ); + + onSubmit( formData ); + } + + const handleChange = (event) => { + const { name, value } = event.target; + setFormData( (prevData) => ({ + ...prevData, + [name]: value + })); + } + + return ( +
+
+ {initialData.entries.map( (key, val) => { + return( + )})} +
+ +
+ ); +} + +export default Form; + diff --git a/app/lib/api.ts b/app/lib/api.ts new file mode 100644 index 0000000..4f656fe --- /dev/null +++ b/app/lib/api.ts @@ -0,0 +1,33 @@ +// the base URL of the api; all requests start with it +const base_url: string = "http://localhost:4000/"; + +/* +export const makeGetRequest = async (path: string): Promise => { + return await (await fetch( base_url + path )).json(); +} +*/ + +// generic function that makes a web request +const makeRequest = async (path: string, method: string, body?: string): Promise => { + console.log( `making ${method} request: ${base_url + path}, ${body}` ); + return fetch( base_url + path, { method: method, body: body ? body : null } ); +} + +// the following functions are wrappers around `makeRequest`, +// for every type of request that's needed. + +export const makeGetRequest = async (path: string): Promise => { + return (await makeRequest( path, "GET" )).json(); +} + +export const makePostRequest = async (path: string, body: T): Promise => { + return (await makeRequest( path, "POST", JSON.stringify( body ) )).json(); +} + +export const makePutRequest = async (path: string, body: T): Promise => { + return (await makeRequest( path, "PUT", JSON.stringify( body ) )).json(); +} + +export const makeDeleteRequest = async (path: string): Promise => { + return (await makeRequest( path, "DELETE" )).json(); +} \ No newline at end of file diff --git a/app/lib/question.ts b/app/lib/question.ts new file mode 100644 index 0000000..0420798 --- /dev/null +++ b/app/lib/question.ts @@ -0,0 +1,33 @@ +import { makeDeleteRequest, makeGetRequest, makePostRequest } from "./api"; + +export const enum QuizQuestion { + UL, + UR, + DL, + DR +}; + +export type Question = { + id: number; + category: string | null; + type: string | null; // TODO: make a TS type for every question type + text: string; + answer: string | boolean | QuizQuestion; +} + +export const getQuestion = async (id: number): Promise => { + return makeGetRequest( `questions/${id}/` ); +} + +export const addQuestion = async (question: Question): Promise => { + console.log( `adding question ${JSON.stringify( question)}` ); + return makePostRequest( `questions`, question ); +} + +export const editQuestion = async (question: Question): Promise => { + return makePostRequest( `questions/${question.id}`, question ); +} + +export const deleteQuestion = async (id: number): Promise => { + return makeDeleteRequest( `questions/${id}` ); +} \ No newline at end of file diff --git a/app/lib/session.ts b/app/lib/session.ts new file mode 100644 index 0000000..2a81c3b --- /dev/null +++ b/app/lib/session.ts @@ -0,0 +1,17 @@ +import { makeGetRequest, makePostRequest } from "./api"; + +export type Session = { + id: number; + right: number; + wrong: number; + total: number; + // user: string; +} + +export const getSession = async (id: number): Promise => { + return makeGetRequest( `sessions/${id}/` ); +} + +export const postSession = async (session: Session): Promise => { + return makePostRequest( `sessions`, session ); +} \ No newline at end of file