Skip to content

Commit

Permalink
修改呼叫API方式及新增分頁功能
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnglede committed Oct 12, 2023
1 parent 830e517 commit 576af68
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/frontend/js/components/Pagination.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useEffect, useState } from 'react'

/**
* Render pagination
Expand All @@ -18,6 +18,7 @@ const Pagination = ({
className = ''
}) => {
const [currentPage, setCurrentPage] = useState(current)

const renderPrevTabs = () => {
const pages = []
let count = 0
Expand Down
56 changes: 46 additions & 10 deletions packages/frontend/js/components/RoomList.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
import { useEffect } from 'react'
import { useEffect, useState } from 'react'
import Pagination from './Pagination'
import { useDispatch, useSelector } from 'react-redux'
import { getRoomList } from '../redux/slice/roomSlice'

const RoomList = () => {
const dispatch = useDispatch()
const { data = [] } = useSelector((state) => state.room)
const { data, loading } = useSelector((state) => state.room)
const { rooms, totalRoom } = data || {}
const isOdd = (totalAmout) => totalAmout % 2 !== 0
// pagination
const [pagination, setPagination] = useState({
totalPage: 1,
current: 1,
pageSize: 6
})
const { totalPage, current, pageSize } = pagination
useEffect(() => {
let init = true
if (init) dispatch(getRoomList())
return () => {
init = false
if (totalRoom > 0) {
const totalPage = Math.ceil(totalRoom / pageSize)
setPagination((prev) => ({ ...prev, totalPage }))
}
}, [totalRoom])
const getData = () => {
dispatch(getRoomList())
}
const onPrevClick = () => {
setPagination((prev) => ({ ...prev, current: current - 1 }))
}
const onNextClick = () => {
setPagination((prev) => ({ ...prev, current: current + 1 }))
}
// data
const [dataSource, setDataSource] = useState([])
useEffect(() => {
getData()
}, [])
useEffect(() => {
if (rooms) {
const end = pageSize * current
const start = end - pageSize
setDataSource(rooms.slice(start, end))
}
}, [current, rooms])
return (
<div className='room-list'>
<div className='navbar'>
Expand All @@ -27,8 +55,8 @@ const RoomList = () => {
</div>
</div>
<div className='row__list list'>
{!data && <div>loading ...</div>}
{data?.map((room, index) => (
{loading && <div>loading ...</div>}
{dataSource?.map((room, index) => (
<div className='list__card' key={index}>
<div className='avatar'></div>
<div className='host'>
Expand All @@ -41,10 +69,18 @@ const RoomList = () => {
</div>
</div>
))}
{isOdd(data?.length) && <div className='list__card bg--trans'></div>}
{isOdd(dataSource?.length) && (
<div className='list__card bg--trans'></div>
)}
</div>
<div className='row__list'>
<Pagination className='row__pagination' current={10} totalPage={20} />
<Pagination
className='row__pagination'
current={current}
totalPage={totalPage}
onPrevClick={onPrevClick}
onNextClick={onNextClick}
/>
</div>
</div>
)
Expand Down
22 changes: 16 additions & 6 deletions packages/frontend/js/redux/slice/roomSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const getRoomList = createAsyncThunk(
async (payload, { rejectWithValue }) => {
try {
const res = await api.getRoomList(payload)
console.log(res)
if (res.status === 'OK') {
return res
} else {
Expand All @@ -21,18 +20,29 @@ export const getRoomList = createAsyncThunk(
const roomSlice = createSlice({
name: 'room',
initialState: {
data: null
data: null,
loading: false
},
reducers: {
clearData: (state, action) => {
state.data = null
}
},
extraReducers: (builder) => {
builder.addCase(getRoomList.fulfilled, (state, action) => {
state.data = action.payload.rooms
return state
})
builder
.addCase(getRoomList.pending, (state, action) => {
state.loading = true
return state
})
.addCase(getRoomList.fulfilled, (state, action) => {
state.data = action.payload
state.loading = false
return state
})
.addCase(getRoomList.rejected, (state, action) => {
state.loading = false
return state
})
}
})

Expand Down
4 changes: 3 additions & 1 deletion packages/frontend/src/scss/_init.scss
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ body {
background-repeat: no-repeat;
height: 100vh;
}

#app {
background-image: linear-gradient(45deg, #7e66c5, #567ecc);
}
.bg {
&--trans {
background-color: transparent !important;
Expand Down

0 comments on commit 576af68

Please sign in to comment.