2020-09-03 17:26:36 +00:00
|
|
|
import React, { ReactNode } from "react";
|
2020-09-04 10:38:58 +00:00
|
|
|
import { Meta } from "@storybook/react";
|
2020-09-03 17:26:36 +00:00
|
|
|
|
|
|
|
import { DataLoader } from "../components/data-loader/DataLoader";
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: "Data Loader",
|
|
|
|
component: DataLoader,
|
|
|
|
} as Meta;
|
|
|
|
|
|
|
|
type Post = {
|
|
|
|
title: string;
|
|
|
|
body: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loadPosts = () => {
|
|
|
|
const PostLoader = (props: { url: string; children: ReactNode }) => {
|
|
|
|
const loader = async () => {
|
|
|
|
const wait = (ms: number, value: Post) =>
|
|
|
|
new Promise((resolve) => setTimeout(resolve, ms, value));
|
|
|
|
return await fetch(props.url)
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((value) => wait(3000, value));
|
|
|
|
};
|
|
|
|
return <DataLoader loader={loader}>{props.children}</DataLoader>;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PostLoader url="https://jsonplaceholder.typicode.com/posts">
|
2020-12-04 18:58:28 +00:00
|
|
|
{(posts: { data: Post[] }) => (
|
2020-09-03 17:26:36 +00:00
|
|
|
<table>
|
2020-12-04 18:58:28 +00:00
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Description</th>
|
2020-09-03 17:26:36 +00:00
|
|
|
</tr>
|
2020-12-04 18:58:28 +00:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{posts.data.map((post, i) => (
|
|
|
|
<tr key={i}>
|
|
|
|
<td>{post.title}</td>
|
|
|
|
<td>{post.body}</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
2020-09-03 17:26:36 +00:00
|
|
|
</table>
|
|
|
|
)}
|
|
|
|
</PostLoader>
|
|
|
|
);
|
|
|
|
};
|