blog-fs/pages/index.tsx

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-05-02 20:04:54 +08:00
import { useQuery } from "@apollo/client";
import { GetServerSideProps } from 'next';
import Link from "next/link";
import React from "react";
import { addApolloState, initializeApollo } from '../commons/graphql/client';
import { Article } from "../commons/graphql/generated";
import { ARTICLE_FOR_HOME } from "../commons/graphql/queries";
import styles from "./index.module.css";
2021-05-01 18:34:45 +08:00
export default function Index() {
2021-05-02 20:04:54 +08:00
const { data, loading } = useQuery<{ articles: Article[] }>(ARTICLE_FOR_HOME);
2021-05-02 10:42:38 +08:00
2021-05-02 20:04:54 +08:00
return (
<main className={styles.index}>
<ol>
{data?.articles?.map((article) => (
<Item article={article} key={article.id} />
))}
</ol>
</main>
);
2021-05-02 10:42:38 +08:00
}
2021-05-02 20:04:54 +08:00
function Item({ article }: { article: Article }) {
return (
<Link href={`/articles/${article.id}`}>
<li className={styles.item}>
<h2 className={styles.title}>{article.title}</h2>
2021-07-03 09:55:27 +08:00
<p className={styles.description}>{article.description}</p>
2021-05-02 20:04:54 +08:00
</li>
</Link>
);
}
export const getServerSideProps: GetServerSideProps = async ({ params }) => {
const apolloClient = initializeApollo();
await apolloClient.query({
query: ARTICLE_FOR_HOME,
});
return addApolloState(apolloClient, {
props: {},
});
};