Skip to content

Commit ccb4afb

Browse files
feat(Labels): create new labels
1 parent 350c6a1 commit ccb4afb

File tree

6 files changed

+64
-30
lines changed

6 files changed

+64
-30
lines changed

lib/common/components/FeedLabel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const getComplementaryColor = (cssHex, strength) => {
1616

1717
export default class FeedLabel extends React.Component {
1818
_onConfirmDelete = () => {
19-
this.props.deleteLabel(this.props)
19+
this.props.deleteLabel(this.props.label)
2020
}
2121

2222
_onClickDelete = () => {
@@ -35,7 +35,7 @@ export default class FeedLabel extends React.Component {
3535
className={`feedLabel ${small ? 'small' : ''}`}
3636
style={{
3737
backgroundColor: label.color,
38-
color: getComplementaryColor(label.color, 40),
38+
color: getComplementaryColor(label.color, 55),
3939
borderColor: getComplementaryColor(label.color, 10)
4040
}}
4141
title={label.description}

lib/manager/actions/labels.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ const LABEL_URL = '/api/manager/secure/label'
1515
*/
1616
export function createLabel (label: Label) {
1717
return function (dispatch: dispatchFn, getState: getStateFn) {
18+
console.log('createlabel')
19+
console.log(label)
1820
return dispatch(secureFetch(LABEL_URL, 'post', label))
19-
.then((res) => res.json())
20-
.then((createdLabel) => {
21+
.then(async (res) => {
22+
const createdLabel = await res.json()
2123
dispatch(fetchProject(createdLabel.projectId))
2224
dispatch(fetchProjectFeeds(createdLabel.projectId))
2325
})

lib/manager/components/LabelEditor.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ export default class LabelEditor extends React.Component {
3030

3131
componentDidMount = () => {
3232
// If we didn't get a label passed in, declare the label to be new
33-
if (!this.props.label) {
33+
if (Object.keys(this.props.label).length === 0) {
3434
this.setState({
35-
labelIsNew: true
35+
labelIsNew: true,
36+
// Default to a nice orange
37+
newLabel: {
38+
color: '#FF9200'
39+
}
3640
})
3741
}
3842
}
@@ -47,23 +51,24 @@ export default class LabelEditor extends React.Component {
4751
const {name, value, type, checked} = target
4852

4953
const universalValue = type === 'checkbox' ? checked : value
50-
const valid = alwaysValid || universalValue.length
54+
const valid = alwaysValid || universalValue.length > 0
5155

5256
this.setState(
5357
update(this.state, {
5458
newLabel: { $merge: {[name]: universalValue} },
55-
validation: { [name]: { $set: universalValue !== undefined && valid } }
59+
validation: { [name]: { $set: universalValue && valid } }
5660
})
5761
)
5862
}
5963

6064
_getChanges = () => {
6165
const newLabel = this.state.newLabel
62-
const oldLabel = this.props.label || {}
66+
const oldLabel = this.props.label
67+
6368
const changes: any = {}
64-
Object.keys(oldLabel).map(k => {
65-
if (oldLabel[k] !== newLabel[k]) {
66-
changes[k] = oldLabel[k]
69+
Object.keys(newLabel).map(k => {
70+
if (newLabel[k] !== oldLabel[k]) {
71+
changes[k] = newLabel[k]
6772
}
6873
})
6974
return changes
@@ -80,6 +85,8 @@ export default class LabelEditor extends React.Component {
8085
const {createLabel, updateLabel} = this.props
8186

8287
if (labelIsNew) {
88+
// Insert project ID
89+
label.projectId = this.props.projectId
8390
createLabel(label)
8491
} else {
8592
updateLabel(label)

lib/manager/components/LabelEditorModal.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {Label} from '../../types'
88

99
type LabelEditorModalProps = {
1010
label?: Label,
11+
projectId?: String
1112
}
1213

1314
type LabelEditorModalState = {
@@ -37,15 +38,16 @@ export default class LabelEditorModal extends React.Component<LabelEditorModalPr
3738

3839
render () {
3940
const {Body, Header, Title} = Modal
40-
const {label} = this.props
41+
const label = this.props.label || {}
42+
const { projectId } = this.props
4143
return (
4244
<Modal show={this.state.showModal} onHide={this.close}>
4345
<Header>
4446
<Title>Editing {label.name ? 'Label' : 'New Label'}</Title>
4547
</Header>
4648

4749
<Body>
48-
<LabelEditor label={label} onDone={this.close} />
50+
<LabelEditor label={label} onDone={this.close} projectId={projectId} />
4951
</Body>
5052
</Modal>
5153
)

lib/manager/components/ProjectViewer.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type {Props as ContainerProps} from '../containers/ActiveProjectViewer'
3232
import type {Feed, Project} from '../../types'
3333
import type {ManagerUserState} from '../../types/reducers'
3434

35+
import LabelEditorModal from './LabelEditorModal'
3536
import CreateFeedSource from './CreateFeedSource'
3637
import ProjectSettings from './ProjectSettings'
3738

@@ -308,23 +309,28 @@ class ProjectSummaryPanel extends Component<{feedSources: Array<Feed>, project:
308309
}
309310
}
310311

311-
const LabelPanel = ({ project }) => {
312-
const { labels } = project
312+
class LabelPanel extends Component<{project: Project}> {
313+
_onClickNewLabel = () => this.refs.newLabelModal.open()
314+
render () {
315+
const { labels, id: projectId } = this.props.project
313316

314-
let labelBody = 'There are no labels in this project.'
315-
if (labels.length > 0) {
316-
labelBody = labels.map((label) => (
317-
<FeedLabel key={label.id} label={label} />
318-
))
319-
}
317+
let labelBody = 'There are no labels in this project.'
318+
if (labels.length > 0) {
319+
labelBody = labels.map((label) => (
320+
<FeedLabel key={label.id} label={label} />
321+
))
322+
}
320323

321-
return (
322-
<Panel header={<h3>Labels</h3>}>
323-
<div className='feedLabelContainer'>
324-
{labelBody}
325-
</div>
326-
</Panel>
327-
)
324+
return (
325+
<Panel header={<h3>Labels</h3>}>
326+
<div className='feedLabelContainer'>
327+
<LabelEditorModal ref='newLabelModal' projectId={projectId} />
328+
<button className='newLabel' onClick={this._onClickNewLabel}>Add a new label</button>
329+
{labelBody}
330+
</div>
331+
</Panel>
332+
)
333+
}
328334
}
329335

330336
const ExplanatoryPanel = ({ project }) => {

lib/style.css

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ td.feed-source-info {
191191
padding: 0;
192192
}
193193

194-
/* Delete Button */
194+
.feedLabel .labelName i {
195+
padding-right: 2px;
196+
}
197+
198+
/* Label Buttons */
195199
.feedLabel .actionButtons {
196200
display: flex;
197201
align-items: flex-end;
@@ -207,6 +211,19 @@ td.feed-source-info {
207211
opacity: 1;
208212
}
209213

214+
/* New Label "button" */
215+
.newLabel {
216+
background: none;
217+
border: none;
218+
margin: 0 0 10px 0;
219+
padding: 0;
220+
color: #337ab7;
221+
text-decoration: none;
222+
}
223+
.newLabel:hover {
224+
text-decoration: underline;
225+
}
226+
210227
/* Feed transformation substitution styling */
211228
.substitution-row td {
212229
border-top: none;

0 commit comments

Comments
 (0)