Skip to content

Commit

Permalink
add gec view
Browse files Browse the repository at this point in the history
  • Loading branch information
ducvugithub committed Sep 17, 2024
1 parent 71ff29c commit 7ad79c8
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 7 deletions.
82 changes: 82 additions & 0 deletions client/components/GecView/index.js
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
9 changes: 2 additions & 7 deletions client/components/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import ReferenceView from './ReferenceView'
import EditStoryView from './EditStoryView'
import LessonPracticeView from './Lessons/LessonPracticeView'
import LessonLibrary from './Lessons/LessonLibrary'
import GecView from './GecView'

export default () => {
const userData = useSelector(state => state.user?.data?.user)
Expand All @@ -58,7 +59,6 @@ export default () => {
const dispatch = useDispatch()

useEffect(() => {
// This closes and opens recommendations on page changes
dispatch(closeEncouragement())
dispatch(closeFCEncouragement())
if (
Expand Down Expand Up @@ -147,11 +147,6 @@ export default () => {
<ProtectedRoute exact path="/stories/:id/group/review" component={ReadViews} />
<ProtectedRoute exact path="/stories/:id/group/preview" component={ReadViews} />

{/* <ProtectedRoute
exact
path="/stories/:id/preview-controlled-story/"
component={ControlledStoryPreviewView}
/> */}
<ProtectedRoute exact path="/stories/:id/compete/" component={CompeteView} />
<ProtectedRoute
exact
Expand Down Expand Up @@ -186,7 +181,7 @@ export default () => {
<ProtectedRoute exact path="/test-construction" component={ConstructTestView} />
<ProtectedRoute exact path="/test-debug" component={DebugTestView} />
<ProtectedRoute exact path="/vocabulary-view" component={VocabularyView} />

<ProtectedRoute exact path="/gec" component={GecView} />
<ProtectedRoute exact path="/reference" component={ReferenceView} />
</Switch>
</main>
Expand Down
57 changes: 57 additions & 0 deletions client/util/redux/gecReducer.js
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
}
}
3 changes: 3 additions & 0 deletions client/util/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import levelUp from './levelUpReducer'
import activity from './activityReducer'
import contextTranslation from './contextTranslationReducer'
import chatbot from './chatbotReducer'
import gec from './gecReducer'


const rootReducer = combineReducers({
stories,
Expand Down Expand Up @@ -108,6 +110,7 @@ const rootReducer = combineReducers({
contextTranslation,
activity,
chatbot,
gec,
})

export default (state, action) =>
Expand Down

0 comments on commit 7ad79c8

Please sign in to comment.