This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
website/utils/handleSocketData.ts
2021-10-24 05:19:39 +02:00

55 lines
1.3 KiB
TypeScript

import { SetData } from 'hooks/usePagination'
export interface Item {
id: number
[key: string]: any
}
export interface SocketData {
action: 'create' | 'update' | 'delete'
item: Item
}
export interface HandleSocketDataOptions<T> {
setData: SetData<T>
}
export type SocketListener = (data: SocketData) => void
export const handleSocketData = <T extends Item>(
options: HandleSocketDataOptions<T>
): SocketListener => {
const { setData } = options
const socketListener: SocketListener = (data) => {
setData((oldItems) => {
const newItems = { ...oldItems }
switch (data.action) {
case 'create': {
newItems.rows.push(data.item as T)
break
}
case 'delete': {
const itemIndex = newItems.rows.findIndex(
(item) => item.id === data.item.id
)
if (itemIndex !== -1) {
newItems.rows.splice(itemIndex, 1)
}
break
}
case 'update': {
const itemIndex = newItems.rows.findIndex(
(item) => item.id === data.item.id
)
if (itemIndex !== -1) {
newItems.rows[itemIndex] = data.item as T
}
break
}
}
return newItems
})
}
return socketListener
}