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

Alexandru Munteanu
committed
import { Tag, Label, Row, Item, PersonInfo, IconButton } from './'
const Empty = () => <div />
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
81
82
83
84
85
86
87
88
// #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>
<AuthorTitle
editMode={editMode}
isCorresponding={author.isCorresponding}
isSubmitting={author.isSubmitting}
listIndex={listIndex}
saveChanges={handleSubmit}
toggleEditMode={toggleEditMode}
/>
<Field component={Empty} name="id" />
<Field component={Empty} name="isSubmitting" />
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<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(
withProps(({ author }) => ({
initialValues: {
...author,
},
})),
reduxForm({
form: 'author-edit',
}),
)(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 = ({

Alexandru Munteanu
committed
editMode,
dragHandle,
toggleEdit,
index = null,
saveNewAuthor,
editExistingAuthor,
authorEditorSubmit,
isOver,
isDragging,

Alexandru Munteanu
committed
}) => (
<Root isDragging={isDragging} isOver={isOver}>

Alexandru Munteanu
committed
{!editMode &&
(typeof dragHandle === 'function' ? dragHandle() : dragHandle)}
{editMode ? (
<EnhancedAuthorEdit
author={item}
editExistingAuthor={editExistingAuthor}
listIndex={index}
onSubmit={authorEditorSubmit}
saveNewAuthor={saveNewAuthor}
toggleEditMode={toggleEdit(index)}
listIndex={index}
toggleEditMode={toggleEdit(index)}
/>
)}
</Root>
)
export default compose(
withState('editMode', 'setEditMode', ({ item }) => item.id === 'newAuthor'),
withHandlers({
toggleEdit: ({ setEditMode, onEdit }) => index => () => {
onEdit(index)
setEditMode(e => !e)
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: calc(${th('gridUnit')} * 2);

Alexandru Munteanu
committed
`

Alexandru Munteanu
committed
align-items: center;
background-color: ${props =>
props.isOver ? th('colorFurniture') : th('colorBackgroundHue')};
border-radius: ${th('borderRadius')};
box-shadow: ${th('boxShadow')};

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