From b589ad8553dc104e36673f3fa2935e547ce10d72 Mon Sep 17 00:00:00 2001 From: Nuwan Date: Thu, 9 Feb 2023 09:35:29 +0530 Subject: [PATCH] working on new session screen --- .../src/components/page/JKNewMusicSession.js | 59 +++++---- jam-ui/src/hooks/useAutocomplete.js | 118 ++++++++++++++++++ 2 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 jam-ui/src/hooks/useAutocomplete.js diff --git a/jam-ui/src/components/page/JKNewMusicSession.js b/jam-ui/src/components/page/JKNewMusicSession.js index 08d23125d..333c6808c 100644 --- a/jam-ui/src/components/page/JKNewMusicSession.js +++ b/jam-ui/src/components/page/JKNewMusicSession.js @@ -1,10 +1,11 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { Form, FormGroup, Input, Label, Text, Card, CardBody, Button } from 'reactstrap'; import FalconCardHeader from '../common/FalconCardHeader'; import Flex from '../common/Flex'; import JKTooltip from '../common/JKTooltip'; import { useTranslation } from 'react-i18next'; import AsyncSelect from 'react-select/async'; +//import useAutoComplete from '../../hooks/useAutocomplete'; import { useAuth } from '../../context/UserAuth'; import { getFriends } from '../../helpers/rest'; import Avatar from '../common/Avatar'; @@ -51,18 +52,26 @@ const JKNewMusicSession = () => { return matches; }; + const handleSubmit = (event) => { + event.preventDefault() + const formData = new FormData(event.target) + console.log(formData.get('privacy')); + console.log(formData.get('friendIds')); + console.log(formData.get('description')); + } + return (
-
+ - + @@ -74,26 +83,25 @@ const JKNewMusicSession = () => { Session Invitations{' '} - - (e.inputValue ? 'No options' : null)} - components={{ - Option: CustomOption, - DropdownIndicator: () => null, - IndicatorSeparator: () => null - }} - styles={{ - control: (baseStyles, state) => ({ - ...baseStyles, - borderRadius: '1.25em' - }) - }} - /> - - + + (e.inputValue ? 'No options' : null)} + components={{ + Option: CustomOption, + DropdownIndicator: () => null, + IndicatorSeparator: () => null + }} + styles={{ + control: (baseStyles, state) => ({ + ...baseStyles, + borderRadius: '1.25em' + }) + }} + /> @@ -102,11 +110,16 @@ const JKNewMusicSession = () => { + + + +
diff --git a/jam-ui/src/hooks/useAutocomplete.js b/jam-ui/src/hooks/useAutocomplete.js new file mode 100644 index 000000000..e3443799e --- /dev/null +++ b/jam-ui/src/hooks/useAutocomplete.js @@ -0,0 +1,118 @@ +import React, { useRef, useState } from 'react'; + +const KEY_CODES = { + DOWN: 40, + UP: 38, + PAGE_DOWN: 34, + ESCAPE: 27, + PAGE_UP: 33, + ENTER: 13 +}; + +export default function useAutoComplete({ delay = 500, source, onChange }) { + const [myTimeout, setMyTimeOut] = useState(setTimeout(() => {}, 0)); + const listRef = useRef(); + const [suggestions, setSuggestions] = useState([]); + const [isBusy, setBusy] = useState(false); + const [selectedIndex, setSelectedIndex] = useState(-1); + const [textValue, setTextValue] = useState(''); + + function delayInvoke(cb) { + if (myTimeout) { + clearTimeout(myTimeout); + } + setMyTimeOut(setTimeout(cb, delay)); + } + + function selectOption(index) { + if (index > -1) { + onChange(suggestions[index]); + setTextValue(suggestions[index].label); + } + clearSuggestions(); + } + + async function getSuggestions(searchTerm) { + if (searchTerm && source) { + const options = await source(searchTerm); + setSuggestions(options); + } + } + + function clearSuggestions() { + setSuggestions([]); + setSelectedIndex(-1); + } + + function onTextChange(searchTerm) { + setBusy(true); + setTextValue(searchTerm); + clearSuggestions(); + delayInvoke(() => { + getSuggestions(searchTerm); + setBusy(false); + }); + } + + const optionHeight = listRef?.current?.children[0]?.clientHeight; + + function scrollUp() { + if (selectedIndex > 0) { + setSelectedIndex(selectedIndex - 1); + } + //listRef?.current?.scrollTop -= optionHeight + } + + function scrollDown() { + if (selectedIndex < suggestions.length - 1) { + setSelectedIndex(selectedIndex + 1); + } + //listRef?.current?.scrollTop = selectedIndex * optionHeight + } + + function pageDown() { + setSelectedIndex(suggestions.length - 1); + //listRef?.current?.scrollTop = suggestions.length * optionHeight + } + + function pageUp() { + setSelectedIndex(0); + //listRef?.current?.scrollTop = 0 + } + + function onKeyDown(e) { + const keyOperation = { + [KEY_CODES.DOWN]: scrollDown, + [KEY_CODES.UP]: scrollUp, + [KEY_CODES.ENTER]: () => selectOption(selectedIndex), + [KEY_CODES.ESCAPE]: clearSuggestions, + [KEY_CODES.PAGE_DOWN]: pageDown, + [KEY_CODES.PAGE_UP]: pageUp + }; + if (keyOperation[e.keyCode]) { + keyOperation[e.keyCode](); + } else { + setSelectedIndex(-1); + } + } + + return { + bindOption: { + onClick: e => { + let nodes = Array.from(listRef.current.children); + selectOption(nodes.indexOf(e.target.closest('li'))); + } + }, + bindInput: { + value: textValue, + onChange: e => onTextChange(e.target.value), + onKeyDown + }, + bindOptions: { + ref: listRef + }, + isBusy, + suggestions, + selectedIndex + }; +}