DebugCRUD.jsx 3.6 KB
Newer Older
1
import React from "react";
2
3
import { Formik, Field, Form, ErrorMessage } from "formik";
import * as Yup from "yup";
4
import { PageTemplate } from "Components";
5
import { GetAllGoLinks } from "../../Utils";
6
7
8
9
10
11
12
13
14

const SignupSchema = Yup.object().shape({
  password: Yup.string()
    .min(2, "Too Short!")
    .max(50, "Too Long!"),
  email: Yup.string()
    .email("Invalid email")
    .required("Required")
});
15

16
17
18
class DebugCRUD extends React.Component {
  constructor(props) {
    super(props);
19
    this.state = { AllGoLinks: [], error: null };
20
  }
21

22
  async componentDidMount() {
23
    GetAllGoLinks()
24
25
26
27
28
29
30
      .then(data =>
        this.setState({
          AllGoLinks: data
        })
      )
      .catch(reason => this.setState({ error: reason }));
  }
31

32
33
34
35
36
  render() {
    return (
      <PageTemplate>
        <div>
          <h1>Debug CRUD Page</h1>
37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
          <h3>Create</h3>
          <Formik
            initialValues={{ email: "", password: "" }}
            validationSchema={SignupSchema}
            onSubmit={(values, { setSubmitting }) => {
              setTimeout(() => {
                console.log(JSON.stringify(values, null, 2));
                setSubmitting(false);
              }, 400);
            }}
            render={({ isSubmitting }) => (
              <Form>
                <Field type="email" name="email" placeholder="Email" />
                <ErrorMessage name="email" component="div" />
                <Field type="password" name="password" />
                <ErrorMessage name="password" />
                <button type="submit" disabled={isSubmitting}>
                  Submit
                </button>
              </Form>
            )}
          />
60

61
          <h3>Read</h3>
62
63
64
65
66
67
68

          {this.state.AllGoLinks.map(golink => (
            <li key={golink.short}>
              <a href={`/${golink.short}`}> /{golink.short}</a> |{" "}
              {golink.destination}
            </li>
          ))}
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

          <h3>Update</h3>
          <Formik
            initialValues={{ email: "", password: "" }}
            validationSchema={SignupSchema}
            onSubmit={(values, { setSubmitting }) => {
              setTimeout(() => {
                console.log(JSON.stringify(values, null, 2));
                setSubmitting(false);
              }, 400);
            }}
            render={({ isSubmitting }) => (
              <Form>
                <Field type="email" name="email" placeholder="Email" />
                <ErrorMessage name="email" component="div" />
                <Field type="password" name="password" />
                <ErrorMessage name="password" />
                <button type="submit" disabled={isSubmitting}>
                  Submit
                </button>
              </Form>
            )}
          />

          <h3>Delete</h3>
          <Formik
            initialValues={{ email: "", password: "" }}
            validationSchema={SignupSchema}
            onSubmit={(values, { setSubmitting }) => {
              setTimeout(() => {
                console.log(JSON.stringify(values, null, 2));
                setSubmitting(false);
              }, 400);
            }}
            render={({ isSubmitting }) => (
              <Form>
                <Field type="email" name="email" placeholder="Email" />
                <ErrorMessage name="email" component="div" />
                <Field type="password" name="password" />
                <ErrorMessage name="password" />
                <button type="submit" disabled={isSubmitting}>
                  Submit
                </button>
              </Form>
            )}
          />
        </div>
      </PageTemplate>
    );
  }
}
120
121

export default DebugCRUD;