Fetch citations
This commit is contained in:
parent
95b27b202c
commit
1dbb712d7b
10
README.md
10
README.md
|
|
@ -13,10 +13,10 @@ Pirmas puslapis:
|
||||||
+ Keisti elementų pozicijas (pasirinkimo laisvė, kaip tai įgyvendinti)
|
+ Keisti elementų pozicijas (pasirinkimo laisvė, kaip tai įgyvendinti)
|
||||||
|
|
||||||
Antras puslapis:
|
Antras puslapis:
|
||||||
* Gauti duomenis iš public API (https://api.chucknorris.io/jokes/random?category=dev) ir juos parodyti ekrane
|
+ Gauti duomenis iš public API (https://api.chucknorris.io/jokes/random?category=dev) ir juos parodyti ekrane
|
||||||
* Kol puslapis atidarytas, kas 15s atnaujinti duomenis
|
+ Kol puslapis atidarytas, kas 15s atnaujinti duomenis
|
||||||
* Taip pat reikia pavaizduoti datą ir laiką, kada duomenys buvo paskutinį kartą gauti
|
+ Taip pat reikia pavaizduoti datą ir laiką, kada duomenys buvo paskutinį kartą gauti
|
||||||
* Išjungus puslapį, nustoti duomenų gavimą
|
+ Išjungus puslapį, nustoti duomenų gavimą
|
||||||
* Atidarius, iš karto atnaujinti
|
+ Atidarius, iš karto atnaujinti
|
||||||
|
|
||||||
Stiliai ir aplikacijos išvaizda yra laisva forma, bet dizaino libų nenaudoti (material-ui, antd ir pan.)
|
Stiliai ir aplikacijos išvaizda yra laisva forma, bet dizaino libų nenaudoti (material-ui, antd ir pan.)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { } from 'react'
|
||||||
|
|
||||||
export function EnumSelect({ name, entries, defaultValue }: { name: string; entries: Record<string, string>, defaultValue?: string }): JSX.Element {
|
export function EnumSelect({ name, entries, defaultValue }: { name: string; entries: Record<string, string>, defaultValue?: string }): JSX.Element {
|
||||||
return (
|
return (
|
||||||
<select id={name} name={name} defaultValue={defaultValue}>
|
<select id={name} name={name} defaultValue={defaultValue} className="w-full">
|
||||||
{Object.entries(entries).map(([v, name]) => <option key={v} value={v}>{name}</option>)}
|
{Object.entries(entries).map(([v, name]) => <option key={v} value={v}>{name}</option>)}
|
||||||
</select>
|
</select>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ export function TaskForm({ createTask }: { createTask: (t: Record<string, FormDa
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="title">Pavadinimas</label>
|
<label htmlFor="title">Pavadinimas</label>
|
||||||
<div className="mt-2">
|
<div className="mt-2">
|
||||||
<input type="text" id="title" name="title" className="" />
|
<input type="text" id="title" name="title" className="w-full" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ export function TaskRow({ task, updateTask, deleteTask, swapTasks }: TaskProps &
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="td">
|
<div className="td">
|
||||||
<svg className="w-5 h-5 stroke-neutral-200" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
<svg className="w-5 h-5 stroke-neutral-800 dark:stroke-neutral-200" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
||||||
<path d="M21 10L3 10M21 14L3 14M12 4L12 10M12 14L12 20M15 18L12 21L9 18M15 6L12 3L9 6" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round"></path>
|
<path d="M21 10L3 10M21 14L3 14M12 4L12 10M12 14L12 20M15 18L12 21L9 18M15 6L12 3L9 6" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round"></path>
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@
|
||||||
@apply my-4;
|
@apply my-4;
|
||||||
}
|
}
|
||||||
|
|
||||||
a.nav-button {
|
.nav-button {
|
||||||
@apply cursor-pointer px-5 py-2 rounded-lg border border-gray-300 hover:border-indigo-500 transition-colors duration-700;
|
@apply cursor-pointer px-5 py-2 rounded-lg ring-1 ring-inset ring-gray-300 hover:ring-indigo-500 focus:ring-2 focus:ring-indigo-500 transition-shadow duration-500;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tr {
|
.tr {
|
||||||
|
|
@ -63,7 +63,10 @@
|
||||||
|
|
||||||
select,
|
select,
|
||||||
input {
|
input {
|
||||||
@apply bg-neutral-100 dark:bg-neutral-700 h-14 w-full rounded-lg border-0 px-3 py-4 ring-1 ring-inset ring-neutral-400 focus:ring-2 focus:ring-indigo-500 focus:outline-none hover:ring-indigo-500 transition-shadow duration-700;
|
@apply bg-white dark:bg-neutral-800 h-10 rounded-lg border-0 px-3 ring-1 ring-inset ring-neutral-200 focus:ring-2 focus:ring-indigo-500 focus:outline-none hover:ring-indigo-500 transition-shadow duration-700;
|
||||||
|
}
|
||||||
|
input:disabled {
|
||||||
|
@apply bg-neutral-100 dark:bg-neutral-700 hover:ring-neutral-200 transition-none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.accordion-title {
|
.accordion-title {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import {
|
||||||
} from "react-router-dom";
|
} from "react-router-dom";
|
||||||
import './index.css'
|
import './index.css'
|
||||||
import { List } from './routes/list.tsx'
|
import { List } from './routes/list.tsx'
|
||||||
|
import { Fetch } from './routes/fetch.tsx'
|
||||||
import { Root } from './routes/root.tsx'
|
import { Root } from './routes/root.tsx'
|
||||||
import { ErrorPage } from './error-page.tsx';
|
import { ErrorPage } from './error-page.tsx';
|
||||||
|
|
||||||
|
|
@ -21,7 +22,7 @@ const router = createBrowserRouter([
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/fetch",
|
path: "/fetch",
|
||||||
element: <div/>,
|
element: <Fetch />,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,86 @@
|
||||||
|
import { useState, useEffect, useRef, useCallback } from 'react'
|
||||||
|
|
||||||
|
const URL = 'https://api.chucknorris.io/jokes/random?category=dev';
|
||||||
|
|
||||||
|
export function Fetch() {
|
||||||
|
// const [seconds, setSeconds] = useState(15);
|
||||||
|
const [last, setLast] = useState(0);
|
||||||
|
const [now, setNow] = useState(0);
|
||||||
|
const [response, setResponse] = useState<undefined | {
|
||||||
|
icon_url: string;
|
||||||
|
value: string;
|
||||||
|
}>(undefined);
|
||||||
|
|
||||||
|
|
||||||
|
const fetchRef = useRef(0);
|
||||||
|
const countdownRef = useRef(0);
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
const fetchData = async () => {
|
||||||
|
setLast(Date.now());
|
||||||
|
setResponse(undefined);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(URL);
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
setResponse(data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const stopFetching = () => {
|
||||||
|
clearInterval(fetchRef.current);
|
||||||
|
clearInterval(countdownRef.current);
|
||||||
|
if (inputRef.current) {
|
||||||
|
inputRef.current.disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const startFetching = useCallback(() => {
|
||||||
|
if (inputRef.current) {
|
||||||
|
stopFetching();
|
||||||
|
inputRef.current.disabled = true;
|
||||||
|
fetchRef.current = setInterval(fetchData, 1000 * Number(inputRef.current.value));
|
||||||
|
countdownRef.current = setInterval(() => setNow(Date.now()), 1000);
|
||||||
|
setNow(Date.now());
|
||||||
|
fetchData();
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
startFetching();
|
||||||
|
return stopFetching;
|
||||||
|
}, [startFetching])
|
||||||
|
|
||||||
|
const localizedLast = new Intl.DateTimeFormat('lt-LT', {
|
||||||
|
dateStyle: 'short',
|
||||||
|
timeStyle: 'long',
|
||||||
|
}).format(last);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<button className="nav-button mr-5" onClick={stopFetching}>
|
||||||
|
Sustabdyti
|
||||||
|
</button>
|
||||||
|
<button className="nav-button mr-5" onClick={startFetching}>
|
||||||
|
Paleisti
|
||||||
|
</button>
|
||||||
|
gauti kas <input ref={inputRef} className="w-12" defaultValue="15" /> sek. Iki sekančio liko {inputRef.current ? Number(inputRef.current.value) - Math.floor((now - last) / 1000) : 0} sek.
|
||||||
|
<p>Paskutinis gavimas: {localizedLast}</p>
|
||||||
|
<div className="flex">
|
||||||
|
{
|
||||||
|
response ? <>
|
||||||
|
<div className="flex-auto w-1/12 mr-3"><img src={response.icon_url} /></div>
|
||||||
|
<div className="flex-auto w-11/12">{response.value}</div>
|
||||||
|
</> : <svg aria-hidden="true" className="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
|
||||||
|
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
|
||||||
|
</svg>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,7 @@ export function Root() {
|
||||||
<>
|
<>
|
||||||
<nav className="mx-auto flex items-center justify-center gap-x-5 py-6 px-8">
|
<nav className="mx-auto flex items-center justify-center gap-x-5 py-6 px-8">
|
||||||
<Link to={`/list`} className="nav-button">Sąrašas</Link>
|
<Link to={`/list`} className="nav-button">Sąrašas</Link>
|
||||||
<Link to={`/fetch`} className="nav-button">Duomenys</Link>
|
<Link to={`/fetch`} className="nav-button">Gavimas</Link>
|
||||||
</nav>
|
</nav>
|
||||||
<main className="my-10">
|
<main className="my-10">
|
||||||
<Outlet />
|
<Outlet />
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue