Newer
Older
import React, { Fragment } from 'react'
import { isNumber } from 'lodash'
import styled from 'styled-components'
import { reduxForm } from 'redux-form'
import { th } from '@pubsweet/ui-toolkit'
import { required } from 'xpub-validators'
import { H3, ValidatedField, TextField, Checkbox } from '@pubsweet/ui'
import { compose, setDisplayName, withStateHandlers } from 'recompose'

Alexandru Munteanu
committed
import { Tag, Label, Row, Item, PersonInfo, IconButton } from './'
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// #region AuthorTitle
const AuthorTitle = ({
editMode,
listIndex,
saveChanges,
isSubmitting,
toggleEditMode,
isCorresponding,
}) => (
<Fragment>
{!editMode ? (
<IconButton
icon="edit-2"
iconSize={2}
onClick={toggleEditMode}
right={8}
top={15}
/>
) : (
<Fragment>
<IconButton
icon="check-circle"
iconSize={2}
onClick={saveChanges}
right={36}
top={15}
/>
<IconButton
icon="x-circle"
iconSize={2}
onClick={toggleEditMode}
right={8}
top={15}
/>
</Fragment>
)}
<Row alignItems="center" justify="flex-start">
<H3>{isNumber(listIndex) ? `#${listIndex + 1} Author` : 'Author'}</H3>
{!editMode ? (
<AuthorTags>
{isSubmitting && <Tag>Submitting</Tag>}
{isCorresponding && <Tag>Corresponding</Tag>}
</AuthorTags>
) : (
<ValidatedField
component={({ value, onChange }) => (
<Checkbox
checked={value}
label="Set corresponding"
onChange={onChange}
value={value}
/>
)}
name="isCorresponding"
/>
)}
</Row>
</Fragment>
)
// #endregion
// #region AuthorEdit
const AuthorEdit = ({
author,
editMode,
listIndex,
handleSubmit,
toggleEditMode,
}) => (

Alexandru Munteanu
committed
<AuthorContainer>
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<AuthorTitle
editMode={editMode}
isCorresponding={author.isCorresponding}
isSubmitting={author.isSubmitting}
listIndex={listIndex}
saveChanges={handleSubmit}
toggleEditMode={toggleEditMode}
/>
<Row>
<Item vertical withRightMargin>
<Label required>Email</Label>
<ValidatedField
component={TextField}
name="email"
validate={[required]}
/>
</Item>
<Item vertical withRightMargin>
<Label required>First name</Label>
<ValidatedField
component={TextField}
name="firstName"
validate={[required]}
/>
</Item>
<Item vertical withRightMargin>
<Label required>Last name</Label>
<ValidatedField
component={TextField}
name="lastName"
validate={[required]}
/>
</Item>
<Item vertical>
<Label required>Affiliation</Label>
<ValidatedField
component={TextField}
name="affiliation"
validate={[required]}
/>
</Item>
</Row>

Alexandru Munteanu
committed
</AuthorContainer>
)
// #endregion
const EnhancedAuthorEdit = compose(
reduxForm({
form: 'author-edit',
onSubmit: values => {},
}),
)(AuthorEdit)
const Author = ({ author, listIndex, toggleEditMode }) => (

Alexandru Munteanu
committed
<AuthorContainer>
<AuthorTitle
isCorresponding={author.isCorresponding}
isSubmitting={author.isSubmitting}
listIndex={listIndex}
toggleEditMode={toggleEditMode}
/>
<PersonInfo person={author} />

Alexandru Munteanu
committed
</AuthorContainer>

Alexandru Munteanu
committed
const AuthorCard = ({
author,
editMode,
dragHandle,
toggleEditMode,
listIndex = null,
}) => (

Alexandru Munteanu
committed
{!editMode &&
(typeof dragHandle === 'function' ? dragHandle() : dragHandle)}
{editMode ? (
<EnhancedAuthorEdit
author={author}
editMode={editMode}
listIndex={listIndex}
toggleEditMode={toggleEditMode}
/>
) : (
<Author
author={author}
editMode={editMode}
listIndex={listIndex}
toggleEditMode={toggleEditMode}
/>
)}
</Root>
)
export default compose(
withStateHandlers(
{ editMode: false },
{
toggleEditMode: ({ editMode }, { onEdit }) => () => {

Alexandru Munteanu
committed
typeof onEdit === 'function' && onEdit()
return {
editMode: !editMode,
}
},
},
),
setDisplayName('AuthorCard'),
)(AuthorCard)
// #region styles
const AuthorTags = styled.div`
align-items: center;
display: flex;
${Tag} {
margin-right: ${th('gridUnit')};
}
`

Alexandru Munteanu
committed
const AuthorContainer = styled.div`
display: flex;
flex: 1;
flex-direction: column;
padding: ${th('gridUnit')};
`

Alexandru Munteanu
committed
align-items: center;
border-radius: ${th('borderRadius')};
box-shadow: ${th('boxShadow')};

Alexandru Munteanu
committed
display: flex;
justify-content: flex-start;