Settings.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import React, { Component, Fragment } 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 SettingsBan from './SettingsBan';
  13. import SettingsApi from './SettingsApi';
  14. import Modal from '../Modal';
  15. import { fadeIn } from '../../helpers/animations';
  16. import {
  17. deleteCustomDomain,
  18. generateApiKey,
  19. getUserSettings,
  20. setCustomDomain,
  21. showDomainInput,
  22. banUrl,
  23. } from '../../actions';
  24. const Wrapper = styled.div`
  25. poistion: relative;
  26. width: 600px;
  27. max-width: 90%;
  28. display: flex;
  29. flex-direction: column;
  30. align-items: flex-start;
  31. padding: 0 0 80px;
  32. animation: ${fadeIn} 0.8s ease;
  33. > * {
  34. max-width: 100%;
  35. }
  36. hr {
  37. width: 100%;
  38. height: 1px;
  39. outline: none;
  40. border: none;
  41. background-color: #e3e3e3;
  42. margin: 24px 0;
  43. @media only screen and (max-width: 768px) {
  44. margin: 12px 0;
  45. }
  46. }
  47. h3 {
  48. font-size: 24px;
  49. margin: 32px 0 16px;
  50. @media only screen and (max-width: 768px) {
  51. font-size: 18px;
  52. }
  53. }
  54. p {
  55. margin: 24px 0;
  56. }
  57. a {
  58. margin: 32px 0 0;
  59. color: #2196f3;
  60. text-decoration: none;
  61. :hover {
  62. color: #2196f3;
  63. border-bottom: 1px dotted #2196f3;
  64. }
  65. }
  66. `;
  67. class Settings extends Component {
  68. constructor() {
  69. super();
  70. this.state = {
  71. showModal: false,
  72. passwordMessage: '',
  73. passwordError: '',
  74. ban: {
  75. domain: false,
  76. error: '',
  77. host: false,
  78. loading: false,
  79. message: '',
  80. user: false,
  81. },
  82. };
  83. this.onSubmitBan = this.onSubmitBan.bind(this);
  84. this.onChangeBanCheckboxes = this.onChangeBanCheckboxes.bind(this);
  85. this.handleCustomDomain = this.handleCustomDomain.bind(this);
  86. this.deleteDomain = this.deleteDomain.bind(this);
  87. this.showModal = this.showModal.bind(this);
  88. this.closeModal = this.closeModal.bind(this);
  89. this.changePassword = this.changePassword.bind(this);
  90. }
  91. componentDidMount() {
  92. if (!this.props.auth.isAuthenticated) Router.push('/login');
  93. this.props.getUserSettings();
  94. }
  95. async onSubmitBan(e) {
  96. e.preventDefault();
  97. const { ban: { domain, host, user } } = this.state;
  98. this.setState(state => ({
  99. ban: {
  100. ...state.ban,
  101. loading: true,
  102. },
  103. }));
  104. const id = e.currentTarget.elements.id.value;
  105. let message;
  106. let error;
  107. try {
  108. message = await this.props.banUrl({
  109. id,
  110. domain,
  111. host,
  112. user,
  113. });
  114. } catch (err) {
  115. error = err;
  116. }
  117. this.setState(
  118. state => ({
  119. ban: {
  120. ...state.ban,
  121. loading: false,
  122. message,
  123. error,
  124. },
  125. }),
  126. () => {
  127. setTimeout(() => {
  128. this.setState(state => ({
  129. ban: {
  130. ...state.ban,
  131. loading: false,
  132. message: '',
  133. error: '',
  134. },
  135. }));
  136. }, 2000);
  137. }
  138. );
  139. }
  140. onChangeBanCheckboxes(type) {
  141. return e => {
  142. const { checked } = e.target;
  143. this.setState(state => ({
  144. ban: {
  145. ...state.ban,
  146. [type]: !checked,
  147. },
  148. }));
  149. };
  150. }
  151. handleCustomDomain(e) {
  152. e.preventDefault();
  153. if (this.props.domainLoading) return null;
  154. const customDomain = e.currentTarget.elements.customdomain.value;
  155. return this.props.setCustomDomain({ customDomain });
  156. }
  157. deleteDomain() {
  158. this.closeModal();
  159. this.props.deleteCustomDomain();
  160. }
  161. showModal() {
  162. this.setState({ showModal: true });
  163. }
  164. closeModal() {
  165. this.setState({ showModal: false });
  166. }
  167. changePassword(e) {
  168. e.preventDefault();
  169. const form = e.target;
  170. const password = form.elements.password.value;
  171. if (password.length < 8) {
  172. return this.setState({ passwordError: 'Password must be at least 8 chars long.' }, () => {
  173. setTimeout(() => {
  174. this.setState({
  175. passwordError: '',
  176. });
  177. }, 1500);
  178. });
  179. }
  180. return axios
  181. .post(
  182. '/api/auth/changepassword',
  183. { password },
  184. { headers: { Authorization: cookie.get('token') } }
  185. )
  186. .then(res =>
  187. this.setState({ passwordMessage: res.data.message }, () => {
  188. setTimeout(() => {
  189. this.setState({ passwordMessage: '' });
  190. }, 1500);
  191. form.reset();
  192. })
  193. )
  194. .catch(err =>
  195. this.setState({ passwordError: err.response.data.error }, () => {
  196. setTimeout(() => {
  197. this.setState({
  198. passwordError: '',
  199. });
  200. }, 1500);
  201. })
  202. );
  203. }
  204. render() {
  205. const { auth: { user, admin } } = this.props;
  206. return (
  207. <Wrapper>
  208. <SettingsWelcome user={user} />
  209. <hr />
  210. {admin && (
  211. <Fragment>
  212. <SettingsBan
  213. {...this.state.ban}
  214. onSubmitBan={this.onSubmitBan}
  215. onChangeBanCheckboxes={this.onChangeBanCheckboxes}
  216. />
  217. <hr />
  218. </Fragment>
  219. )}
  220. <SettingsDomain
  221. handleCustomDomain={this.handleCustomDomain}
  222. loading={this.props.domainLoading}
  223. settings={this.props.settings}
  224. showDomainInput={this.props.showDomainInput}
  225. showModal={this.showModal}
  226. />
  227. <hr />
  228. <SettingsPassword
  229. message={this.state.passwordMessage}
  230. error={this.state.passwordError}
  231. changePassword={this.changePassword}
  232. />
  233. <hr />
  234. <SettingsApi
  235. loader={this.props.apiLoading}
  236. generateKey={this.props.generateApiKey}
  237. apikey={this.props.settings.apikey}
  238. />
  239. <Modal show={this.state.showModal} close={this.closeModal} handler={this.deleteDomain}>
  240. Are you sure do you want to delete the domain?
  241. </Modal>
  242. </Wrapper>
  243. );
  244. }
  245. }
  246. Settings.propTypes = {
  247. auth: PropTypes.shape({
  248. admin: PropTypes.bool.isRequired,
  249. isAuthenticated: PropTypes.bool.isRequired,
  250. user: PropTypes.string.isRequired,
  251. }).isRequired,
  252. apiLoading: PropTypes.bool,
  253. deleteCustomDomain: PropTypes.func.isRequired,
  254. domainLoading: PropTypes.bool,
  255. banUrl: PropTypes.func.isRequired,
  256. setCustomDomain: PropTypes.func.isRequired,
  257. generateApiKey: PropTypes.func.isRequired,
  258. getUserSettings: PropTypes.func.isRequired,
  259. settings: PropTypes.shape({
  260. apikey: PropTypes.string.isRequired,
  261. customDomain: PropTypes.string.isRequired,
  262. domainInput: PropTypes.bool.isRequired,
  263. }).isRequired,
  264. showDomainInput: PropTypes.func.isRequired,
  265. };
  266. Settings.defaultProps = {
  267. apiLoading: false,
  268. domainLoading: false,
  269. };
  270. const mapStateToProps = ({
  271. auth,
  272. loading: { api: apiLoading, domain: domainLoading },
  273. settings,
  274. }) => ({
  275. auth,
  276. apiLoading,
  277. domainLoading,
  278. settings,
  279. });
  280. const mapDispatchToProps = dispatch => ({
  281. banUrl: bindActionCreators(banUrl, dispatch),
  282. deleteCustomDomain: bindActionCreators(deleteCustomDomain, dispatch),
  283. setCustomDomain: bindActionCreators(setCustomDomain, dispatch),
  284. generateApiKey: bindActionCreators(generateApiKey, dispatch),
  285. getUserSettings: bindActionCreators(getUserSettings, dispatch),
  286. showDomainInput: bindActionCreators(showDomainInput, dispatch),
  287. });
  288. export default connect(mapStateToProps, mapDispatchToProps)(Settings);