Settings.js 8.0 KB

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