diff --git a/.gitignore b/.gitignore index a547bf3..463de42 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ dist-ssr *.njsproj *.sln *.sw? + +src/assets/*.json diff --git a/src/App.jsx b/src/App.jsx index fcc25f9..62d8a8b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,38 +1,139 @@ -import { useState } from 'react'; -import { House } from '@phosphor-icons/react'; +import { useState, useEffect, useRef } from 'react'; +import { ArrowRight, House, Trash } from '@phosphor-icons/react'; import { GREP_A_WORD } from './constants/options' import { OptionsRow } from './components/OptionsRow'; +import wordCountObject from './assets/wordCount.json'; + import styles from './App.module.css' import './global.css' export function App() { - const [selectOption, setSelectOption] = useState(''); + const [selectedOption, setSelectedOption] = useState(GREP_A_WORD); + const [inputValue, setInputValue] = useState(''); + const [wordCount, setWordCount] = useState(''); + const [isCleanFormButtonDisabled, setIsCleanFormButtonDisabled] = useState(true); + const [isSubmitButtonDisabled, setIsSubmitButtonDisabled] = useState(true); + + const inputRef = useRef(null); function handleHomeButtonClick() { - setSelectOption(''); + setSelectedOption(''); } + function handleInputValueChange() { + const inputStr = event.target.value; + + if(!event.target.value) { + setInputValue(''); + } + + if (!inputStr.match(/^[a-zA-Z0-9À-ú]+$/)) return; + + setInputValue(inputStr); + } + + function handleSubmitForm() { + event.preventDefault(); + + const inputValueStr = inputValue.toLowerCase(); + + if (!wordCountObject.hasOwnProperty(inputValueStr)) { + setWordCount('no'); + return; + } + + setWordCount(wordCountObject[inputValueStr]); + } + + function handleCleanForm() { + event.preventDefault(); + + setInputValue(''); + setWordCount(''); + inputRef.current.focus(); + } + + function onKeyUpInput() { + const enterKeyCharCode = 13; + if (event.keyCode !== enterKeyCharCode) { + return; + } + + if (inputValue === '') { + return; + } + + handleSubmitForm(); + } + + useEffect(() => { + if (inputValue === '') { + setIsSubmitButtonDisabled(true); + } else { + setIsSubmitButtonDisabled(false); + } + + }, [inputValue]) + + useEffect(() => { + if ([inputValue, wordCount].every((varStr) => varStr === '')) { + setIsCleanFormButtonDisabled(true); + } else { + setIsCleanFormButtonDisabled(false); + } + + }, [inputValue, wordCount]) + return (
My diary exposer
-Please select one of the options:
-foo
+ { !!! selectedOption && + <> +Please select one of the options:
+(it's case insensitive)
+There are
+{wordCount}
+occurrences of this word in my diary!
+