url-password.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { useFormState } from "react-use-form-state";
  2. import { Flex } from "reflexbox/styled-components";
  3. import React, { useState } from "react";
  4. import { NextPage } from "next";
  5. import axios from "axios";
  6. import AppWrapper from "../components/AppWrapper";
  7. import TextInput from "../components/TextInput";
  8. import { Button } from "../components/Button";
  9. import Text, { H2 } from "../components/Text";
  10. import { Col } from "../components/Layout";
  11. interface Props {
  12. protectedLink?: string;
  13. }
  14. const UrlPasswordPage: NextPage<Props> = ({ protectedLink }) => {
  15. const [loading, setLoading] = useState(false);
  16. const [formState, { password }] = useFormState<{ password: string }>();
  17. const [error, setError] = useState<string>();
  18. const onSubmit = async e => {
  19. e.preventDefault();
  20. const { password } = formState.values;
  21. if (!password) {
  22. return setError("Password must not be empty.");
  23. }
  24. setError("");
  25. setLoading(true);
  26. // TODO: better api calls
  27. try {
  28. const { data } = await axios.post("/api/url/requesturl", {
  29. id: protectedLink,
  30. password
  31. });
  32. window.location.replace(data.target);
  33. } catch ({ response }) {
  34. setError(response.data.error);
  35. }
  36. setLoading(false);
  37. };
  38. return (
  39. <AppWrapper>
  40. {!protectedLink ? (
  41. <H2 my={4} light>
  42. 404 | Link could not be found.
  43. </H2>
  44. ) : (
  45. <Col width={500} maxWidth="97%">
  46. <H2 my={3} bold>
  47. Protected link
  48. </H2>
  49. <Text mb={4}>Enter the password to be redirected to the link.</Text>
  50. <Flex
  51. as="form"
  52. alignItems="center"
  53. onSubmit={onSubmit}
  54. style={{ position: "relative" }}
  55. >
  56. <TextInput
  57. {...password("password")}
  58. placeholder="Password"
  59. height={[44, 54]}
  60. width={[1, 1 / 2]}
  61. mr={3}
  62. autoFocus
  63. required
  64. />
  65. <Button
  66. type="submit"
  67. icon={loading ? "loader" : ""}
  68. height={[40, 44]}
  69. >
  70. Go
  71. </Button>
  72. </Flex>
  73. <Text fontSize={14} color="red" mt={3} normal>
  74. {error}
  75. </Text>
  76. </Col>
  77. )}
  78. </AppWrapper>
  79. );
  80. };
  81. UrlPasswordPage.getInitialProps = async ({ req }) => {
  82. return {
  83. protectedLink: req && (req as any).protectedLink
  84. };
  85. };
  86. export default UrlPasswordPage;