Skip to content

Commit

Permalink
resolves #909 publie un évenement quand la sessions collaborative est…
Browse files Browse the repository at this point in the history
… terminée
  • Loading branch information
ggrossetie committed Jul 3, 2023
1 parent 40f8aeb commit 97b6b3c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
10 changes: 8 additions & 2 deletions front/src/components/collaborative/CollaborativeEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useTranslation } from 'react-i18next'
import { useSelector } from 'react-redux'
import { useHistory, useParams } from 'react-router-dom'
import useGraphQL, { useMutation } from '../../hooks/graphql.js'
import CollaborativeEditorWebSocketStatus from './CollaborativeEditorWebSocketStatus.jsx'

import { stopCollaborativeSession, getCollaborativeSession } from './CollaborativeSession.graphql'

Expand All @@ -28,6 +27,7 @@ export default function CollaborativeEditor () {
const {
data: collaborativeSessionData,
isLoading: collaborativeSessionLoading,
mutate: mutateCollaborativeSession,
} = useGraphQL({ query: getCollaborativeSession, variables: { articleId } })

const {
Expand Down Expand Up @@ -56,6 +56,12 @@ export default function CollaborativeEditor () {
}
}, [mutation])

const handleCollaborativeSessionStateUpdated = useCallback(({ state }) => {
if (state === 'ended') {
mutateCollaborativeSession()
}
}, [])

if (collaborativeSessionLoading) {
return <Loading/>
}
Expand Down Expand Up @@ -83,7 +89,7 @@ export default function CollaborativeEditor () {
</GeistModal>

<CollaborativeEditorArticleHeader articleId={articleId}/>
<CollaborativeTextEditor collaborativeSessionId={collaborativeSessionId}/>
<CollaborativeTextEditor collaborativeSessionId={collaborativeSessionId} onCollaborativeSessionStateUpdated={handleCollaborativeSessionStateUpdated}/>
<CollaborativeEditorArticleStats/>
</div>)
}
14 changes: 8 additions & 6 deletions front/src/components/collaborative/CollaborativeTextEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ const colors = [
'#DDDDDD',
]

export default function CollaborativeTextEditor ({ collaborativeSessionId }) {
export default function CollaborativeTextEditor ({ collaborativeSessionId, onCollaborativeSessionStateUpdated }) {
const connectingRef = useRef(false)
const [dynamicStyles, setDynamicStyles] = useState('')
const [websocketStatus, setWebsocketStatus] = useState('')
const [yStatus, setYStatus] = useState(null)
const [yText, setYText] = useState(null)
const [awareness, setAwareness] = useState(null)
const { websocketEndpoint } = useSelector(state => state.applicationConfig, shallowEqual)
Expand Down Expand Up @@ -125,13 +124,15 @@ export default function CollaborativeTextEditor ({ collaborativeSessionId }) {
onStatusUpdated: handleWebsocketStatusUpdated
})
const yText = yDocument.getText('main')
const yStatus = yDocument.getText('status')
const yState = yDocument.getText('state')
yText.observe(function () {
handleUpdateArticleStructureAndStats({ text: yText.toString() })
})
yState.observe(function () {
onCollaborativeSessionStateUpdated({ state: yState.toString() })
})
setAwareness(awareness)
setYText(yText)
setYStatus(yStatus)
return () => {
try {
awareness.destroy()
Expand All @@ -157,7 +158,7 @@ export default function CollaborativeTextEditor ({ collaborativeSessionId }) {
editor?.revealLine(line + 1, 1) // smooth
}, [editorRef, editorCursorPosition])

if (!yStatus || !yText) {
if (!yText) {
return <Loading/>
}

Expand All @@ -183,5 +184,6 @@ export default function CollaborativeTextEditor ({ collaborativeSessionId }) {
}

CollaborativeTextEditor.propTypes = {
collaborativeSessionId: PropTypes.string.isRequired
collaborativeSessionId: PropTypes.string.isRequired,
onCollaborativeSessionStateUpdated: PropTypes.func
}
11 changes: 8 additions & 3 deletions graphql/resolvers/articleResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,21 @@ module.exports = {
const yDoc = getYDoc(`ws/${collaborativeSessionId.toString()}`)
const yText = yDoc.getText('main')
yText.insert(0, article.workingVersion.md)

const yState = yDoc.getText('state')
yState.delete(0, yState.length)
yState.insert(0, 'started')

await article.save()
return collaborativeSession
},

async stopCollaborativeSession(article) {
if (article.collaborativeSession && article.collaborativeSession.id) {
const yDoc = getYDoc(`ws/${article.collaborativeSession.id.toString()}`)
const yStatus = yDoc.getText('status')
yStatus.delete(0, yStatus.length)
yStatus.insert(0, 'ended')
const yState = yDoc.getText('state')
yState.delete(0, yState.length)
yState.insert(0, 'ended')

const yText = yDoc.getText('main')
article.workingVersion.md = yText.toString()
Expand Down

0 comments on commit 97b6b3c

Please sign in to comment.