import React, { useState } from "react"; import { NextPage } from "next"; import styled from "styled-components"; import axios from "axios"; import { Flex } from "reflexbox/styled-components"; import { useFormState } from "react-use-form-state"; import BodyWrapper from "../components/BodyWrapper"; import TextInput from "../components/TextInput"; import { Button } from "../components/Button"; import Text from "../components/Text"; 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;