Skip to content

Commit f0cf5ed

Browse files
committed
releasing field works but automatic textarea update is not working --refs #1170
1 parent f02a5d8 commit f0cf5ed

File tree

5 files changed

+51
-48
lines changed

5 files changed

+51
-48
lines changed

client/components/V1/Generic/TextFieldComponent.tsx

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { useState, useEffect } from 'react'
1+
import { useState, useEffect, useRef } from 'react'
22
import { TextField, Button, Box } from '@mui/material'
33
import { useTranslation } from 'react-i18next'
44
import { useDispatch, useSelector } from 'react-redux'
55
import ReactMarkdown from 'react-markdown'
6-
import { getLockHttp } from '../../../util/redux/formReducer'
6+
import { clearFormState, getLockHttp, updateFormField } from '../../../util/redux/formReducer'
77
import { RootState } from '../../../util/store'
8-
import { wsJoinRoom } from '../../../util/redux/websocketReducer'
98
import { releaseFieldLocally } from '../../../util/redux/currentEditorsReducer'
109
import { deepCheck } from '../../Generic/Textarea'
11-
import { updateReportHttp, getReports } from '../../../util/redux/reportsReducer'
10+
import { updateReportHttp } from '../../../util/redux/reportsReducer'
1211

13-
const TextFieldComponent = ({ id }: { id: string }) => {
12+
const TextFieldComponent = ({ id, type }: { id: string, type: string }) => {
1413
const { t } = useTranslation()
1514
const dispatch = useDispatch()
1615

@@ -22,12 +21,6 @@ const TextFieldComponent = ({ id }: { id: string }) => {
2221
const dataFromRedux = useSelector(({ form }: { form: any }) => form.data[id] || '')
2322
const currentEditors = useSelector(({ currentEditors }: { currentEditors: any }) => currentEditors.data, deepCheck)
2423
const currentUser = useSelector(({ currentUser }: { currentUser: any }) => currentUser.data)
25-
const form = 10
26-
27-
useEffect(() => {
28-
dispatch(getReports('KH50_005'))
29-
dispatch(wsJoinRoom('KH50_005', form))
30-
}, [])
3124

3225
useEffect(() => {
3326
const gotTheLock = (currentEditors && currentEditors[id] && currentEditors[id].uid === currentUser.uid)
@@ -54,34 +47,34 @@ const TextFieldComponent = ({ id }: { id: string }) => {
5447
}
5548
}
5649

57-
const handleStartEditing = () => {
58-
askForLock()
59-
}
60-
6150
return (
6251
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2, alignItems: 'start' }}>
63-
<TextField
64-
disabled={!hasLock}
65-
type="text"
66-
defaultValue={content}
67-
variant="outlined"
68-
multiline
69-
minRows={8}
70-
fullWidth
71-
label="Testattava tekstikenttä"
72-
value={content}
73-
onChange={e => setContent(e.target.value)}
74-
/>
7552
{hasLock ? (
76-
<Button variant="contained" onClick={handleStopEditing}>
77-
{t('stopEditing')}
78-
</Button>
53+
<>
54+
<TextField
55+
type="text"
56+
defaultValue={content}
57+
variant="outlined"
58+
multiline
59+
minRows={type === 'comment' ? 3 : 10}
60+
fullWidth
61+
label="Koulutusohjelman kommentti"
62+
value={content}
63+
onChange={e => setContent(e.target.value)}
64+
onClick={askForLock}
65+
/>
66+
<Button variant="outlined" onClick={handleStopEditing}>
67+
{t('generic:kludgeButton')}
68+
</Button>
69+
</>
7970
) : (
80-
<Button variant="contained" onClick={handleStartEditing}>
81-
{t('edit')}
82-
</Button>
71+
<>
72+
<ReactMarkdown>{content}</ReactMarkdown>
73+
<Button variant="outlined" onClick={askForLock}>
74+
{t('edit')}
75+
</Button>
76+
</>
8377
)}
84-
<ReactMarkdown>{content}</ReactMarkdown>
8578
</Box>
8679
)
8780
}

client/components/V1/ProgrammeView/ProgrammeView.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ const ProgrammeView = () => {
7474
<h3>{programme.koulutusohjelma}</h3>
7575
</div>
7676
{KeyDataPoints.map(data => (
77-
<KeyDataCard key={data.title} level={level} metadata={metadata} programme={programme} {...data} />
77+
<>
78+
<KeyDataCard key={data.title} level={level} metadata={metadata} programme={programme} {...data} />
79+
<TextFieldComponent id={data.title} type="comment" />
80+
</>
7881
))}
7982
<TextFieldComponent id="testing" />
8083
</Box>

server/controllers/reportsController.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import db from '../models/index.js'
33

44
import Report from '../models/reports.js'
55
import { Op } from 'sequelize'
6-
import { updateWebsocketState } from '../websocket.js'
6+
import { updateWSAndClearEditors } from '../websocket.js'
77

88
import type { Request, Response } from 'express'
99

@@ -19,8 +19,6 @@ interface ValidateOperationResponse {
1919
const validateOperation = async (req: Request): Promise<ValidateOperationResponse> => {
2020
const { studyprogrammeKey, year } = req.params
2121

22-
console.log(req.params)
23-
2422
// TODO: validate body data
2523

2624
const resultObject: ValidateOperationResponse = {
@@ -100,7 +98,7 @@ const updateReport = async (req: Request, res: Response) => {
10098
const result = await validateOperation(req)
10199
if (!result.success) return res.status(result.status).json({ error: result.error })
102100

103-
let data = req.body
101+
const data = req.body
104102
const { report, studyprogrammeId, year } = result
105103

106104
const [_, updatedReport] = await Report.update(
@@ -116,12 +114,13 @@ const updateReport = async (req: Request, res: Response) => {
116114
}
117115
)
118116

119-
data = updatedReport[0].data
117+
const updatedData = updatedReport[0].data
118+
const field = Object.keys(data)[0]
120119

121120
// @ts-ignore
122-
updateWebsocketState(req.user, { room: req.params.studyprogrammeKey, data })
121+
updateWSAndClearEditors({ room: req.params.studyprogrammeKey, updatedData, field })
123122

124-
return res.status(200).json(updatedReport)
123+
return res.status(200).json(updatedData)
125124
} catch (error) {
126125
logger.error(`Database error ${error}`)
127126
return res.status(500).json({ error: 'Database error' })

server/util/websocketHandlers.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ const updateField = async (socket, payload, io, uuid) => {
189189
[Op.and]: [{ programme: room }, { year: await whereDraftYear() }, { form }],
190190
},
191191
})
192-
logger.info('currentAnswer', currentAnswer)
193192
if (currentAnswer) {
194193
await db.tempAnswer.update(
195194
{ data: { ...currentAnswer.data, ...data } },
@@ -247,16 +246,25 @@ const getLockHttp = (currentUser, payload, io) => {
247246
return stripTimeouts(currentEditors[room])
248247
}
249248

250-
const updateWebsocketState = (currentUser, payload, io) => {
251-
const { room, data } = payload
249+
const updateWSAndClearEditors = (io, payload) => {
250+
const { room, data, field } = payload
252251

253-
logAndEmitToRoom(io, room, 'new_form_data', data)
252+
currentEditors = {
253+
...currentEditors,
254+
[room]: {
255+
...currentEditors[room],
256+
[field]: undefined,
257+
},
258+
}
259+
260+
emitCurrentEditorsTo(io, room, currentEditors)
261+
// logAndEmitToRoom(io, room, 'new_form_data', data)
254262
}
255263

256264
export default {
257265
joinRoom: withLogging(joinRoom),
258266
leaveRoom: withLogging(leaveRoom),
259267
updateField: withLogging(updateField),
260268
getLockHttp,
261-
updateWebsocketState,
269+
updateWSAndClearEditors,
262270
}

server/websocket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ const createWebsocketServer = server => {
1313
}
1414

1515
export const getLockForHttp = (cuser, room) => websocketHandlers.getLockHttp(cuser, room, io)
16-
export const updateWebsocketState = (cuser, room) => websocketHandlers.updateWebsocketState(cuser, room, io)
16+
export const updateWSAndClearEditors = payload => websocketHandlers.updateWSAndClearEditors(io, payload)
1717

1818
export default createWebsocketServer

0 commit comments

Comments
 (0)