-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71ff29c
commit 7ad79c8
Showing
4 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React 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 ( | ||
<span style={{ color: isError ? 'red' : 'black', fontWeight: isError ? 'bold' : 'normal' }}> | ||
{word + ' '} | ||
</span> | ||
) | ||
} | ||
|
||
const GrammarCheck = () => { | ||
const dispatch = useDispatch() | ||
const { essay, edits, pending } = useSelector(state => state.gec) | ||
|
||
const handleEssayChange = (event) => { | ||
const newValue = event.target.value | ||
dispatch(updateEssay(newValue)) | ||
} | ||
|
||
const handleGrammarCheck = () => { | ||
if (essay.trim() !== "") { | ||
dispatch(checkGrammar(essay)) | ||
} | ||
} | ||
|
||
const renderTextWithHighlights = (text, edits) => { | ||
const words = text.split(' ') | ||
return words.map((word, index) => { | ||
const isError = edits && edits.some(edit => edit.word === word) | ||
return <Word key={index} word={word} isError={isError} /> | ||
}) | ||
} | ||
|
||
return ( | ||
<div style={{ padding: '20px', display: 'flex', flexDirection: 'column', height: '100vh' }}> | ||
<textarea | ||
style={{ | ||
height: '150px', | ||
borderColor: '#ccc', | ||
borderWidth: '1px', | ||
padding: '10px', | ||
fontSize: '16px', | ||
marginBottom: '20px', | ||
width: '100%', | ||
resize: 'vertical', | ||
}} | ||
value={essay} | ||
onChange={handleEssayChange} | ||
/> | ||
<button | ||
style={{ | ||
backgroundColor: '#007BFF', | ||
padding: '15px', | ||
borderRadius: '5px', | ||
color: 'white', | ||
fontSize: '16px', | ||
border: 'none', | ||
cursor: 'pointer', | ||
}} | ||
onClick={handleGrammarCheck} | ||
disabled={pending} // Disable button when pending | ||
> | ||
{pending ? '...' : <FormattedMessage id="check-gec-grammar" />} | ||
</button> | ||
|
||
{/* <div style={{ marginTop: '20px', borderColor: '#ccc', borderWidth: '1px', padding: '10px', height: '200px', overflowY: 'auto' }}> | ||
{pending ? ( | ||
<span>Checking...</span> | ||
) : ( | ||
<span> | ||
{renderTextWithHighlights(essay, edits)} | ||
</span> | ||
)} | ||
</div> */} | ||
</div> | ||
) | ||
} | ||
|
||
export default GrammarCheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import callBuilder from '../apiConnection' | ||
|
||
export const checkGrammar = (essay) => { | ||
const route = '/gec' | ||
const prefix = 'CHECK_GRAMMAR' | ||
const payload = { essay } | ||
return callBuilder(route, prefix, 'post', payload) // Use POST method | ||
} | ||
|
||
export const updateEssay = (text) => ({ | ||
type: 'UPDATE_ESSAY', | ||
payload: text, | ||
}); | ||
|
||
|
||
const initialState = { | ||
essay: "", // Essay input by the user | ||
edits: null, // Grammar corrections received | ||
pending: false, // Loading state | ||
error: null, // Error message if request fails | ||
} | ||
|
||
export default (state = initialState, action) => { | ||
switch (action.type) { | ||
case 'CHECK_GRAMMAR_ATTEMPT': | ||
return { | ||
...state, | ||
pending: true, | ||
error: null, | ||
} | ||
case 'CHECK_GRAMMAR_SUCCESS': | ||
return { | ||
...state, | ||
edits: action.response.edits, | ||
pending: false, | ||
error: null, | ||
} | ||
case 'CHECK_GRAMMAR_FAILURE': | ||
return { | ||
...state, | ||
pending: false, | ||
error: action.error, | ||
} | ||
case 'UPDATE_ESSAY': | ||
return { | ||
...state, | ||
essay: action.payload, | ||
} | ||
case 'CLEAR_EDITS': | ||
return { | ||
...state, | ||
edits: null, | ||
} | ||
default: | ||
return state | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters