Frontend added loading when fetching shorteners

pull/3/head
TZGyn 2 years ago
parent 718c2fa6eb
commit 1ecf220941
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

Binary file not shown.

@ -25,7 +25,7 @@ import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Toaster } from '@/components/ui/toaster' import { Toaster } from '@/components/ui/toaster'
import { useToast } from '@/components/ui/use-toast' import { useToast } from '@/components/ui/use-toast'
import { Copy } from 'lucide-react' import { Copy, Loader2 } from 'lucide-react'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
@ -40,8 +40,10 @@ type Shortener = {
export default function App() { export default function App() {
const [shorteners, setShorteners] = useState<Shortener[]>([]) const [shorteners, setShorteners] = useState<Shortener[]>([])
const [isLoading, setIsLoading] = useState<boolean>(false)
const getShorteners = async () => { const getShorteners = async () => {
setIsLoading(true)
const response = await fetch(backend_url + '/link', { const response = await fetch(backend_url + '/link', {
method: 'GET', method: 'GET',
}) })
@ -49,6 +51,7 @@ export default function App() {
const data = (await response.json()).shorteners as Shortener[] const data = (await response.json()).shorteners as Shortener[]
setShorteners(data) setShorteners(data)
setIsLoading(false)
} }
useEffect(() => { useEffect(() => {
@ -64,7 +67,10 @@ export default function App() {
<Navbar getShorteners={getShorteners} /> <Navbar getShorteners={getShorteners} />
<div className='flex justify-center px-2 pt-8'> <div className='flex justify-center px-2 pt-8'>
<div className='w-full max-w-6xl'> <div className='w-full max-w-6xl'>
<ShortenerTable shorteners={shorteners} /> <ShortenerTable
shorteners={shorteners}
isLoading={isLoading}
/>
</div> </div>
</div> </div>
<Toaster /> <Toaster />
@ -146,7 +152,13 @@ const CreateShortener = ({
) )
} }
const ShortenerTable = ({ shorteners }: { shorteners: Shortener[] }) => { const ShortenerTable = ({
shorteners,
isLoading,
}: {
shorteners: Shortener[]
isLoading: boolean
}) => {
const { toast } = useToast() const { toast } = useToast()
const copyLinkToClipboard = async (code: string) => { const copyLinkToClipboard = async (code: string) => {
await navigator.clipboard.writeText(backend_url + '/' + code) await navigator.clipboard.writeText(backend_url + '/' + code)
@ -158,7 +170,10 @@ const ShortenerTable = ({ shorteners }: { shorteners: Shortener[] }) => {
return ( return (
<Card> <Card>
<CardHeader> <CardHeader>
<CardTitle>Shorteners</CardTitle> <CardTitle className='flex gap-2'>
<div>Shorteners</div>
{isLoading && <Loader2 className='animate-spin'></Loader2>}
</CardTitle>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<Table> <Table>

Loading…
Cancel
Save