Skip to content

Commit

Permalink
fix(fixture): Remove fixture from scene and venue
Browse files Browse the repository at this point in the history
When deleting a fixture, it will now also be removed from the related scenes and venues.

fix #83
  • Loading branch information
TimPietrusky committed Oct 11, 2019
1 parent 02ea464 commit 5ce5d6f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
21 changes: 17 additions & 4 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import * as utils from '../utils/index.js'

export * from './timeline.js'
export * from './animation.js'
export * from './venue.js'

import { setVenueSlot } from './venue.js'

/*
*
Expand Down Expand Up @@ -289,10 +292,20 @@ export const removeFixtureFromEverywhere = fixtureId => {
utils.clearFixtureInBatch(fixtureId)

// Remove fixture from all scenes
selectors.getScenes(getState()).map(scene => {
if (scene.fixtures.indexOf(fixtureId) > -1) {
dispatch(removeFixtureFromScene(scene.id, scene.fixtures.indexOf(fixtureId)))
}
selectors.getScenesWithFixture(getState(), { fixtureId }).map(scene => {
dispatch(removeFixtureFromScene(scene.id, fixtureId))
})

// Remove fixture from all venues
selectors.getVenuesWithFixture(getState(), { fixtureId }).map(venue => {
venue.slots
// Find all slots that contain the fixture
.filter(slot => slot.fixtures.includes(fixtureId))
.map(slot => {
// Remove the fixture from the list of fixtures of this slot
const fixtures = slot.fixtures.filter(_fixtureId => _fixtureId !== fixtureId)
dispatch(setVenueSlot(venue.id, { id: slot.id, fixtures }))
})
})

dispatch(removeFixture(fixtureId))
Expand Down
27 changes: 10 additions & 17 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createSelector } from 'reselect/src/index.js'
import { collator } from '../utils/index.js'

export * from './venue.js'

/*
* Selectors help you to retrieve data from the state so you don't have to write the
* same code to access the state over and over again. It also helps to have a central position
Expand Down Expand Up @@ -30,7 +32,6 @@ export const getDekk = state => state.dekkManager
export const getDekkConnected = state => state.dekkManager.connected
export const getDekkData = state => state.dekkManager.data
export const getUsbDmxControllerConnected = state => state.connectionManager.usb.connected
export const getVenues = state => state.venueManager

export const getAnimation = (state, properties) => {
return getAnimations(state)
Expand Down Expand Up @@ -77,6 +78,14 @@ export const getScenesWithAnimation = (state, properties) => {
.filter(scene => scene.animations.includes(properties.animationId))
}

/*
* Get scenes that contain a certain fixtureId
*/
export const getScenesWithFixture = (state, properties) => {
return getScenes(state)
.filter(scene => scene.fixtures.includes(properties.fixtureId))
}

/*
* Get a specific fixture by using the fixtureId
*/
Expand All @@ -93,22 +102,6 @@ export const getFixtureByName = (state, properties) => {
.filter(fixture => fixture.name === properties.name)[0]
}

/*
* Get a specific fixture by using the fixtureId
*/
export const getVenue = (state, properties) => {
return getVenues(state)
.filter(venue => venue.id === properties.venueId)[0]
}

/*
* Sort venues by venue.name
*/
export const getVenuesSorted = createSelector(
getVenues,
venues => venues.sort((a, b) => collator.compare(a.name, b.name))
)

/*
* Sort fixtures by fixture.name
*/
Expand Down
41 changes: 41 additions & 0 deletions src/selectors/venue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createSelector } from 'reselect/src/index.js'

export const getVenues = state => state.venueManager

/*
* Get a specific fixture by using the fixtureId
*/
export const getVenue = (state, properties) => {
return getVenues(state)
.filter(venue => venue.id === properties.venueId)[0]
}

/*
* Sort venues by venue.name
*/
export const getVenuesSorted = createSelector(
getVenues,
venues => venues.sort((a, b) => collator.compare(a.name, b.name))
)

/*
* Get venues that contain a specific fixture
*/
export const getVenuesWithFixture = (state, properties) => {
return getVenues(state)
.filter(venue => {
return venue.slots.filter(slot => slot.fixtures.includes(properties.fixtureId))
})
}

/*
* Get venues that contain a specific fixture
*/
export const getVenueSlotsWithFixture = (state, properties) => {
return getVenues(state)
.filter(venue => {
return venue.slots.filter(slot => slot.fixtures.includes(properties.fixtureId))
})
.map(venue => venue.slots)
.filter(slot => slot.fixtures.includes(properties.fixtureId))
}

0 comments on commit 5ce5d6f

Please sign in to comment.