Refactor using hooks rules

This commit is contained in:
David
2021-03-11 13:29:22 +01:00
parent 27ffaa425c
commit f7897061fc
2 changed files with 13 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
import { useEffect, useReducer, FC, ChangeEvent } from "react"; import { useState, useEffect, useReducer, FC, ChangeEvent } from "react";
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next"; import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next";
import { useRouter } from "next/router"; import Router from "next/router";
import Error from "next/error"; import Error from "next/error";
import { googleScrape, extractSlug } from "../utils/translate"; import { googleScrape, extractSlug } from "../utils/translate";
import Languages from "../components/Languages"; import Languages from "../components/Languages";
@@ -8,10 +8,8 @@ import { retrieveFiltered } from "../utils/language";
import langReducer, { Actions, initialState } from "../utils/reducer"; import langReducer, { Actions, initialState } from "../utils/reducer";
const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation, error, initial }) => { const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation, error, initial }) => {
if (error)
return <Error statusCode={error} />
const [{ source, target, query }, dispatch] = useReducer(langReducer, initialState); const [{ source, target, query }, dispatch] = useReducer(langReducer, initialState);
const [encodedQuery, setEncodedQuery] = useState("");
const handleChange = (e: ChangeEvent<HTMLInputElement | HTMLSelectElement>) => { const handleChange = (e: ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
dispatch({ dispatch({
@@ -23,12 +21,6 @@ const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation,
}); });
}; };
const router = useRouter();
const updateTranslation = () => {
query && router.push(`/${source}/${target}/${query}`);
};
useEffect(() => { useEffect(() => {
initial && dispatch({ type: Actions.SET_ALL, payload: { state: initial }}); initial && dispatch({ type: Actions.SET_ALL, payload: { state: initial }});
}, [initial]); }, [initial]);
@@ -37,15 +29,19 @@ const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation,
if (!query || query === initial?.query) if (!query || query === initial?.query)
return; return;
const timeout = setTimeout(updateTranslation, 1000); const timeout = setTimeout(() => setEncodedQuery(encodeURIComponent(query)), 1000);
return () => clearTimeout(timeout); return () => clearTimeout(timeout);
}, [query]); }, [query, initial?.query]);
useEffect(updateTranslation, [source, target]); useEffect(() => {
encodedQuery && Router.push(`/${source}/${target}/${encodedQuery}`);
}, [source, target, encodedQuery]);
const { sourceLangs, targetLangs } = retrieveFiltered(source, target); const { sourceLangs, targetLangs } = retrieveFiltered(source, target);
return ( return error ? (
<Error statusCode={error} />
) : (
<div> <div>
<div> <div>
<button onClick={() => dispatch({ type: Actions.SWITCH_LANGS })} disabled={source === "auto"}> <button onClick={() => dispatch({ type: Actions.SWITCH_LANGS })} disabled={source === "auto"}>
@@ -92,7 +88,7 @@ export const getStaticPaths: GetStaticPaths = async () => ({
fallback: true fallback: true
}); });
export const getStaticProps: GetStaticProps = async ({ params, locale }) => { export const getStaticProps: GetStaticProps = async ({ params }) => {
if (!params?.slug || !Array.isArray(params.slug)) if (!params?.slug || !Array.isArray(params.slug))
return { return {
props: {} props: {}

View File

@@ -10,7 +10,7 @@ export async function googleScrape(
error?: number error?: number
}> { }> {
const parsed = replaceBoth("mapping", { source, target }); const parsed = replaceBoth("mapping", { source, target });
const res = await fetch(`https://translate.google.com/m?sl=${parsed.source}&tl=${parsed.target}&q=${encodeURI(query)}`); const res = await fetch(`https://translate.google.com/m?sl=${parsed.source}&tl=${parsed.target}&q=${encodeURIComponent(query)}`);
if (!res.ok) if (!res.ok)
return { return {