diff --git a/client/components/GecView/index.js b/client/components/GecView/index.js index dc5ef2fe..30bdf44a 100644 --- a/client/components/GecView/index.js +++ b/client/components/GecView/index.js @@ -1,12 +1,52 @@ -import React from 'react' +import React, { useState } from 'react' import { FormattedMessage } from 'react-intl' import { useDispatch, useSelector } from 'react-redux' import { checkGrammar, updateEssay } from 'Utilities/redux/gecReducer' -const Word = ({ word, isError }) => { - return ( - - {word + ' '} +const Word = ({ word, isError, edit, index }) => { + const [hovered, setHovered] = useState(false) + + return isError ? ( + setHovered(true)} + onMouseLeave={() => setHovered(false)} + > + + {hovered && ( + + {edit.type} + + )} + + ) : ( + + {word} ) } @@ -14,42 +54,87 @@ const Word = ({ word, isError }) => { const GrammarCheck = () => { const dispatch = useDispatch() const { essay, edits, pending } = useSelector(state => state.gec) + const [submitted, setSubmitted] = useState(false) - const handleEssayChange = (event) => { + const handleEssayChange = event => { const newValue = event.target.value dispatch(updateEssay(newValue)) } const handleGrammarCheck = () => { - if (essay.trim() !== "") { + if (essay.trim() !== '') { dispatch(checkGrammar(essay)) + setSubmitted(true) } } const renderTextWithHighlights = (text, edits) => { const words = text.split(' ') - return words.map((word, index) => { - const isError = edits && edits.some(edit => edit.word === word) - return - }) + let modifiedText = [] + let currentEditIndex = 0 + let currentWordIndex = 0 + + // Find matching edits and insert editable inputs + while (currentWordIndex < words.length) { + const word = words[currentWordIndex] + const currentEdit = edits[currentEditIndex] + const o_start = currentEdit ? currentEdit.o_start : null + const o_end = currentEdit ? currentEdit.o_end : null + + // Check if the current word is in the range of the edit + if (currentEdit && currentWordIndex >= o_start && currentWordIndex < o_end) { + // Wrap the word in an input box if it's within the edit range + modifiedText.push( + + ) + + if (currentWordIndex + 1 === o_end) { + currentEditIndex++ + } + } else { + // Regular word (no edit) + modifiedText.push( + + {word + ' '} + + ) + } + + currentWordIndex++ + } + + return modifiedText } return (
-