Settings.js 7.9 KB

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