diff --git a/client/package.json b/client/package.json index e7f5b4a..b4dc829 100644 --- a/client/package.json +++ b/client/package.json @@ -9,6 +9,9 @@ "preview": "vite preview" }, "dependencies": { + "@emotion/react": "^11.10.5", + "@emotion/styled": "^11.10.5", + "@mui/material": "^5.11.9", "@tinymce/tinymce-react": "^4.2.0", "axios": "^1.2.0", "jwt-decode": "^3.1.2", diff --git a/client/src/components/pages/AddRecipe.tsx b/client/src/components/pages/AddRecipe.tsx index 15a43ac..ebd5a40 100644 --- a/client/src/components/pages/AddRecipe.tsx +++ b/client/src/components/pages/AddRecipe.tsx @@ -5,8 +5,9 @@ import { IIngredient, IRecipe } from "../../schemas"; import API from "../../util/API"; import Creatable from "react-select/creatable"; import { OptionType } from "../../util/types"; -import { useSelectorContext } from "../../context/SelectorContext"; +import { createOptionFromText, useSelectorContext } from "../../context/SelectorContext"; import { MultiValue } from "react-select"; +import { Autocomplete, Chip, TextField } from "@mui/material"; const AddRecipe = () => { const { user, token } = useAuthContext(); @@ -17,6 +18,7 @@ const AddRecipe = () => { } = useSelectorContext(); const [triggerChange, setTriggerChange] = useState(false); + const [optionCount, setOptionCount] = useState(0); const [form, setForm] = useState(); const [toast, setToast] = useState(<>) const [input, setInput] = useState({ name: '', preptime: '', description: '', authoruserid: '' }) @@ -41,6 +43,8 @@ const AddRecipe = () => { setOptions(result.map((each: IIngredient) => { return { label: each.name, value: each.id } })); + + setOptionCount(result.length); } })(); }, [token]) @@ -51,7 +55,35 @@ const AddRecipe = () => { useEffect(() => { if (data.length) { - console.log('caught'); + const autocompleteInstance = ( + each.label)} + onKeyDown={(e) => { + if (e.code == 'Enter') { + const inputVal: string = e.target['value' as keyof EventTarget].toString(); + const newOption = createOptionFromText(inputVal, optionCount + 1); + setOptions((prev) => [...prev, newOption]); + setOptionCount(prev => prev + 1); + } + }} + renderTags={(value, getTagProps) => value.map((option, idx) => handleDelete(target)} />)} + renderInput={(params) => ( + + )} + /> + ) + // create dropdown from new data const selectorInstance = { onChange={(selection: MultiValue) => onChange(selection)} onCreateOption={(input: string) => onCreateOption(input, () => {})} /> - data.length && setSelector(selectorInstance); + data.length && setSelector(autocompleteInstance); setTriggerChange(true); } }, [data, options]) @@ -83,6 +115,10 @@ const AddRecipe = () => { ) }, [triggerChange]) + useEffect(() => { + console.log(options); + }, [options]) + // once user information is available, store it in recipe data useEffect(() => { if (!user) return; @@ -99,6 +135,12 @@ const AddRecipe = () => { setInput(data); }, [input]) + const handleDelete = (target: string) => { + setOptions((prev) => { + return prev.filter(option => option.label !== target); + }) + } + // submit handler const handleCreate = async () => { if (!token) return; diff --git a/client/src/context/SelectorContext.tsx b/client/src/context/SelectorContext.tsx index 8b4d80f..a2b871f 100644 --- a/client/src/context/SelectorContext.tsx +++ b/client/src/context/SelectorContext.tsx @@ -27,5 +27,12 @@ const defaultValue: SelectorContextProps = { onCreateOption: (label: string) => {}, } +export function createOptionFromText(label: string, value?: number): OptionType { + return { + value: value || 0, + label: label, + } +} + export const SelectorContext = createContext>(defaultValue); export const useSelectorContext = () => useContext(SelectorContext); \ No newline at end of file