Skip to content

Commit b45d3d2

Browse files
authored
Merge pull request #381 from kblazelek/master
Add ability to update single card through event bus
2 parents 56a3d56 + e63de74 commit b45d3d2

File tree

7 files changed

+51
-0
lines changed

7 files changed

+51
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ const setEventBus = (handle) => {
271271
//To add a card
272272
eventBus.publish({type: 'ADD_CARD', laneId: 'COMPLETED', card: {id: "M1", title: "Buy Milk", label: "15 mins", description: "Also set reminder"}})
273273

274+
//To update a card
275+
eventBus.publish({type: 'UPDATE_CARD', laneId: 'COMPLETED', card: {id: "M1", title: "Buy Milk (Updated)", label: "20 mins", description: "Also set reminder (Updated)"}})
276+
274277
//To remove a card
275278
eventBus.publish({type: 'REMOVE_CARD', laneId: 'PLANNED', cardId: "M1"})
276279

src/actions/LaneActions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createAction} from 'redux-actions'
22

33
export const addCard = createAction('ADD_CARD')
4+
export const updateCard = createAction('UPDATE_CARD')
45
export const removeCard = createAction('REMOVE_CARD')
56
export const moveCardAcrossLanes = createAction('MOVE_CARD')
67
export const updateCards = createAction('UPDATE_CARDS')

src/controllers/BoardContainer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class BoardContainer extends Component {
6363
switch (event.type) {
6464
case 'ADD_CARD':
6565
return actions.addCard({laneId: event.laneId, card: event.card})
66+
case 'UPDATE_CARD':
67+
return actions.updateCard({laneId: event.laneId, card: event.card})
6668
case 'REMOVE_CARD':
6769
return actions.removeCard({laneId: event.laneId, cardId: event.cardId})
6870
case 'REFRESH_BOARD':

src/helpers/LaneHelper.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ const LaneHelper = {
6969
return update(state, {lanes: {$set: lanes}})
7070
},
7171

72+
updateCardFromLane: (state, {laneId, card}) => {
73+
const laneIndex = state.lanes.findIndex(x => x.id === laneId)
74+
if (laneIndex < 0) {
75+
return state
76+
}
77+
const cardIndex = state.lanes[laneIndex].cards.findIndex(x => x.id === card.id)
78+
if (cardIndex < 0) {
79+
return state
80+
}
81+
return update(state, {
82+
lanes: {
83+
[laneIndex]: {
84+
cards: {
85+
[cardIndex]: {
86+
$set: card
87+
}
88+
}
89+
}
90+
}
91+
})
92+
},
93+
7294
moveCardAcrossLanes: (state, {fromLaneId, toLaneId, cardId, index}) => {
7395
let cardToMove = null
7496
const interimLanes = state.lanes.map(lane => {

src/reducers/BoardReducer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const boardReducer = (state = {lanes: []}, action) => {
77
return Lh.initialiseLanes(state, payload)
88
case 'ADD_CARD':
99
return Lh.appendCardToLane(state, payload)
10+
case 'UPDATE_CARD':
11+
return Lh.updateCardFromLane(state, payload)
1012
case 'REMOVE_CARD':
1113
return Lh.removeCardFromLane(state, payload)
1214
case 'MOVE_CARD':

stories/Realtime.story.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class RealtimeBoard extends Component {
4343
this.setState({boardData: newData})
4444
}
4545

46+
updateCard = () => {
47+
this.state.eventBus.publish({
48+
type: 'UPDATE_CARD',
49+
laneId: 'PLANNED',
50+
card: {id: 'Plan2', title: 'UPDATED Dispose Garbage', label: '45 mins', description: 'UPDATED Sort out recyclable and waste as needed'}
51+
})
52+
}
53+
4654
prioritizeWriteBlog = () => {
4755
this.state.eventBus.publish({
4856
type: 'MOVE_CARD',
@@ -76,6 +84,9 @@ class RealtimeBoard extends Component {
7684
<button onClick={this.prioritizeWriteBlog} style={{margin: 5}}>
7785
Prioritize Write Blog
7886
</button>
87+
<button onClick={this.updateCard} style={{margin: 5}}>
88+
Update Dispose Garbage
89+
</button>
7990
<Board data={this.state.boardData} onDataChange={this.shouldReceiveNewData} eventBusHandle={this.setEventBus} />
8091
</div>
8192
)

tests/__snapshots__/Storyshots.test.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,6 +3781,16 @@ exports[`Storyshots Advanced Features Realtime Events 1`] = `
37813781
>
37823782
Prioritize Write Blog
37833783
</button>
3784+
<button
3785+
onClick={[Function]}
3786+
style={
3787+
Object {
3788+
"margin": 5,
3789+
}
3790+
}
3791+
>
3792+
Update Dispose Garbage
3793+
</button>
37843794
<div
37853795
className="react-trello-board c0"
37863796
data={

0 commit comments

Comments
 (0)