-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall.tsx
143 lines (128 loc) · 4.15 KB
/
all.tsx
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { useTranslation } from "next-i18next";
import { useEffect, useState } from "react";
import { createServerSideProps } from "#server/requests";
import { createSEOTags } from "#lib/seo";
import { allCountries } from "#lib/api/rest-countries";
import { CardList } from "#components/lists/card-list";
import { LoadingBar } from "#components/state";
import { PaginationLocal } from "#components/pagination";
import {
Form,
SubmitSection,
TextSection,
SelectSection,
} from "#components/fancy/form";
import { Option } from "#components/fancy/input";
import { RESTCountries as Layout } from "#components/layout/frontend-mentor";
import { Page } from "#components/pages";
import { CountryCard } from "#components/frontend-mentor";
import styles from "./all.module.scss";
import type { ParsedUrlQuery } from "querystring";
import type { NextPage, InferGetServerSidePropsType } from "next";
import type { FormEvent } from "react";
import type { Country } from "#lib/api/rest-countries";
interface IProps {
countries: Country[];
}
interface IParams extends ParsedUrlQuery {}
const limit = 25;
export default function RESTCountriesAllPage({
localeInfo,
countries,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
const { t } = useTranslation("frontend-mentor");
const [currentPage, changeCurrentPage] = useState(1);
const [filteredCountries, filterCountries] = useState(countries);
const [currentCountries, changeCurrentCountries] = useState<Country[]>([]);
const seoTags = createSEOTags({
localeInfo: localeInfo!,
title: "All Countries",
description: "All Countries",
canonicalPath: "/frontend-mentor/rest-countries/all",
});
const regions = Array.from(
countries.reduce(
(prev, current) => prev.add(current.region),
new Set<string>()
)
);
useEffect(() => {
const currentRange = [(currentPage - 1) * limit, currentPage * limit];
const slicedCountries = filteredCountries.slice(...currentRange);
changeCurrentCountries(() => slicedCountries);
}, [filteredCountries, currentPage]);
function handleSearch(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = event.target as HTMLFormElement;
const name =
// @ts-expect-error `elements` can be accessed by `name`
(form.elements["country-name"] as HTMLInputElement).value.toLowerCase();
const region =
// @ts-expect-error same
(form.elements["country-region"] as HTMLSelectElement).value;
const filteredByName = countries.filter(
(country) =>
country.name.common.toLowerCase().includes(name) &&
(region ? country.region === region : true)
);
filterCountries(() => filteredByName);
changeCurrentPage(() => 1);
}
if (!countries) {
return <LoadingBar />;
}
return (
<Page seoTags={seoTags}>
<Form className={styles.search} onSubmit={handleSearch}>
<TextSection id="country-search" name="country-name">
Name:
</TextSection>
<SelectSection
id="country-region"
label={"Region:"}
name="country-region"
>
<Option key="base-key" value="">
All
</Option>
{regions.map((region) => (
<Option key={region} value={region}>
{region}
</Option>
))}
</SelectSection>
<SubmitSection>Search</SubmitSection>
</Form>
<PaginationLocal
changeCurrentPage={changeCurrentPage}
currentPage={currentPage}
totalCount={filteredCountries.length}
/>
<CardList>
{currentCountries.map((country) => (
<CountryCard key={country.cca3} country={country} />
))}
</CardList>
</Page>
);
}
RESTCountriesAllPage.getLayout = function getLayout(page: NextPage) {
// @ts-expect-error fix type
return <Layout>{page}</Layout>;
};
export const getServerSideProps = createServerSideProps<IProps, IParams>(
{ extraLangNamespaces: ["frontend-mentor"] },
async () => {
const countries = await allCountries();
if (!countries) {
return {
notFound: true,
};
}
return {
props: {
countries,
},
};
}
);