Settings.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import Router from 'next/router';
  4. import { bindActionCreators } from 'redux';
  5. import { connect } from 'react-redux';
  6. import styled from 'styled-components';
  7. import cookie from 'js-cookie';
  8. import axios from 'axios';
  9. import SettingsWelcome from './SettingsWelcome';
  10. import SettingsDomain from './SettingsDomain';
  11. import SettingsPassword from './SettingsPassword';
  12. import SettingsApi from './SettingsApi';
  13. import Modal from '../Modal';
  14. import { fadeIn } from '../../helpers/animations';
  15. import {
  16. deleteCustomDomain,
  17. generateApiKey,
  18. getUserSettings,
  19. setCustomDomain,
  20. showDomainInput,
  21. } from '../../actions';
  22. const Wrapper = styled.div`
  23. poistion: relative;
  24. width: 600px;
  25. max-width: 97%;
  26. display: flex;
  27. flex-direction: column;
  28. align-items: flex-start;
  29. padding: 0 0 80px;
  30. animation: ${fadeIn} 0.8s ease;
  31. > * {
  32. max-width: 100%;
  33. }
  34. hr {
  35. width: 100%;
  36. height: 1px;
  37. outline: none;
  38. border: none;
  39. background-color: #e3e3e3;
  40. margin: 24px 0;
  41. @media only screen and (max-width: 768px) {
  42. margin: 12px 0;
  43. }
  44. }
  45. h3 {
  46. font-size: 24px;
  47. margin: 32px 0 16px;
  48. @media only screen and (max-width: 768px) {
  49. font-size: 18px;
  50. }
  51. }
  52. p {
  53. margin: 24px 0;
  54. }
  55. a {
  56. margin: 32px 0 0;
  57. color: #2196f3;
  58. text-decoration: none;
  59. :hover {
  60. color: #2196f3;
  61. border-bottom: 1px dotted #2196f3;
  62. }
  63. }
  64. `;
  65. class Settings extends Component {
  66. constructor() {
  67. super();
  68. this.state = {
  69. showModal: false,
  70. passwordMessage: '',
  71. passwordError: '',
  72. };
  73. this.handleCustomDomain = this.handleCustomDomain.bind(this);
  74. this.deleteDomain = this.deleteDomain.bind(this);
  75. this.showModal = this.showModal.bind(this);
  76. this.closeModal = this.closeModal.bind(this);
  77. this.changePassword = this.changePassword.bind(this);
  78. }
  79. componentDidMount() {
  80. if (!this.props.auth.isAuthenticated) Router.push('/login');
  81. this.props.getUserSettings();
  82. }
  83. handleCustomDomain(e) {
  84. e.preventDefault();
  85. if (this.props.domainLoading) return null;
  86. const customDomain = e.currentTarget.elements.customdomain.value;
  87. return this.props.setCustomDomain({ customDomain });
  88. }
  89. deleteDomain() {
  90. this.closeModal();
  91. this.props.deleteCustomDomain();
  92. }
  93. showModal() {
  94. this.setState({ showModal: true });
  95. }
  96. closeModal() {
  97. this.setState({ showModal: false });
  98. }
  99. changePassword(e) {
  100. e.preventDefault();
  101. const form = e.target;
  102. const password = form.elements.password.value;
  103. if (password.length < 8) {
  104. return this.setState({ passwordError: 'Password must be at least 8 chars long.' }, () => {
  105. setTimeout(() => {
  106. this.setState({
  107. passwordError: '',
  108. });
  109. }, 1500);
  110. });
  111. }
  112. return axios
  113. .post(
  114. '/api/auth/changepassword',
  115. { password },
  116. { headers: { Authorization: cookie.get('token') } }
  117. )
  118. .then(res =>
  119. this.setState({ passwordMessage: res.data.message }, () => {
  120. setTimeout(() => {
  121. this.setState({ passwordMessage: '' });
  122. }, 1500);
  123. form.reset();
  124. })
  125. )
  126. .catch(err =>
  127. this.setState({ passwordError: err.response.data.error }, () => {
  128. setTimeout(() => {
  129. this.setState({
  130. passwordError: '',
  131. });
  132. }, 1500);
  133. })
  134. );
  135. }
  136. render() {
  137. return (
  138. <Wrapper>
  139. <SettingsWelcome user={this.props.auth.user} />
  140. <SettingsDomain
  141. handleCustomDomain={this.handleCustomDomain}
  142. loading={this.props.domainLoading}
  143. settings={this.props.settings}
  144. showDomainInput={this.props.showDomainInput}
  145. showModal={this.showModal}
  146. />
  147. <hr />
  148. <SettingsPassword
  149. message={this.state.passwordMessage}
  150. error={this.state.passwordError}
  151. changePassword={this.changePassword}
  152. />
  153. <hr />
  154. <SettingsApi
  155. loader={this.props.apiLoading}
  156. generateKey={this.props.generateApiKey}
  157. apikey={this.props.settings.apikey}
  158. />
  159. <Modal show={this.state.showModal} close={this.closeModal} handler={this.deleteDomain}>
  160. Are you sure do you want to delete the domain?
  161. </Modal>
  162. </Wrapper>
  163. );
  164. }
  165. }
  166. Settings.propTypes = {
  167. auth: PropTypes.shape({
  168. isAuthenticated: PropTypes.bool.isRequired,
  169. user: PropTypes.string.isRequired,
  170. }).isRequired,
  171. apiLoading: PropTypes.bool,
  172. deleteCustomDomain: PropTypes.func.isRequired,
  173. domainLoading: PropTypes.bool,
  174. setCustomDomain: PropTypes.func.isRequired,
  175. generateApiKey: PropTypes.func.isRequired,
  176. getUserSettings: PropTypes.func.isRequired,
  177. settings: PropTypes.shape({
  178. apikey: PropTypes.string.isRequired,
  179. customDomain: PropTypes.string.isRequired,
  180. domainInput: PropTypes.bool.isRequired,
  181. }).isRequired,
  182. showDomainInput: PropTypes.func.isRequired,
  183. };
  184. Settings.defaultProps = {
  185. apiLoading: false,
  186. domainLoading: false,
  187. };
  188. const mapStateToProps = ({
  189. auth,
  190. loading: { api: apiLoading, domain: domainLoading },
  191. settings,
  192. }) => ({
  193. auth,
  194. apiLoading,
  195. domainLoading,
  196. settings,
  197. });
  198. const mapDispatchToProps = dispatch => ({
  199. deleteCustomDomain: bindActionCreators(deleteCustomDomain, dispatch),
  200. setCustomDomain: bindActionCreators(setCustomDomain, dispatch),
  201. generateApiKey: bindActionCreators(generateApiKey, dispatch),
  202. getUserSettings: bindActionCreators(getUserSettings, dispatch),
  203. showDomainInput: bindActionCreators(showDomainInput, dispatch),
  204. });
  205. export default connect(mapStateToProps, mapDispatchToProps)(Settings);