Settings.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. useHttps: null,
  75. ban: {
  76. domain: false,
  77. error: '',
  78. host: false,
  79. loading: false,
  80. message: '',
  81. user: false,
  82. },
  83. };
  84. this.onSubmitBan = this.onSubmitBan.bind(this);
  85. this.onChangeBanCheckboxes = this.onChangeBanCheckboxes.bind(this);
  86. this.handleCustomDomain = this.handleCustomDomain.bind(this);
  87. this.handleCheckbox = this.handleCheckbox.bind(this);
  88. this.deleteDomain = this.deleteDomain.bind(this);
  89. this.showModal = this.showModal.bind(this);
  90. this.closeModal = this.closeModal.bind(this);
  91. this.changePassword = this.changePassword.bind(this);
  92. }
  93. componentDidMount() {
  94. if (!this.props.auth.isAuthenticated) Router.push('/login');
  95. this.props.getUserSettings();
  96. }
  97. async onSubmitBan(e) {
  98. e.preventDefault();
  99. const { ban: { domain, host, user } } = this.state;
  100. this.setState(state => ({
  101. ban: {
  102. ...state.ban,
  103. loading: true,
  104. },
  105. }));
  106. const id = e.currentTarget.elements.id.value;
  107. let message;
  108. let error;
  109. try {
  110. message = await this.props.banUrl({
  111. id,
  112. domain,
  113. host,
  114. user,
  115. });
  116. } catch (err) {
  117. error = err;
  118. }
  119. this.setState(
  120. state => ({
  121. ban: {
  122. ...state.ban,
  123. loading: false,
  124. message,
  125. error,
  126. },
  127. }),
  128. () => {
  129. setTimeout(() => {
  130. this.setState(state => ({
  131. ban: {
  132. ...state.ban,
  133. loading: false,
  134. message: '',
  135. error: '',
  136. },
  137. }));
  138. }, 2000);
  139. }
  140. );
  141. }
  142. onChangeBanCheckboxes(type) {
  143. return e => {
  144. const { checked } = e.target;
  145. this.setState(state => ({
  146. ban: {
  147. ...state.ban,
  148. [type]: !checked,
  149. },
  150. }));
  151. };
  152. }
  153. handleCustomDomain(e) {
  154. e.preventDefault();
  155. if (this.props.domainLoading) return null;
  156. const { useHttps } = this.state;
  157. const customDomain = e.currentTarget.elements.customdomain.value;
  158. const homepage = e.currentTarget.elements.homepage.value;
  159. return this.props.setCustomDomain({ customDomain, homepage, useHttps });
  160. }
  161. handleCheckbox({ target: { id, checked } }) {
  162. this.setState({ [id]: !checked });
  163. }
  164. deleteDomain() {
  165. this.closeModal();
  166. this.props.deleteCustomDomain();
  167. }
  168. showModal() {
  169. this.setState({ showModal: true });
  170. }
  171. closeModal() {
  172. this.setState({ showModal: false });
  173. }
  174. changePassword(e) {
  175. e.preventDefault();
  176. const form = e.target;
  177. const password = form.elements.password.value;
  178. if (password.length < 8) {
  179. return this.setState({ passwordError: 'Password must be at least 8 chars long.' }, () => {
  180. setTimeout(() => {
  181. this.setState({
  182. passwordError: '',
  183. });
  184. }, 1500);
  185. });
  186. }
  187. return axios
  188. .post(
  189. '/api/auth/changepassword',
  190. { password },
  191. { headers: { Authorization: cookie.get('token') } }
  192. )
  193. .then(res =>
  194. this.setState({ passwordMessage: res.data.message }, () => {
  195. setTimeout(() => {
  196. this.setState({ passwordMessage: '' });
  197. }, 1500);
  198. form.reset();
  199. })
  200. )
  201. .catch(err =>
  202. this.setState({ passwordError: err.response.data.error }, () => {
  203. setTimeout(() => {
  204. this.setState({
  205. passwordError: '',
  206. });
  207. }, 1500);
  208. })
  209. );
  210. }
  211. render() {
  212. const { auth: { user, admin } } = this.props;
  213. return (
  214. <Wrapper>
  215. <SettingsWelcome user={user} />
  216. <hr />
  217. {admin && (
  218. <Fragment>
  219. <SettingsBan
  220. {...this.state.ban}
  221. onSubmitBan={this.onSubmitBan}
  222. onChangeBanCheckboxes={this.onChangeBanCheckboxes}
  223. />
  224. <hr />
  225. </Fragment>
  226. )}
  227. <SettingsDomain
  228. handleCustomDomain={this.handleCustomDomain}
  229. handleCheckbox={this.handleCheckbox}
  230. useHttps={this.state.useHttps}
  231. loading={this.props.domainLoading}
  232. settings={this.props.settings}
  233. showDomainInput={this.props.showDomainInput}
  234. showModal={this.showModal}
  235. />
  236. <hr />
  237. <SettingsPassword
  238. message={this.state.passwordMessage}
  239. error={this.state.passwordError}
  240. changePassword={this.changePassword}
  241. />
  242. <hr />
  243. <SettingsApi
  244. loader={this.props.apiLoading}
  245. generateKey={this.props.generateApiKey}
  246. apikey={this.props.settings.apikey}
  247. />
  248. <Modal show={this.state.showModal} close={this.closeModal} handler={this.deleteDomain}>
  249. Are you sure do you want to delete the domain?
  250. </Modal>
  251. </Wrapper>
  252. );
  253. }
  254. }
  255. Settings.propTypes = {
  256. auth: PropTypes.shape({
  257. admin: PropTypes.bool.isRequired,
  258. isAuthenticated: PropTypes.bool.isRequired,
  259. user: PropTypes.string.isRequired,
  260. }).isRequired,
  261. apiLoading: PropTypes.bool,
  262. deleteCustomDomain: PropTypes.func.isRequired,
  263. domainLoading: PropTypes.bool,
  264. banUrl: PropTypes.func.isRequired,
  265. setCustomDomain: PropTypes.func.isRequired,
  266. generateApiKey: PropTypes.func.isRequired,
  267. getUserSettings: PropTypes.func.isRequired,
  268. settings: PropTypes.shape({
  269. apikey: PropTypes.string.isRequired,
  270. customDomain: PropTypes.string.isRequired,
  271. domainInput: PropTypes.bool.isRequired,
  272. }).isRequired,
  273. showDomainInput: PropTypes.func.isRequired,
  274. };
  275. Settings.defaultProps = {
  276. apiLoading: false,
  277. domainLoading: false,
  278. };
  279. const mapStateToProps = ({
  280. auth,
  281. loading: { api: apiLoading, domain: domainLoading },
  282. settings,
  283. }) => ({
  284. auth,
  285. apiLoading,
  286. domainLoading,
  287. settings,
  288. });
  289. const mapDispatchToProps = dispatch => ({
  290. banUrl: bindActionCreators(banUrl, dispatch),
  291. deleteCustomDomain: bindActionCreators(deleteCustomDomain, dispatch),
  292. setCustomDomain: bindActionCreators(setCustomDomain, dispatch),
  293. generateApiKey: bindActionCreators(generateApiKey, dispatch),
  294. getUserSettings: bindActionCreators(getUserSettings, dispatch),
  295. showDomainInput: bindActionCreators(showDomainInput, dispatch),
  296. });
  297. export default connect(mapStateToProps, mapDispatchToProps)(Settings);