Skip to content
Snippets Groups Projects
Commit 42572afc authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files

refactor(component): add link to edit and extract utils

parent 7a2e6e32
No related branches found
No related tags found
No related merge requests found
import React from 'react' import React from 'react'
import { get, map } from 'lodash' import { get } from 'lodash'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { reduxForm, SubmissionError } from 'redux-form' import { reduxForm, SubmissionError } from 'redux-form'
import styled from 'styled-components' import styled from 'styled-components'
...@@ -13,13 +13,12 @@ import { compose, withProps, withHandlers, withState } from 'recompose' ...@@ -13,13 +13,12 @@ import { compose, withProps, withHandlers, withState } from 'recompose'
import AddUserForm from './AddUserForm' import AddUserForm from './AddUserForm'
import EditUserForm from './EditUserForm' import EditUserForm from './EditUserForm'
import { getRoleOptions, setAdmin, parseUpdateUser } from './utils'
const getRoleOptions = journal =>
map(journal.roles, (value, key) => ({ label: value, value: key }))
const onSubmit = (values, dispatch, { isEdit, history }) => { const onSubmit = (values, dispatch, { isEdit, history }) => {
if (!isEdit) { if (!isEdit) {
return create('/users/invite', values) const newValues = setAdmin(values)
return create('/users/invite', newValues)
.then(r => history.push('/admin/users')) .then(r => history.push('/admin/users'))
.catch(error => { .catch(error => {
const err = get(error, 'response') const err = get(error, 'response')
...@@ -32,7 +31,7 @@ const onSubmit = (values, dispatch, { isEdit, history }) => { ...@@ -32,7 +31,7 @@ const onSubmit = (values, dispatch, { isEdit, history }) => {
}) })
} }
return update(`/users/${values.id}`, values) return update(`/users/${values.id}`, parseUpdateUser(values))
.then(() => { .then(() => {
history.push('/admin/users') history.push('/admin/users')
}) })
...@@ -47,7 +46,7 @@ const onSubmit = (values, dispatch, { isEdit, history }) => { ...@@ -47,7 +46,7 @@ const onSubmit = (values, dispatch, { isEdit, history }) => {
}) })
} }
const AddEditUser = ({ handleSubmit, journal, isEdit, user }) => ( const AddEditUser = ({ handleSubmit, journal, isEdit, user, history }) => (
<Root> <Root>
<FormContainer onSubmit={handleSubmit}> <FormContainer onSubmit={handleSubmit}>
{isEdit ? ( {isEdit ? (
...@@ -60,6 +59,7 @@ const AddEditUser = ({ handleSubmit, journal, isEdit, user }) => ( ...@@ -60,6 +59,7 @@ const AddEditUser = ({ handleSubmit, journal, isEdit, user }) => (
<AddUserForm journal={journal} roles={getRoleOptions(journal)} /> <AddUserForm journal={journal} roles={getRoleOptions(journal)} />
)} )}
<Row> <Row>
<Button onClick={history.goBack}>Back</Button>
<Button primary type="submit"> <Button primary type="submit">
Save user Save user
</Button> </Button>
......
...@@ -5,6 +5,7 @@ import styled from 'styled-components' ...@@ -5,6 +5,7 @@ import styled from 'styled-components'
import { Icon, Menu } from '@pubsweet/ui' import { Icon, Menu } from '@pubsweet/ui'
import { actions } from 'pubsweet-client' import { actions } from 'pubsweet-client'
import { ConnectPage } from 'xpub-connect' import { ConnectPage } from 'xpub-connect'
import { withJournal } from 'xpub-journal'
import { withRouter } from 'react-router-dom' import { withRouter } from 'react-router-dom'
import { compose, withState, withHandlers } from 'recompose' import { compose, withState, withHandlers } from 'recompose'
...@@ -13,13 +14,16 @@ import { Pagination } from './' ...@@ -13,13 +14,16 @@ import { Pagination } from './'
const TableRow = ({ const TableRow = ({
toggleUser, toggleUser,
selected, selected,
id,
email, email,
roles, roles,
username, username,
title = '',
firstName = '', firstName = '',
lastName = '', lastName = '',
affiliation, affiliation,
isConfirmed, isConfirmed,
roleOptions,
}) => ( }) => (
<Row> <Row>
<td> <td>
...@@ -28,10 +32,20 @@ const TableRow = ({ ...@@ -28,10 +32,20 @@ const TableRow = ({
<td>{email}</td> <td>{email}</td>
<td>{`${firstName} ${lastName}`}</td> <td>{`${firstName} ${lastName}`}</td>
<td>{affiliation}</td> <td>{affiliation}</td>
<td>{roles && roles.map(r => <div key={r}>{r}</div>)}</td> <td>
<Status> {roles &&
<span>{isConfirmed ? 'Confirmed' : 'Invited'}</span> roles.map((r, i, arr) => (
</Status> <Role key={r}>{`${roleOptions[r]}${
i !== arr.length - 1 ? ', ' : ''
}`}</Role>
))}
</td>
<td>
<Tag>{isConfirmed ? 'Confirmed' : 'Invited'}</Tag>
</td>
<td>
<Action href={`/admin/users/edit/${id}`}>Edit</Action>
</td>
</Row> </Row>
) )
...@@ -44,13 +58,17 @@ const Users = ({ ...@@ -44,13 +58,17 @@ const Users = ({
page, page,
itemsPerPage, itemsPerPage,
history, history,
journal,
}) => ( }) => (
<div> <div>
<Header> <Header>
<span>Users</span> <BreadCrumbs>
<span>Admin Dashboard</span>
<span>Users</span>
</BreadCrumbs>
<AddButton onClick={() => history.push('/admin/users/add')}> <AddButton onClick={() => history.push('/admin/users/add')}>
<Icon color="#667080">plus-circle</Icon> <Icon color="#667080">plus-circle</Icon>
Add User &nbsp; Add User
</AddButton> </AddButton>
</Header> </Header>
<SubHeader> <SubHeader>
...@@ -99,13 +117,19 @@ const Users = ({ ...@@ -99,13 +117,19 @@ const Users = ({
<td>Email</td> <td>Email</td>
<td>Full name</td> <td>Full name</td>
<td>Affiliation</td> <td>Affiliation</td>
<td>Roles</td> <td width="200">Roles</td>
<td>Status</td> <td>Status</td>
<td width="50" />
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{users.map(u => ( {users.map(u => (
<TableRow key={u.id} {...u} toggleUser={toggleUser(u)} /> <TableRow
key={u.id}
{...u}
roleOptions={journal.roles}
toggleUser={toggleUser(u)}
/>
))} ))}
</tbody> </tbody>
</Table> </Table>
...@@ -115,6 +139,7 @@ const Users = ({ ...@@ -115,6 +139,7 @@ const Users = ({
export default compose( export default compose(
ConnectPage(() => [actions.getUsers()]), ConnectPage(() => [actions.getUsers()]),
withRouter, withRouter,
withJournal,
connect(state => ({ currentUsers: get(state, 'users.users') })), connect(state => ({ currentUsers: get(state, 'users.users') })),
withState('users', 'setUsers', props => withState('users', 'setUsers', props =>
props.currentUsers.map(u => ({ ...u, selected: false })), props.currentUsers.map(u => ({ ...u, selected: false })),
...@@ -160,13 +185,24 @@ const Header = styled.div` ...@@ -160,13 +185,24 @@ const Header = styled.div`
display: flex; display: flex;
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
`
const BreadCrumbs = styled.div`
& span { & span {
font-family: Helvetica; font-size: 17px;
font-size: 24px;
font-weight: bold;
text-align: left; text-align: left;
color: #667080; color: #667080;
&:after {
content: '>';
padding: 0 10px;
}
&:last-child {
font-size: 24px;
font-weight: bold;
&:after {
content: '';
}
} }
` `
...@@ -196,7 +232,7 @@ const Table = styled.table` ...@@ -196,7 +232,7 @@ const Table = styled.table`
border-spacing: 0; border-spacing: 0;
border-collapse: collapse; border-collapse: collapse;
margin-top: 10px; margin-top: 10px;
width: 100vw; width: 100%;
& thead tr { & thead tr {
height: 40px; height: 40px;
...@@ -216,19 +252,37 @@ const Row = styled.tr` ...@@ -216,19 +252,37 @@ const Row = styled.tr`
font-size: 14px; font-size: 14px;
height: 40px; height: 40px;
text-align: left; text-align: left;
&:hover {
background-color: #e6e7e9;
a {
display: block;
}
}
` `
const Status = styled.td` const Tag = styled.span`
& span { border: solid 1px #667080;
border: solid 1px #667080; text-transform: uppercase;
text-transform: uppercase; font-family: Helvetica;
font-family: Helvetica; font-size: 12px;
font-size: 12px; font-weight: bold;
font-weight: bold; text-align: left;
text-align: left; color: #667080;
color: #667080; padding: 2px 10px;
padding: 2px 10px; margin-right: 5px;
} `
const Role = styled.span`
height: 17px;
font-size: 14px;
text-align: left;
color: #667080;
text-transform: uppercase;
`
const Action = styled.a`
color: #667080;
display: none;
` `
const Input = styled.input` const Input = styled.input`
......
import { pick, map } from 'lodash'
export const getRoleOptions = journal =>
map(journal.roles, (value, key) => ({ label: value, value: key }))
export const setAdmin = values => {
const newValues = { ...values }
if (newValues.roles && newValues.roles.includes('admin')) {
newValues.admin = true
} else {
newValues.admin = false
}
return newValues
}
export const parseUpdateUser = values => {
const valuesToSave = [
'admin',
'firstName',
'lastName',
'affiliation',
'title',
'roles',
'rev',
]
const newValues = setAdmin(values)
return pick(newValues, valuesToSave)
}
...@@ -23,8 +23,4 @@ export default [ ...@@ -23,8 +23,4 @@ export default [
label: 'Professor', label: 'Professor',
value: 'prof', value: 'prof',
}, },
{
label: 'Other',
value: 'other',
},
] ]
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment