diff --git a/client/package.json b/client/package.json index 148c6da..e7f5b4a 100644 --- a/client/package.json +++ b/client/package.json @@ -15,6 +15,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.4.3", + "react-select": "^5.7.0", "sass": "^1.56.1", "uuid": "^9.0.0" }, diff --git a/client/src/App.tsx b/client/src/App.tsx index 0d2c4ea..7d48201 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -23,6 +23,7 @@ import { TokenType } from './util/types'; import './sass/App.scss'; import handleToken from './util/handleToken'; import AddFriends from './components/pages/AddFriends'; +import Sandbox from './components/Sandbox'; function App() { const { setUser, token, setToken } = useAuthContext(); @@ -64,6 +65,7 @@ function App() { } /> } /> } /> + } /> diff --git a/client/src/components/Sandbox.tsx b/client/src/components/Sandbox.tsx new file mode 100644 index 0000000..fe314e2 --- /dev/null +++ b/client/src/components/Sandbox.tsx @@ -0,0 +1,97 @@ +import Select, { MultiValue } from "react-select"; +import Creatable, { useCreatable } from "react-select/creatable"; +import { useEffect, useState } from "react" +import { useAuthContext } from "../context/AuthContext"; +import { ICollection, IIngredient } from "../schemas"; +import { Button, Page } from "./ui"; +import API from "../util/API"; + +interface OptionType { + value: number + label: string +} + +export default function Sandbox() { + const [ingredients, setIngredients] = useState>([]); + const [collections, setCollections] = useState>([]); + const [newEntries, setNewEntries] = useState>([]); + const [selections, setSelections] = useState>([]); + const { user, token } = useAuthContext(); + + function handleNewIngredient(name: string) { + if (!user || !user.id) return; + + let maxID = 0; + ingredients.forEach((entry: IIngredient) => { + if (entry.id! > maxID) { + maxID = entry.id!; + } + }); + + const newEntry: OptionType = { + label: name, + value: maxID + 1 + } + + const newIngredient: IIngredient = { + id: maxID + 1, + name: name, + createdbyid: user.id + } + + setIngredients(prev => [...prev, newIngredient]); + setNewEntries(prev => [...prev, newEntry]); + } + + function handleChange(event: MultiValue) { + setSelections(prev => [...prev, ...event]) + } + + async function bulkInsertIngredients() { + if (!user || !token) return; + + console.log(newEntries.length - ingredients.length); + } + + useEffect(() => { + token && (async() => { + const ingredients = new API.Ingredient(token); + const collections = new API.Collection(token); + + const allIngredients = await ingredients.getAll(); + if (allIngredients) { + setNewEntries(prev => [...prev, ...allIngredients]); + setIngredients(allIngredients.map((each: IIngredient) => { + return { label: each.name, value: each.id } + })); + } + + const myCollections = await collections.getAllAuthored(); + if (myCollections) setCollections(myCollections.map((each: ICollection) => { + return { label: each.name, value: each.id } + })) + })(); + }, [token]) + + useEffect(() => { + console.log(newEntries); + }, [newEntries]) + + return ( + +

Sandbox

+ +

Ingredients:

+ + { ingredients + ? ) => handleChange(value)} onCreateOption={handleNewIngredient} options={newEntries} /> + :

Loading...

+ } + +

My collections:

+ { collections ? +
+ + + +
) } diff --git a/client/src/sass/components/Form.scss b/client/src/sass/components/Form.scss index f14eeef..d173348 100644 --- a/client/src/sass/components/Form.scss +++ b/client/src/sass/components/Form.scss @@ -17,6 +17,11 @@ border-radius: 4px; width: 65%; } + + select { + width: 65%; + margin: 0; + } } .form-row-editor { diff --git a/client/src/util/API.ts b/client/src/util/API.ts index 0af8f5f..9ef77c8 100644 --- a/client/src/util/API.ts +++ b/client/src/util/API.ts @@ -186,7 +186,7 @@ module API { export class Ingredient extends RestController { constructor(token: string) { - super(Settings.getAPISTRING() + "/app/ingredients", token); + super(Settings.getAPISTRING() + "/app/ingredient", token); } }