Settings.js 8.2 KB

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