44 Input
55} from '@chakra-ui/react'
66import { Skeleton } from '@chakra-ui/react'
7- import { useState } from 'react'
7+ import { useCallback , useState } from 'react'
88import { useRouter } from 'next/router'
99import Head from 'next/head'
1010import useSWR , { useSWRConfig } from 'swr'
@@ -96,7 +96,7 @@ export default function Room() {
9696 const { data, error } = useSWR ( `/api/rooms/room?id=${ id } ` , fetcher )
9797 const [ password , setPassword ] = useState ( '' )
9898 const handlePasswordChange = ( event ) => setPassword ( event . target . value )
99- const handleClick = async ( row , col ) => {
99+ const handleClick = useCallback ( async ( row , col ) => {
100100 mutate ( `/api/rooms/room?id=${ id } ` , { ...data , occupied : [ ...data . occupied , { name : name , row : row , col : col } ] } , false )
101101 let res = await fetch ( `/api/rooms/room?id=${ id } ` , {
102102 method : "POST" ,
@@ -124,8 +124,9 @@ export default function Room() {
124124 setLocked ( true )
125125 setLocation ( { row, col } )
126126 }
127- }
128- const handleExitSeat = async ( { row, col } ) => {
127+ } , [ id , name , data ] )
128+
129+ const handleExitSeat = useCallback ( async ( { row, col } ) => {
129130 mutate ( `/api/rooms/room?id=${ id } ` , { ...data , occupied : [ ...data . occupied . filter ( occupied => ! ( occupied . row === row && occupied . col === col ) ) ] } , false )
130131 let res = await fetch ( `/api/rooms/room?id=${ id } ` , {
131132 method : "POST" ,
@@ -150,8 +151,8 @@ export default function Room() {
150151 setLocked ( false )
151152 setLocation ( { row : - 1 , col : - 1 } )
152153 }
153- }
154- const handleClearSeat = async ( ) => {
154+ } , [ id , data ] )
155+ const handleClearSeat = useCallback ( async ( ) => {
155156 if ( password . trim ( ) === '' ) {
156157 toast ( {
157158 title : `Please enter the room password` ,
@@ -193,8 +194,8 @@ export default function Room() {
193194 isClosable : true ,
194195 } )
195196 }
196- }
197- const handleDeleteRoom = async ( ) => {
197+ } , [ id , password , data ] )
198+ const handleDeleteRoom = useCallback ( async ( ) => {
198199 if ( password . trim ( ) === '' ) {
199200 toast ( {
200201 title : `Please enter the room password` ,
@@ -238,17 +239,16 @@ export default function Room() {
238239 isClosable : true ,
239240 } )
240241 }
241- }
242-
243- const handleRefresh = async ( ) => {
242+ } , [ password , id ] )
243+ const handleRefresh = useCallback ( async ( ) => {
244244 toast ( {
245245 title : `Fetched latest data` ,
246246 status : 'success' ,
247247 position : 'top' ,
248248 isClosable : true ,
249249 } )
250250 mutate ( `/api/rooms/room?id=${ id } ` )
251- }
251+ } , [ id ] )
252252
253253 if ( ! data || error ) {
254254 return DefaultPage
@@ -291,7 +291,7 @@ export default function Room() {
291291 < div className = "flex justify-center mb-10" >
292292 < div className = "py-2 mb-10 overflow-x-auto rounded-lg lg:mb-0" >
293293 < div className = "grid gap-2 lg:gap-4" >
294- < FloorPlan data = { data } gap = { data . gap } handleClick = { handleClick } name = { name } locked = { locked } />
294+ < FloorPlan data = { data } gap = { data . gap } handleClick = { handleClick } name = { name } locked = { locked } location = { location } />
295295 </ div >
296296 </ div >
297297 </ div >
0 commit comments