mirror of
https://github.com/m1ngsama/FUJI.git
synced 2025-12-25 02:56:38 +00:00
Add pagination to UserRepairHistory component
- Add pagination state management with currentPage and totalPages - Update API call to use limit/offset parameters for pagination - Add HeroUI Pagination component with navigation controls - Support 12 items per page with conditional pagination display - Handle page changes and loading states properly 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
e33b81775e
commit
f235cd5f1b
1 changed files with 41 additions and 7 deletions
|
|
@ -1,25 +1,31 @@
|
||||||
import { useState, useEffect } from "react"
|
import { useState, useEffect } from "react"
|
||||||
import { Alert, Button, Spinner } from "@heroui/react"
|
import { Alert, Button, Spinner, Pagination } from "@heroui/react"
|
||||||
import { saturdayClient } from "../../utils/client"
|
import { saturdayClient } from "../../utils/client"
|
||||||
import { makeLogtoClient } from "../../utils/auth"
|
import { makeLogtoClient } from "../../utils/auth"
|
||||||
import RepairHistoryCard from "./RepairHistoryCard"
|
import RepairHistoryCard from "./RepairHistoryCard"
|
||||||
import type { components } from "../../types/saturday"
|
import type { components } from "../../types/saturday"
|
||||||
|
import { LogtoError } from "@logto/browser"
|
||||||
|
|
||||||
type PublicEvent = components["schemas"]["PublicEvent"]
|
type PublicEvent = components["schemas"]["PublicEvent"]
|
||||||
|
|
||||||
interface UserRepairHistoryProps {
|
interface UserRepairHistoryProps {
|
||||||
onCreateNew: () => void
|
onCreateNew: () => void
|
||||||
|
onLogin: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function UserRepairHistory({ onCreateNew }: UserRepairHistoryProps) {
|
export default function UserRepairHistory({ onCreateNew, onLogin }: UserRepairHistoryProps) {
|
||||||
const [events, setEvents] = useState<PublicEvent[]>([])
|
const [events, setEvents] = useState<PublicEvent[]>([])
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [error, setError] = useState<string>("")
|
const [error, setError] = useState<string>("")
|
||||||
|
const [currentPage, setCurrentPage] = useState(1)
|
||||||
|
const [totalPages, setTotalPages] = useState(1)
|
||||||
|
const itemsPerPage = 12
|
||||||
|
|
||||||
const fetchUserEvents = async () => {
|
const fetchUserEvents = async (page: number = currentPage) => {
|
||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const logtoToken = await makeLogtoClient().getAccessToken()
|
const logtoToken = await makeLogtoClient().getAccessToken()
|
||||||
|
const offset = (page - 1) * itemsPerPage
|
||||||
|
|
||||||
const { data, error: apiError } = await saturdayClient.GET("/client/events", {
|
const { data, error: apiError } = await saturdayClient.GET("/client/events", {
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -27,8 +33,9 @@ export default function UserRepairHistory({ onCreateNew }: UserRepairHistoryProp
|
||||||
},
|
},
|
||||||
params: {
|
params: {
|
||||||
query: {
|
query: {
|
||||||
limit: 50,
|
limit: itemsPerPage,
|
||||||
offset: 0,
|
offset: offset,
|
||||||
|
order: "desc",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
@ -38,8 +45,17 @@ export default function UserRepairHistory({ onCreateNew }: UserRepairHistoryProp
|
||||||
}
|
}
|
||||||
|
|
||||||
setEvents(data || [])
|
setEvents(data || [])
|
||||||
|
|
||||||
|
// Calculate total pages based on total count
|
||||||
|
// Note: You may need to adjust this based on your API response structure
|
||||||
|
// If your API returns total count in headers or response metadata
|
||||||
|
const totalItems = data.length < itemsPerPage ? offset + data.length : offset + data.length + 1
|
||||||
|
setTotalPages(Math.ceil(totalItems / itemsPerPage))
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
if (err instanceof LogtoError) {
|
||||||
|
onLogin()
|
||||||
|
}
|
||||||
console.error("Error fetching user events:", err)
|
console.error("Error fetching user events:", err)
|
||||||
setError("获取维修记录失败")
|
setError("获取维修记录失败")
|
||||||
}
|
}
|
||||||
|
|
@ -49,9 +65,14 @@ export default function UserRepairHistory({ onCreateNew }: UserRepairHistoryProp
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchUserEvents()
|
fetchUserEvents(1)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const handlePageChange = (page: number) => {
|
||||||
|
setCurrentPage(page)
|
||||||
|
fetchUserEvents(page)
|
||||||
|
}
|
||||||
|
|
||||||
const handleViewDetail = (event: PublicEvent) => {
|
const handleViewDetail = (event: PublicEvent) => {
|
||||||
window.location.href = `/repair/ticket-detail?eventId=${event.eventId}`
|
window.location.href = `/repair/ticket-detail?eventId=${event.eventId}`
|
||||||
}
|
}
|
||||||
|
|
@ -68,7 +89,7 @@ export default function UserRepairHistory({ onCreateNew }: UserRepairHistoryProp
|
||||||
return (
|
return (
|
||||||
<Alert color="danger" className="mb-4">
|
<Alert color="danger" className="mb-4">
|
||||||
{error}
|
{error}
|
||||||
<Button size="sm" color="danger" variant="flat" onPress={fetchUserEvents}>
|
<Button size="sm" color="danger" variant="flat" onPress={() => fetchUserEvents(currentPage)}>
|
||||||
重试
|
重试
|
||||||
</Button>
|
</Button>
|
||||||
</Alert>
|
</Alert>
|
||||||
|
|
@ -104,6 +125,19 @@ export default function UserRepairHistory({ onCreateNew }: UserRepairHistoryProp
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{totalPages > 1 && (
|
||||||
|
<div className="flex justify-center mt-8">
|
||||||
|
<Pagination
|
||||||
|
total={totalPages}
|
||||||
|
page={currentPage}
|
||||||
|
onChange={handlePageChange}
|
||||||
|
showControls
|
||||||
|
showShadow
|
||||||
|
color="primary"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue