Skip to content

Commit

Permalink
users api
Browse files Browse the repository at this point in the history
  • Loading branch information
hopefourie-nyt committed Apr 13, 2022
1 parent 76edacc commit b97fbb6
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 28 deletions.
File renamed without changes.
26 changes: 16 additions & 10 deletions components/layout.js → components/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import Head from 'next/head'
import Image from 'next/image'
import styles from './layout.module.css'
import utilStyles from '../styles/utils.module.css'
import Link from 'next/link'
import React from 'react'
import Head from "next/head";
import Image from "next/image";
import styles from "./layout.module.css";
import utilStyles from "../styles/utils.module.css";
import Link from "next/link";

const name = '[Your Name]'
export const siteTitle = 'Next.js Sample Website'
const name = "[Your Name]";
export const siteTitle = "Next.js Sample Website";

export default function Layout({ children, home }) {
type Props = {
children: React.ReactNode;
home?: boolean;
};

export default function Layout({ children, home }: Props) {
return (
<div className={styles.container}>
<Head>
<Head >
<link rel="icon" href="/favicon.ico" />
<meta
name="description"
Expand Down Expand Up @@ -69,5 +75,5 @@ export default function Layout({ children, home }) {
</div>
)}
</div>
)
);
}
5 changes: 5 additions & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
111 changes: 110 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"react": "17.0.2",
"react-dom": "17.0.2",
"remark": "^14.0.1",
"remark-html": "^15.0.0"
"remark-html": "^15.0.0",
"swr": "^1.3.0"
},
"devDependencies": {
"@types/node": "^17.0.23",
"@types/react": "^18.0.3",
"typescript": "^4.6.3"
}
}
File renamed without changes.
3 changes: 0 additions & 3 deletions pages/api/hello.js

This file was deleted.

6 changes: 6 additions & 0 deletions pages/api/users.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const users = [{ id: 1 }, { id: 2 }, { id: 3 }]

export default function handler(req, res) {
// Get data from your database
res.status(200).json(users)
}
20 changes: 20 additions & 0 deletions pages/api/users/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default function userHandler(req, res) {
const {
query: { id, name },
method,
} = req

switch (method) {
case 'GET':
// Get data from your database
res.status(200).json({ id, name: `User ${id}` })
break
case 'PUT':
// Update or create data in your database
res.status(200).json({ id, name: name || `User ${id}` })
break
default:
res.setHeader('Allow', ['GET', 'PUT'])
res.status(405).end(`Method ${method} Not Allowed`)
}
}
44 changes: 31 additions & 13 deletions pages/index.js → pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import Head from 'next/head'
import Layout, { siteTitle } from '../components/layout'
import utilStyles from '../styles/utils.module.css'
import { getSortedPostsData } from '../lib/posts'
import Link from 'next/link'
import Date from '../components/date'
import Head from "next/head";
import Layout, { siteTitle } from "../components/layout";
import utilStyles from "../styles/utils.module.css";
import { getSortedPostsData } from "../lib/posts";
import Link from "next/link";
import Date from "../components/date";
import useSwr from 'swr'


const fetcher = (url) => fetch(url).then((res) => res.json())

export default function Home({ allPostsData }) {
const { data, error } = useSwr('/api/users', fetcher)

if (error) return <div>Failed to load users</div>
if (!data) return <div>Loading...</div>
return (
<Layout home>
<Layout>
<Head>
<title>{siteTitle}</title>
</Head>
<section className={utilStyles.headingMd}>
<p>[Your Self Introduction]</p>
<p>
(This is a sample website - you’ll be building a site like this in{' '}
(This is a sample website - you’ll be building a site like this in{" "}
<a href="https://nextjs.org/learn">our Next.js tutorial</a>.)
</p>
</section>
Expand All @@ -33,16 +41,26 @@ export default function Home({ allPostsData }) {
</li>
))}
</ul>
<h2 className={utilStyles.headingLg}>Users</h2>
<ul>
{data.map((user) => (
<li key={user.id}>
<Link href="/users/[id]" as={`/users/${user.id}`}>
<a>{`User ${user.id}`}</a>
</Link>
</li>
))}
</ul>
</section>
</Layout>
)
);
}

export async function getStaticProps() {
const allPostsData = getSortedPostsData()
const allPostsData = getSortedPostsData();
return {
props: {
allPostsData
}
}
allPostsData,
},
};
}
File renamed without changes.
17 changes: 17 additions & 0 deletions pages/users/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useRouter } from 'next/router'
import useSwr from 'swr'

const fetcher = (url) => fetch(url).then((res) => res.json())

export default function User() {
const router = useRouter()
const { data, error } = useSwr(
router.query.id ? `/api/users/${router.query.id}` : null,
fetcher
)

if (error) return <div>Failed to load user</div>
if (!data) return <div>Loading...</div>

return <div>{data.name}</div>
}
30 changes: 30 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}

0 comments on commit b97fbb6

Please sign in to comment.