Skip to content

Commit

Permalink
feat: table loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
TinsFox committed Nov 2, 2024
1 parent 3cbb4b3 commit 1a57a44
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/components/empty.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from "react"

interface EmptyProps {
description?: React.ReactNode
icon?: React.ReactNode
}

export function Empty(props: EmptyProps) {
return <div>{props.description || "No results"}</div>
}
12 changes: 12 additions & 0 deletions src/components/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Icons } from "./icons"

export function Loading() {
return (
<div className="flex items-center justify-center text-sm text-muted-foreground">
<div className="flex items-center justify-center">
<Icons.spinner className="mr-2 size-4 animate-spin" />
<span>Loading...</span>
</div>
</div>
)
}
3 changes: 2 additions & 1 deletion src/hooks/query/use-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function useUserLogoutMutation() {
}

export function useUsers(pagination: PaginationState) {
const { data } = useQuery({
const { data, isPending } = useQuery({
queryKey: ["users", pagination.pageIndex, pagination.pageSize],
queryFn: async () => apiFetch<{
list: IUsers[]
Expand All @@ -67,6 +67,7 @@ export function useUsers(pagination: PaginationState) {
})

return {
isPending,
data: {
list: data?.list || [],
total: data?.total || 0,
Expand Down
8 changes: 6 additions & 2 deletions src/pages/(admin)/(with-layout)/list/basic-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
} from "@tanstack/react-table"
import * as React from "react"

import { Empty } from "@/components/empty"
import { Loading } from "@/components/loading"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
Expand Down Expand Up @@ -195,7 +197,7 @@ export function Component() {
const [columnVisibility, setColumnVisibility] =
React.useState<VisibilityState>({})
const [rowSelection, setRowSelection] = React.useState({})
const { data: users } = useUsers(pagination)
const { data: users, isPending } = useUsers(pagination)

const PAGINATION_STEP = 3 // You can easily change this value to adjust the pagination step

Expand Down Expand Up @@ -300,7 +302,9 @@ export function Component() {
colSpan={columns.length}
className="h-24 text-center"
>
No results.
{
isPending ? <Loading /> : <Empty />
}
</TableCell>
</TableRow>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/(admin)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function Component() {
</div>

<ScrollArea className="flex h-[calc(100vh-3.5rem)] flex-col gap-4 p-2 pt-0 sm:h-[calc(100vh-4rem)] sm:p-4">
<div className="py-2 sm:py-4">
<div className="p-2 sm:py-4">
<Outlet />
</div>
<ScrollBar orientation="horizontal" />
Expand Down

0 comments on commit 1a57a44

Please sign in to comment.