Skip to content

Commit b9b838e

Browse files
author
Vikas Agarwal
committed
Github issue#2812, Enable file attachment edit from the left side bar
— Implemented basic feature of editing the file attachment title and permissions
1 parent 199d2c5 commit b9b838e

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import _ from 'lodash'
2+
import React from 'react'
3+
import PropTypes from 'prop-types'
4+
import UserAutoComplete from '../UserAutoComplete/UserAutoComplete'
5+
6+
export class EditFileAttachment extends React.Component {
7+
constructor(props) {
8+
super(props)
9+
10+
this.state = {
11+
title : props.attachment.title,
12+
allowedUsers: props.attachment.allowedUsers
13+
}
14+
this.onUserIdChange = this.onUserIdChange.bind(this)
15+
this.handlesToUserIds = this.handlesToUserIds.bind(this)
16+
this.userIdsToHandles = this.userIdsToHandles.bind(this)
17+
}
18+
19+
handleTitleChange(event) {
20+
this.setState({title: event.target.value})
21+
}
22+
23+
userIdsToHandles(allowedUsers) {
24+
const { projectMembers } = this.props
25+
allowedUsers = allowedUsers || []
26+
return allowedUsers.map(userId => _.get(projectMembers[userId], 'handle'))
27+
}
28+
29+
handlesToUserIds(handles) {
30+
const { projectMembers } = this.props
31+
const projectMembersByHandle = _.mapKeys(projectMembers, value => value.handle)
32+
handles = handles || []
33+
return handles.filter(handle => handle).map(handle => _.get(projectMembersByHandle[handle], 'userId'))
34+
}
35+
36+
onUserIdChange(selectedHandles = '') {
37+
this.setState({
38+
allowedUsers: this.handlesToUserIds(selectedHandles.split(','))
39+
})
40+
}
41+
42+
render() {
43+
const { onCancel, onConfirm, projectMembers, loggedInUser } = this.props
44+
const { title, allowedUsers } = this.state
45+
return (
46+
<div className="modal delete-link-modal">
47+
<div className="modal-title danger">
48+
You're about to edit an attachment
49+
</div>
50+
<div className="modal-body">
51+
<label for="title">Title:</label>
52+
<input className="edit-input" type="text" value={title} onChange={this.handleTitleChange.bind(this)} name="title"/>
53+
<br />
54+
<UserAutoComplete onUpdate={this.onUserIdChange}
55+
projectMembers={projectMembers}
56+
loggedInUser={loggedInUser}
57+
selectedUsers={this.userIdsToHandles(allowedUsers).join(',')}
58+
/>
59+
<br />
60+
61+
<div className="button-area flex center">
62+
<button className="tc-btn tc-btn-default tc-btn-sm btn-cancel" onClick={onCancel}>Cancel</button>
63+
<button className="tc-btn tc-btn-warning tc-btn-sm" onClick={() => onConfirm(title, allowedUsers)}>Edit Attachment</button>
64+
</div>
65+
</div>
66+
</div>
67+
)}
68+
}
69+
70+
EditFileAttachment.propTypes = {
71+
onCancel: PropTypes.func.isRequired,
72+
onConfirm: PropTypes.func.isRequired
73+
// link: PropTypes.object.isRequired
74+
}
75+
76+
export default EditFileAttachment

src/components/LinksMenu/FileLinksMenu.jsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Panel from '../Panel/Panel'
66
import AddFiles from '../FileList/AddFiles'
77
import AddFilePermission from '../FileList/AddFilePermissions'
88
import DeleteLinkModal from './DeleteLinkModal'
9-
import EditLinkModal from './EditLinkModal'
9+
import EditFileAttachment from './EditFileAttachment'
1010
import uncontrollable from 'uncontrollable'
1111
import MobileExpandable from '../MobileExpandable/MobileExpandable'
1212
import cn from 'classnames'
@@ -18,7 +18,6 @@ import Modal from '../Modal/Modal'
1818
const FileLinksMenu = ({
1919
canAdd,
2020
canDelete,
21-
canEdit,
2221
noDots,
2322
isAddingNewLink,
2423
limit,
@@ -154,12 +153,13 @@ const FileLinksMenu = ({
154153
const onDeleteCancel = () => onDeleteIntent(-1)
155154
const handleDeleteClick = () => onDeleteIntent(idx)
156155

157-
const onEditConfirm = (title, address) => {
158-
onEdit(idx, title, address)
156+
const onEditConfirm = (title, allowedUsers) => {
157+
onEdit(idx, title, allowedUsers)
159158
onEditIntent(-1)
160159
}
161160
const onEditCancel = () => onEditIntent(-1)
162161
const handleEditClick = () => onEditIntent(idx)
162+
const canEdit = `${link.createdBy}` === `${loggedInUser.userId}`
163163
if (linkToDelete === idx) {
164164
return (
165165
<li className="delete-confirmation-modal" key={'delete-confirmation-' + idx}>
@@ -173,8 +173,10 @@ const FileLinksMenu = ({
173173
} else if (linkToEdit === idx) {
174174
return (
175175
<li className="delete-confirmation-modal" key={'delete-confirmation-' + idx}>
176-
<EditLinkModal
177-
link={link}
176+
<EditFileAttachment
177+
attachment={link}
178+
projectMembers={projectMembers}
179+
loggedInUser={loggedInUser}
178180
onCancel={onEditCancel}
179181
onConfirm={onEditConfirm}
180182
/>

src/projects/detail/containers/ProjectInfoContainer.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import { PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER,
1414
DIRECT_PROJECT_URL, SALESFORCE_PROJECT_LEAD_LINK, PROJECT_STATUS_CANCELLED, PROJECT_ATTACHMENTS_FOLDER,
1515
PROJECT_FEED_TYPE_PRIMARY, PHASE_STATUS_DRAFT } from '../../../config/constants'
1616
import ProjectInfo from '../../../components/ProjectInfo/ProjectInfo'
17-
import { addProjectAttachment, uploadProjectAttachments, discardAttachments, changeAttachmentPermission } from '../../actions/projectAttachment'
17+
import {
18+
addProjectAttachment, updateProjectAttachment, uploadProjectAttachments, discardAttachments, changeAttachmentPermission
19+
} from '../../actions/projectAttachment'
1820

1921
class ProjectInfoContainer extends React.Component {
2022

@@ -28,6 +30,7 @@ class ProjectInfoContainer extends React.Component {
2830
this.onAddNewLink = this.onAddNewLink.bind(this)
2931
this.onDeleteLink = this.onDeleteLink.bind(this)
3032
this.onEditLink = this.onEditLink.bind(this)
33+
this.onEditAttachment = this.onEditAttachment.bind(this)
3134
this.onAddFile = this.onAddFile.bind(this)
3235
this.onUploadAttachment = this.onUploadAttachment.bind(this)
3336
this.onSubmitForReview = this.onSubmitForReview.bind(this)
@@ -109,6 +112,19 @@ class ProjectInfoContainer extends React.Component {
109112
})
110113
}
111114

115+
onEditAttachment(idx, title, allowedUsers) {
116+
const { project, updateProjectAttachment } = this.props
117+
const updatedAttachment = {
118+
title,
119+
allowedUsers
120+
}
121+
const attachment = project.attachments[idx]
122+
updateProjectAttachment(project.id,
123+
attachment.id,
124+
updatedAttachment
125+
)
126+
}
127+
112128
onDeleteProject() {
113129
const { deleteProject, project } = this.props
114130
deleteProject(project.id)
@@ -175,6 +191,8 @@ class ProjectInfoContainer extends React.Component {
175191
.map(attachment => ({
176192
title: attachment.title,
177193
address: attachment.downloadUrl,
194+
allowedUsers: attachment.allowedUsers,
195+
createdBy : attachment.createdBy
178196
}))
179197

180198
// get list of phase topic in same order as phases
@@ -249,6 +267,7 @@ class ProjectInfoContainer extends React.Component {
249267
links={attachments}
250268
title="Files"
251269
canAdd={enableFileUpload}
270+
onEdit={this.onEditAttachment}
252271
onAddNewLink={this.onAddFile}
253272
onAddAttachment={addProjectAttachment}
254273
onUploadAttachment={this.onUploadAttachment}
@@ -305,7 +324,7 @@ const mapStateToProps = ({ templates, projectState, members, loadUser }) => {
305324
})
306325
}
307326

308-
const mapDispatchToProps = { updateProject, deleteProject, addProjectAttachment,
327+
const mapDispatchToProps = { updateProject, deleteProject, addProjectAttachment, updateProjectAttachment,
309328
discardAttachments, uploadProjectAttachments, loadDashboardFeeds, loadPhaseFeed, changeAttachmentPermission }
310329

311330
export default connect(mapStateToProps, mapDispatchToProps)(ProjectInfoContainer)

0 commit comments

Comments
 (0)