From 1916c813aa37127ee730c6f68518a9162c9de2a4 Mon Sep 17 00:00:00 2001 From: Anurag Roy Date: Mon, 6 Mar 2023 23:56:03 +0530 Subject: [PATCH] Update exit form inputs --- package-lock.json | 22 ++++++++ package.json | 2 + src/components/ComboBoxInputs.tsx | 94 +++++++++++++++++++++++++++++++ src/components/exit/ExitForm.tsx | 65 +++++++++++++++------ src/pages/api/getStocks.ts | 13 +++++ src/pages/api/placeOrder.ts | 4 +- 6 files changed, 180 insertions(+), 20 deletions(-) create mode 100644 src/components/ComboBoxInputs.tsx create mode 100644 src/pages/api/getStocks.ts diff --git a/package-lock.json b/package-lock.json index c7b661f..6adb7c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@headlessui/react": "^1.7.13", "@heroicons/react": "^2.0.16", "kiteconnect-ts": "^0.4.5", + "lodash.startcase": "^4.4.0", "next": "13.2.1", "next-themes": "^0.2.1", "react": "18.2.0", @@ -19,6 +20,7 @@ }, "devDependencies": { "@tailwindcss/forms": "^0.5.3", + "@types/lodash.startcase": "^4.4.7", "@types/node": "^18.14.1", "@types/react": "^18.0.28", "@types/react-dom": "^18.0.11", @@ -306,6 +308,21 @@ "tailwindcss": ">=3.0.0 || >= 3.0.0-alpha.1" } }, + "node_modules/@types/lodash": { + "version": "4.14.191", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz", + "integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==", + "dev": true + }, + "node_modules/@types/lodash.startcase": { + "version": "4.4.7", + "resolved": "https://registry.npmjs.org/@types/lodash.startcase/-/lodash.startcase-4.4.7.tgz", + "integrity": "sha512-6v8FVOcfxdomO1Vammc1Zsah7/4aif/Lx16oQQ0WZmKVGF/Yf5c5m68LqI/ELOhKaKUr8KfnDdKrytdrThoRQw==", + "dev": true, + "dependencies": { + "@types/lodash": "*" + } + }, "node_modules/@types/node": { "version": "18.14.1", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.14.1.tgz", @@ -878,6 +895,11 @@ "node": ">=10" } }, + "node_modules/lodash.startcase": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/lodash.startcase/-/lodash.startcase-4.4.0.tgz", + "integrity": "sha512-+WKqsK294HMSc2jEbNgpHpd0JfIBhp7rEV4aqXWqFr6AlXov+SlcgB1Fv01y2kGe3Gc8nMW7VA0SrGuSkRfIEg==" + }, "node_modules/loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", diff --git a/package.json b/package.json index 2a21ebf..01d8ab1 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "@headlessui/react": "^1.7.13", "@heroicons/react": "^2.0.16", "kiteconnect-ts": "^0.4.5", + "lodash.startcase": "^4.4.0", "next": "13.2.1", "next-themes": "^0.2.1", "react": "18.2.0", @@ -18,6 +19,7 @@ }, "devDependencies": { "@tailwindcss/forms": "^0.5.3", + "@types/lodash.startcase": "^4.4.7", "@types/node": "^18.14.1", "@types/react": "^18.0.28", "@types/react-dom": "^18.0.11", diff --git a/src/components/ComboBoxInputs.tsx b/src/components/ComboBoxInputs.tsx new file mode 100644 index 0000000..2647a84 --- /dev/null +++ b/src/components/ComboBoxInputs.tsx @@ -0,0 +1,94 @@ +import { cx } from '@/utils/ui'; +import { Combobox } from '@headlessui/react'; +import { CheckIcon, ChevronDownIcon } from '@heroicons/react/24/solid'; +import startCase from 'lodash.startcase'; +import { Dispatch, SetStateAction, useState } from 'react'; + +type ComboBoxProps = { + name: string; + items: string[]; + selectedItem?: string; + setSelectedItem?: Dispatch>; +}; + +export function ComboBoxInput({ + name, + items, + selectedItem, + setSelectedItem, +}: ComboBoxProps) { + const [query, setQuery] = useState(''); + const filteredItems = + query === '' + ? items + : items.filter((i) => i.toLowerCase().includes(query.toLowerCase())); + return ( + + + {startCase(name)} + +
+ setQuery(event.target.value)} + /> + + + + {filteredItems.length > 0 && ( + + {filteredItems.map((s) => ( + + cx( + 'relative cursor-default select-none py-2 pl-3 pr-9', + active + ? 'bg-blue-600 text-white' + : 'text-zinc-900 dark:text-zinc-100' + ) + } + > + {({ active, selected }) => ( + <> + + {s} + + + {selected && ( + + + )} + + )} + + ))} + + )} +
+
+ ); +} diff --git a/src/components/exit/ExitForm.tsx b/src/components/exit/ExitForm.tsx index f40cfae..e7ee841 100644 --- a/src/components/exit/ExitForm.tsx +++ b/src/components/exit/ExitForm.tsx @@ -1,6 +1,7 @@ import { Instrument } from '@/types'; import { getExpiryOptions } from '@/utils/ui'; -import { FormEvent, useState } from 'react'; +import { FormEvent, useEffect, useState } from 'react'; +import { ComboBoxInput } from '../ComboBoxInputs'; import { ExitTable } from './ExitTable'; const expiryOptions = getExpiryOptions(); @@ -10,6 +11,14 @@ export function ExitForm() { const [instruments, setInstruments] = useState([]); const [entryBasis, setEntryBasis] = useState(0); + const [stocks, setStocks] = useState([]); + + useEffect(() => { + fetch('/api/getStocks') + .then((res) => res.json()) + .then((response) => setStocks(response)); + }, []); + const handleFormSubmit = (event: FormEvent) => { event.preventDefault(); @@ -38,20 +47,38 @@ export function ExitForm() { className="mb-12 rounded-lg rounded-t-none py-6 bg-zinc-50 dark:bg-zinc-800 dark:bg-white/5 border border-t-0 border-zinc-200 dark:border-white/10 flex justify-between items-end px-8" onSubmit={handleFormSubmit} > +
+ +
+
+
- +
+ +
+