Skip to content

Commit

Permalink
fix sockets on frontend side
Browse files Browse the repository at this point in the history
  • Loading branch information
Rost-is-love committed Jun 28, 2023
1 parent 0a9b54a commit 3d397d9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
46 changes: 21 additions & 25 deletions examples/aidbox-subscription/frontend/create-sample.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, Card, Container, Grid, Link, Progress, Text } from '@nextui-org/react'
import { Timeline } from 'antd'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import { Plus, TickSquare } from 'react-iconly'

import { aidboxClient } from '../backend/aidbox-client.js'
Expand Down Expand Up @@ -65,31 +65,27 @@ export const CreateSample = () => {
setPatientsCreated(true)
}

socketIo.on('subs_notification_patient', function (data) {
if (!subsNotifications.includes(data)) {
setSubsNotifications(subsNotifications => [...subsNotifications, data])
}
})
useEffect(() => {
socketIo.on('subs_notification_patient', function (data) {
setTimeout(() => setSubsNotifications(subsNotifications => [...subsNotifications, data]), 400)
})
socketIo.on('push_patient', function (data) {
setTimeout(() => setPushedPatients(pushedPatients => [...pushedPatients, data]), 400)
})

socketIo.on('push_patient', function (data) {
console.log(data)
if (!pushedPatients.includes(data)) {
setPushedPatients(pushedPatients => [...pushedPatients, data])
}
})
socketIo.on('pull_patient', function (data) {
setTimeout(() => setPulledPatients(pulledPatients => [...pulledPatients, data]), 400)
})

socketIo.on('pull_patient', function (data) {
if (!pulledPatients.includes(data)) {
setPulledPatients(pulledPatients => [...pulledPatients, data])
}
})
socketIo.on('create_task_patient', function (data) {
setTimeout(() => setCreatedTasks(createdTasks => [...createdTasks, data]), 400)
})

socketIo.on('create_task_patient', function (data) {
if (!createdTasks.includes(data)) {
setCreatedTasks(createdTasks => [...createdTasks, data])
return () => {
socketIo.off()
}
})
console.log(pushedPatients, 'pushedPatients')
}, [])

return (
<>
<Text h2>Create Patient Subscription</Text>
Expand All @@ -102,7 +98,7 @@ console.log(pushedPatients, 'pushedPatients')
direction='column'
>
<Text css={{ 'text-align': 'center', 'margin-top': 0 }}>
Create new patients to trigger subscriptions.
We can create 10 patients and observe how the subscription feature works.
</Text>
{patientsCreated
? <>
Expand Down Expand Up @@ -208,13 +204,13 @@ console.log(pushedPatients, 'pushedPatients')
}
]}
/>}
</>
</>
: <Button
onPress={createPatients}
icon={<Plus set='light' />}
>
Create 10 patients
</Button>
</Button>
}
</Container>
</Card.Body>
Expand Down
6 changes: 3 additions & 3 deletions examples/aidbox-subscription/frontend/update-sample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ export const UpdateSample = () => {

socketIo.on('subs_notification_appointment', function (data) {
if (data === appointment?.id) {
setTimeout(() => setSubsNotifications(true), 1400)
setTimeout(() => setSubsNotifications(true), 1400)
}
})

socketIo.on('push_appointment', function (data) {
if (data === appointment?.id) {
setTimeout(() => setPushedAppointment(true), 2400)
setTimeout(() => setPushedAppointment(true), 2400)
}
})

Expand All @@ -151,7 +151,7 @@ export const UpdateSample = () => {

socketIo.on('create_task_appointment', function (data) {
if (data === appointment?.id) {
setTimeout(() => setCreatedTasks(true), 500)
setTimeout(() => setCreatedTasks(true), 500)
}
})

Expand Down

0 comments on commit 3d397d9

Please sign in to comment.