import { useFormState } from "react-use-form-state"; import { Flex } from "reflexbox/styled-components"; import React, { useState } from "react"; import { NextPage } from "next"; import axios from "axios"; import AppWrapper from "../components/AppWrapper"; import TextInput from "../components/TextInput"; import { Button } from "../components/Button"; import Text, { H2 } from "../components/Text"; import { Col } from "../components/Layout"; import Icon from "../components/Icon"; interface Props { protectedLink?: string; } const UrlPasswordPage: NextPage = ({ protectedLink }) => { const [loading, setLoading] = useState(false); const [formState, { password }] = useFormState<{ password: string }>(); const [error, setError] = useState(); const onSubmit = async e => { e.preventDefault(); const { password } = formState.values; if (!password) { return setError("Password must not be empty."); } setError(""); setLoading(true); // TODO: better api calls try { const { data } = await axios.post("/api/url/requesturl", { id: protectedLink, password }); window.location.replace(data.target); } catch ({ response }) { setError(response.data.error); } setLoading(false); }; return ( {!protectedLink ? (

404 | Link could not be found.

) : (

Protected link

Enter the password to be redirected to the link. {error} )}
); }; UrlPasswordPage.getInitialProps = async ({ req }) => { return { protectedLink: req && (req as any).protectedLink }; }; export default UrlPasswordPage;