1- import React , { Component } from 'react' ;
1+ import React , { Component , Fragment } from 'react' ;
22import PropTypes from 'prop-types' ;
33import Router from 'next/router' ;
44import { bindActionCreators } from 'redux' ;
@@ -9,6 +9,7 @@ import axios from 'axios';
99import SettingsWelcome from './SettingsWelcome' ;
1010import SettingsDomain from './SettingsDomain' ;
1111import SettingsPassword from './SettingsPassword' ;
12+ import SettingsBan from './SettingsBan' ;
1213import SettingsApi from './SettingsApi' ;
1314import Modal from '../Modal' ;
1415import { fadeIn } from '../../helpers/animations' ;
@@ -18,6 +19,7 @@ import {
1819 getUserSettings ,
1920 setCustomDomain ,
2021 showDomainInput ,
22+ banUrl ,
2123} from '../../actions' ;
2224
2325const Wrapper = styled . div `
@@ -76,7 +78,17 @@ class Settings extends Component {
7678 showModal : false ,
7779 passwordMessage : '' ,
7880 passwordError : '' ,
81+ ban : {
82+ domain : false ,
83+ error : '' ,
84+ host : false ,
85+ loading : false ,
86+ message : '' ,
87+ user : false ,
88+ } ,
7989 } ;
90+ this . onSubmitBan = this . onSubmitBan . bind ( this ) ;
91+ this . onChangeBanCheckboxes = this . onChangeBanCheckboxes . bind ( this ) ;
8092 this . handleCustomDomain = this . handleCustomDomain . bind ( this ) ;
8193 this . deleteDomain = this . deleteDomain . bind ( this ) ;
8294 this . showModal = this . showModal . bind ( this ) ;
@@ -89,6 +101,64 @@ class Settings extends Component {
89101 this . props . getUserSettings ( ) ;
90102 }
91103
104+ async onSubmitBan ( e ) {
105+ e . preventDefault ( ) ;
106+ const { ban : { domain, host, user } } = this . state ;
107+ this . setState ( state => ( {
108+ ban : {
109+ ...state . ban ,
110+ loading : true ,
111+ } ,
112+ } ) ) ;
113+ const id = e . currentTarget . elements . id . value ;
114+ let message ;
115+ let error ;
116+ try {
117+ message = await this . props . banUrl ( {
118+ id,
119+ domain,
120+ host,
121+ user,
122+ } ) ;
123+ } catch ( err ) {
124+ error = err ;
125+ }
126+ this . setState (
127+ state => ( {
128+ ban : {
129+ ...state . ban ,
130+ loading : false ,
131+ message,
132+ error,
133+ } ,
134+ } ) ,
135+ ( ) => {
136+ setTimeout ( ( ) => {
137+ this . setState ( state => ( {
138+ ban : {
139+ ...state . ban ,
140+ loading : false ,
141+ message : '' ,
142+ error : '' ,
143+ } ,
144+ } ) ) ;
145+ } , 2000 ) ;
146+ }
147+ ) ;
148+ }
149+
150+ onChangeBanCheckboxes ( type ) {
151+ return e => {
152+ const { checked } = e . target ;
153+ this . setState ( state => ( {
154+ ban : {
155+ ...state . ban ,
156+ [ type ] : ! checked ,
157+ } ,
158+ } ) ) ;
159+ } ;
160+ }
161+
92162 handleCustomDomain ( e ) {
93163 e . preventDefault ( ) ;
94164 if ( this . props . domainLoading ) return null ;
@@ -148,9 +218,21 @@ class Settings extends Component {
148218 }
149219
150220 render ( ) {
221+ const { auth : { user, admin } } = this . props ;
151222 return (
152223 < Wrapper >
153- < SettingsWelcome user = { this . props . auth . user } />
224+ < SettingsWelcome user = { user } />
225+ < hr />
226+ { admin && (
227+ < Fragment >
228+ < SettingsBan
229+ { ...this . state . ban }
230+ onSubmitBan = { this . onSubmitBan }
231+ onChangeBanCheckboxes = { this . onChangeBanCheckboxes }
232+ />
233+ < hr />
234+ </ Fragment >
235+ ) }
154236 < SettingsDomain
155237 handleCustomDomain = { this . handleCustomDomain }
156238 loading = { this . props . domainLoading }
@@ -180,12 +262,14 @@ class Settings extends Component {
180262
181263Settings . propTypes = {
182264 auth : PropTypes . shape ( {
265+ admin : PropTypes . bool . isRequired ,
183266 isAuthenticated : PropTypes . bool . isRequired ,
184267 user : PropTypes . string . isRequired ,
185268 } ) . isRequired ,
186269 apiLoading : PropTypes . bool ,
187270 deleteCustomDomain : PropTypes . func . isRequired ,
188271 domainLoading : PropTypes . bool ,
272+ banUrl : PropTypes . func . isRequired ,
189273 setCustomDomain : PropTypes . func . isRequired ,
190274 generateApiKey : PropTypes . func . isRequired ,
191275 getUserSettings : PropTypes . func . isRequired ,
@@ -214,6 +298,7 @@ const mapStateToProps = ({
214298} ) ;
215299
216300const mapDispatchToProps = dispatch => ( {
301+ banUrl : bindActionCreators ( banUrl , dispatch ) ,
217302 deleteCustomDomain : bindActionCreators ( deleteCustomDomain , dispatch ) ,
218303 setCustomDomain : bindActionCreators ( setCustomDomain , dispatch ) ,
219304 generateApiKey : bindActionCreators ( generateApiKey , dispatch ) ,
0 commit comments