Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. import React from "react";
  2. import "./admin.css";
  3.  
  4. import FormWrapper from "../../components/formWrapper";
  5. import Input from "../../components/input";
  6.  
  7. class Admin extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.onSubmitHandler = this.onSubmitHandler.bind(this);
  11. this.state = { errors: {}, loading: false };
  12. }
  13.  
  14. componentDidMount() {
  15. const { history } = this.props;
  16. if (localStorage.getItem("role") !== "admin") {
  17. history.push("/");
  18. }
  19. }
  20.  
  21. onSubmitHandler() {
  22. this.setState({ loading: true });
  23. const { history } = this.props;
  24. const paramsRegister = {
  25. headers: {
  26. Accept: "application/json",
  27. "Content-Type": "application/json"
  28. },
  29. body: JSON.stringify({
  30. name: this.nameInput.value,
  31. email: this.emailInput.value,
  32. password: this.passwordInput.value,
  33. password_confirmation: this.passwordInput.value
  34. }),
  35. method: "POST"
  36. };
  37.  
  38. fetch("http://127.0.0.1:8000/register", paramsRegister)
  39. .then(resp => resp.json())
  40. .then(parsed => {
  41. console.log(parsed);
  42. if (parsed.errors) {
  43. this.setState({ errors: parsed.errors, loading: false });
  44. } else {
  45. const grantStatus = {
  46. headers: {
  47. Accept: "application/json",
  48. "Content-Type": "application/json",
  49. Authorization: `Bearer ${localStorage.getItem("session")}`
  50. },
  51. body: JSON.stringify({
  52. user_id: parsed.user.id,
  53. speciality: this.specialityInput.value
  54. }),
  55. method: "POST"
  56. };
  57. console.log({
  58. user_id: parsed.user.id,
  59. speciality: this.specialityInput.value
  60. });
  61. fetch("http://127.0.0.1:8000/api/giveDoctorRole", grantStatus)
  62. .then(resp => resp.json())
  63. .then(parsed => {
  64. this.setState({ loading: false });
  65. alert(`Success! User was registered and ${parsed.message}`);
  66. });
  67. }
  68. })
  69. .catch(e => {
  70. console.log(e);
  71. this.setState({
  72. errors: {
  73. password: "An unhandled error has occured, please try again."
  74. },
  75. loading: false
  76. });
  77. });
  78. }
  79.  
  80. render() {
  81. const { email, password, name, speciality } = this.state.errors;
  82. const { loading } = this.state;
  83. console.log(this.specialityInput);
  84. return (
  85. <div className="background">
  86. <div className="shadow-lg p-3 mb-2 bg-white rounded">
  87. <h2 className="p-2">Administrators panel</h2>
  88. </div>
  89. {!loading ? (
  90. <FormWrapper
  91. callBack={this.onSubmitHandler}
  92. submitText="Register a doctor"
  93. >
  94. <React.Fragment>
  95. <Input labelText="Name" id="nameField" error={name}>
  96. <input
  97. type="text"
  98. className="form-control"
  99. id="nameField"
  100. placeholder="Bob"
  101. ref={input => (this.nameInput = input)}
  102. />
  103. </Input>
  104. <Input labelText="Email" id="emailField" error={email}>
  105. <input
  106. type="email"
  107. className="form-control"
  108. id="emailField"
  109. placeholder="name@example.com"
  110. ref={input => (this.emailInput = input)}
  111. />
  112. </Input>
  113. <Input labelText="Password" id="passwordField" error={password}>
  114. <input
  115. type="password"
  116. className="form-control"
  117. id="passwordField"
  118. placeholder=""
  119. ref={input => (this.passwordInput = input)}
  120. />
  121. </Input>
  122. <Input
  123. labelText="Speciality"
  124. id="specialityField"
  125. error={speciality}
  126. >
  127. <input
  128. type="text"
  129. className="form-control"
  130. id="specialityField"
  131. placeholder=""
  132. ref={(input) => this.specialityInput = input}
  133. />
  134. </Input>
  135. </React.Fragment>
  136. </FormWrapper>
  137. ) : (
  138. <div
  139. className="spinner-border text-primary"
  140. style={{ marginLeft: "50%" }}
  141. role="status"
  142. >
  143. <span className="sr-only">Loading...</span>
  144. </div>
  145. )}
  146. </div>
  147. );
  148. }
  149. }
  150.  
  151. export default Admin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement