-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index.js
111 lines (96 loc) · 3.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { useState } from 'react';
import Head from 'next/head';
import Fuse from 'fuse.js';
import _ from 'lodash';
import styles from '../styles/Home.module.css';
import CodeSampleModal from '../components/CodeSampleModal';
export default function Start({ countries }) {
const [results, setResults] = useState(countries);
const [isModalOpen, setIsModalOpen] = useState(false);
const fuse = new Fuse(countries, {
keys: ['name'],
threshold: 0.3,
});
return (
<div>
<Head>
<title>Core Web Vitals</title>
<meta name="description" content="Core web vitals walk through" />
<link rel="icon" href="/favicon.ico" />
<link
href="https://fonts.googleapis.com/css2?family=Inter"
rel="stylesheet"
/>
</Head>
<main className={styles.container}>
<h1 className={styles.title}>
Powered by <a href="https://nextjs.org">Next.js!</a>
</h1>
<div className={styles.heroImage}>
<img src="large-image.jpg" alt="Large Image" />
</div>
<div>
<h2 className={styles.secondaryHeading}>Population Lookup</h2>
<input
type="text"
placeholder="Country search..."
className={styles.input}
onChange={async (e) => {
const { value } = e.currentTarget;
const searchResult = fuse
.search(value)
.map((result) => result.item);
const updatedResults = searchResult.length
? searchResult
: countries;
setResults(updatedResults);
// Fake analytics hit
console.info({
searchedAt: _.now(),
});
}}
/>
<ul className={styles.countries}>
{results.map((country) => (
<li key={country.alpha2Code} className={styles.country}>
<p>
{country.name} - {country.population.toLocaleString()}
</p>
</li>
))}
</ul>
</div>
<div className={styles.codeSampleBlock}>
<h2 className={styles.secondaryHeading}>Code Sample</h2>
<p>Ever wondered how to write a function that prints Hello World?</p>
<button onClick={() => setIsModalOpen(true)}>Show Me</button>
<CodeSampleModal
isOpen={isModalOpen}
closeModal={() => setIsModalOpen(false)}
/>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=learn&&utm_campaign=core-web-vitals"
target="_blank"
rel="noopener noreferrer"
>
Powered by
<span className={styles.logo}>
<img src="/vercel.svg" alt="Vercel Logo" />
</span>
</a>
</footer>
</div>
);
}
export async function getServerSideProps() {
const response = await fetch('https://restcountries.eu/rest/v2/all');
const countries = await response.json();
return {
props: {
countries,
},
};
}