Skip to content

Commit

Permalink
Enable deletion of users (#226)
Browse files Browse the repository at this point in the history
* Add route and component for deleteUser
* Invert isAdmin before handing it to DeleteUserForm

Because the form is stupid, it's "impossible" to let it handle isActive as an inverted toggle.
Instead we now invert the value, then hand it to the form. When it comes back we handle it the same
way as before, but in its inverted state. I.e. true means that `isActive` is `false`.

See DIH-399
  • Loading branch information
essoen authored Nov 9, 2016
2 parents a772076 + a11f72f commit 4001de8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/commons/user/deleteUser.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, { PropTypes } from 'react';
import { reduxForm } from 'redux-form';

import Form from '../Form';
import ToggleField from '../Form/ToggleField';
import Button from '../Button';
import Segment from '../Segment';

const fields = ['isActive'];


function DeleteUser(props) {
const {
fields: { isActive },
handleSubmit,
errorMessage,
isFetching
} = props;
return (
<Segment>
<Form
id="deleteUserForm"
errorMessage={errorMessage}
handleSubmit={handleSubmit}
>
<ToggleField
name="I confirm that I want to delete my account"
label={`Deleting your account means that you will have to register again
if you want to go on a new trip. Toggling this button and confirming will
delete your account, and log you out.`}
id="isActive"
>
{isActive}
</ToggleField>
<Button
type="submit"
color="red"
fluid
disabled={!isActive.value}
loading={isFetching}
id="submit"
>
Confirm deletion
</Button>
</Form>
</Segment>
);
}

DeleteUser.propTypes = {
errorMessage: PropTypes.string,
fields: PropTypes.object.isRequired,
handleSubmit: PropTypes.func.isRequired,
isFetching: PropTypes.bool,
submitting: PropTypes.bool.isRequired,
showAdminFields: PropTypes.bool,
disableValidation: PropTypes.bool,
user: PropTypes.object
};

export default reduxForm({
form: 'deleteUserForm',
fields
})(DeleteUser);
2 changes: 2 additions & 0 deletions src/router.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ForgotPasswordConfirm from './sections/forgotPassword/confirm';
import Profile from './sections/profile';
import ViewUser from './commons/user/viewUser';
import EditUser from './commons/user/editUser';
import DeleteUser from './commons/user/deleteUser';

import SignupTrip from './sections/trips/signup';
import Trips from './sections/trips/';
Expand Down Expand Up @@ -81,6 +82,7 @@ export default(
<Route name="My profile" path="/profile" component={Profile} >
<IndexRoute name="View profile" component={ViewUser} />
<Route name="Edit profile" path="edit" component={EditUser} />
<Route name="Delete profile" path="delete" component={DeleteUser} />
</Route>
<Route name="Trips" path="/trips">
<IndexRoute component={Trips} />
Expand Down
19 changes: 18 additions & 1 deletion src/sections/profile/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class Profile extends Component {
{
name: 'Edit',
uri: '/profile/edit'
},
{
name: 'Delete',
uri: '/profile/delete'
}
]
};
Expand All @@ -48,6 +52,18 @@ class Profile extends Component {
}

onUpdate(data) {
if (data.isActive !== 'undefined'
&& data.isActive !== null
&& data.isActive) { // isActive is defined and set to true
// Because it's inverted before it's handed to views
// So that the toggle works as we want in DeleteUser
const deactivatedUser = data;
deactivatedUser.isActive = false;
this.handlers.update(deactivatedUser)
.then(() => this.handlers.notification('Profile is deleted', 'success'))
.then(() => browserHistory.push('/login'));
return;
}
this.handlers.update({
...data,
birth: moment(data.birth).toString()
Expand All @@ -60,7 +76,8 @@ class Profile extends Component {
prepareInitialValues(account) {
return {
...account,
birth: moment(account.birth).format(DATE_FORMAT)
birth: moment(account.birth).format(DATE_FORMAT),
isActive: !account.isActive
};
}

Expand Down

0 comments on commit 4001de8

Please sign in to comment.