Skip to content

Commit ddf9780

Browse files
committed
refactor and style updates
1 parent 5363575 commit ddf9780

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

components/FloorPlan.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ function makeGrid(row, col, occupied) {
4343
}
4444

4545
const initialGrid = defaultGrid()
46-
export default function FloorPlan({ data, gap, handleClick, name, locked }) {
46+
export default function FloorPlan({ data, gap, handleClick, name, locked, location }) {
47+
const getBg = (row, col) => {
48+
if (location.row === row && location.col === col) {
49+
return 'teal.300'
50+
} else {
51+
return 'teal'
52+
}
53+
}
4754
const [grid, setGridData] = useState(initialGrid)
4855
useEffect(() => {
4956
if (data) {
@@ -59,7 +66,7 @@ export default function FloorPlan({ data, gap, handleClick, name, locked }) {
5966
<div className={`${colIdx === gap.col ? 'pl-10' : ''}`}>
6067
{col.occupied ?
6168
<Tooltip label={col.name} hasArrow placement='auto'>
62-
<Avatar textColor={'white'} name={col.name ? col.name : rowIdx + ' ' + colIdx} bg={col.occupied ? 'teal' : 'gray'} >
69+
<Avatar textColor={'white'} name={col.name ? col.name : rowIdx + ' ' + colIdx} bg={getBg(rowIdx, colIdx)} >
6370
<AvatarBadge boxSize='1.25em' bg='green.500' />
6471
</Avatar>
6572
</Tooltip> : <Avatar />}
@@ -68,7 +75,7 @@ export default function FloorPlan({ data, gap, handleClick, name, locked }) {
6875
<PopoverContent>
6976
<PopoverArrow />
7077
<PopoverCloseButton />
71-
<PopoverHeader>Row-{rowIdx + 1} Col-{colIdx + 1}</PopoverHeader>
78+
<PopoverHeader>Row {rowIdx + 1} Column {colIdx + 1}</PopoverHeader>
7279
<PopoverBody>
7380
<div>{col.name ? `Occupant: ${col.name}` : ''}</div>
7481
{col.occupied ? undefined : name === '' ? <div>Enter your name below first!</div> :

pages/[id].js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
Input
55
} from '@chakra-ui/react'
66
import { Skeleton } from '@chakra-ui/react'
7-
import { useState } from 'react'
7+
import { useCallback, useState } from 'react'
88
import { useRouter } from 'next/router'
99
import Head from 'next/head'
1010
import 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>

utils/constants.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const SAMPLE_ROOMS = [
1010
row: 5,
1111
col: 5,
1212
},
13-
creationTime: 'January 1'
13+
creationTime: 'Jan 11, 11 PM'
1414
},
1515
{
1616
id: 'world',
@@ -23,7 +23,7 @@ export const SAMPLE_ROOMS = [
2323
row: 5,
2424
col: 5,
2525
},
26-
creationTime: 'January 1'
26+
creationTime: 'Jan 11, 11 PM'
2727
},
2828
{
2929
id: 'hello',
@@ -36,7 +36,7 @@ export const SAMPLE_ROOMS = [
3636
row: 5,
3737
col: 5,
3838
},
39-
creationTime: 'January 1'
39+
creationTime: 'Jan 11, 11 PM'
4040
},
4141
{
4242
id: 'world',
@@ -49,7 +49,7 @@ export const SAMPLE_ROOMS = [
4949
row: 5,
5050
col: 5,
5151
},
52-
creationTime: 'January 1'
52+
creationTime: 'Jan 11, 11 PM'
5353
},
5454
{
5555
id: 'hello',
@@ -62,7 +62,7 @@ export const SAMPLE_ROOMS = [
6262
row: 5,
6363
col: 5,
6464
},
65-
creationTime: 'January 1'
65+
creationTime: 'Jan 11, 11 PM'
6666
},
6767
{
6868
id: 'world',
@@ -75,7 +75,7 @@ export const SAMPLE_ROOMS = [
7575
row: 5,
7676
col: 5,
7777
},
78-
creationTime: 'January 1'
78+
creationTime: 'Jan 11, 11 PM'
7979
},
8080
{
8181
id: 'hello',
@@ -88,7 +88,7 @@ export const SAMPLE_ROOMS = [
8888
row: 5,
8989
col: 5,
9090
},
91-
creationTime: 'January 1'
91+
creationTime: 'Jan 11, 11 PM'
9292
},
9393
{
9494
id: 'world',
@@ -101,6 +101,6 @@ export const SAMPLE_ROOMS = [
101101
row: 5,
102102
col: 5,
103103
},
104-
creationTime: 'January 1'
104+
creationTime: 'Jan 11, 11 PM'
105105
}
106106
]

0 commit comments

Comments
 (0)