Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
simbafs committed Jul 27, 2024
1 parent 45dfb61 commit 943143d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 57 deletions.
112 changes: 56 additions & 56 deletions frontend/src/hooks/useAttendance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,66 @@ import { Diff } from './useDiff'

// useAttendance get init value of attendance from server and listen to websocket for updates
export function useAttendance() {
const token = useToken()
const { socket, lastMessage } = useWS(`ws://localhost:3000/ws?token=${token}`)
const [attendance, updateAttendance] = useReducer(
(
state: Attendance,
action: {
data: Attendance
overwrite: boolean
},
) => {
if (action.overwrite) {
return action.data
}
if (!state) return state
const token = useToken()
const { socket, lastMessage } = useWS(`ws://localhost:3000/ws?token=${token}`)
const [attendance, updateAttendance] = useReducer(
(
state: Attendance,
action: {
data: Attendance
overwrite: boolean
},
) => {
if (action.overwrite) {
return action.data
}
if (!state) return state

const r = { ...state, ...action.data }
// r[action.id] = Number(action.attendance)
return r
},
{},
)
const r = { ...state, ...action.data }
// r[action.id] = Number(action.attendance)
return r
},
{},
)

// get initial attendance
useEffect(() => {
console.log({ token })
console.log('fetching attendance')
fetch(`/api/attendance?token=${token}`)
.then(res => res.json())
.then(data => {
console.log('!!!', data)
return data
})
.then(data =>
updateAttendance({
data: data.attendance,
overwrite: true,
}),
)
.then(() => console.log('attendance loaded'))
}, [token])
// get initial attendance
useEffect(() => {
console.log({ token })
console.log('fetching attendance')
fetch(`/api/attendance?token=${token}`)
.then(res => res.json())
.then(data => {
console.log('!!!', data)
return data
})
.then(data =>
updateAttendance({
data: data.attendance,
overwrite: true,
}),
)
.then(() => console.log('attendance loaded'))
}, [token])

// process the incoming message
useEffect(() => {
console.log({ lastMessage })
const dataArray = shouldParse<Diff[]>(lastMessage, [])
// process the incoming message
useEffect(() => {
console.log({ lastMessage })
const dataArray = shouldParse<Diff[]>(lastMessage, [])

let update: Record<string, number> = {}
for (let data of dataArray) {
update[data.id] = data.attendance
}
let update: Record<string, number> = {}
for (let data of dataArray) {
update[data.id] = data.attendance
}

updateAttendance({
data: update,
overwrite: false,
})
}, [lastMessage])
updateAttendance({
data: update,
overwrite: false,
})
}, [lastMessage])

return {
isConnected: !!socket,
attendance,
updateAttendance,
}
return {
isConnected: !!socket,
attendance,
updateAttendance,
}
}
2 changes: 1 addition & 1 deletion frontend/src/hooks/useToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRouter } from 'next/router'

export const useToken = () => {
return useRouter().query.token as string
return useRouter().query.token as string
}

0 comments on commit 943143d

Please sign in to comment.