Skip to content

Commit

Permalink
fix: update TimeGrid on resources list change (jquense#1135)
Browse files Browse the repository at this point in the history
* Update TimeGrid on resources list change

* Show all events if resources list is not provided but event objects have resourceId accessors

* Refactor resourcec-related fix
  • Loading branch information
dmitrykrylov authored and jquense committed Dec 10, 2018
1 parent 13459b0 commit 91c6ec0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/TimeGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cn from 'classnames'
import raf from 'dom-helpers/util/requestAnimationFrame'
import React, { Component } from 'react'
import { findDOMNode } from 'react-dom'
import memoize from 'memoize-one'

import dates from './utils/dates'
import DayColumn from './DayColumn'
Expand Down Expand Up @@ -65,8 +66,6 @@ export default class TimeGrid extends Component {
this.state = { gutterWidth: undefined, isOverflowing: null }

this.scrollRef = React.createRef()

this.resources = Resources(props.resources, props.accessors)
}

componentWillMount() {
Expand Down Expand Up @@ -95,6 +94,7 @@ export default class TimeGrid extends Component {
raf.cancel(this.rafHandle)
this.rafHandle = raf(this.checkOverflow)
}

componentWillUnmount() {
window.removeEventListener('resize', this.handleResize)

Expand Down Expand Up @@ -144,9 +144,10 @@ export default class TimeGrid extends Component {
renderEvents(range, events, now) {
let { min, max, components, accessors, localizer } = this.props

const groupedEvents = this.resources.groupEvents(events)
const resources = this.memoizedResources(this.props.resources, accessors)
const groupedEvents = resources.groupEvents(events)

return this.resources.map(([id, resource], i) =>
return resources.map(([id, resource], i) =>
range.map((date, jj) => {
let daysEvents = (groupedEvents.get(id) || []).filter(event =>
dates.inRange(
Expand Down Expand Up @@ -233,7 +234,7 @@ export default class TimeGrid extends Component {
getNow={getNow}
localizer={localizer}
selected={selected}
resources={this.resources}
resources={this.memoizedResources(resources, accessors)}
selectable={this.props.selectable}
accessors={accessors}
getters={getters}
Expand Down Expand Up @@ -314,4 +315,6 @@ export default class TimeGrid extends Component {
})
}
}

memoizedResources = memoize((resources, accessors) => Resources(resources, accessors))
}
6 changes: 6 additions & 0 deletions src/utils/Resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default function Resources(resources, accessors) {

groupEvents(events) {
const eventsByResource = new window.Map()

if (!resources) {
// Return all events if resources are not provided
eventsByResource.set(NONE, events)
}

events.forEach(event => {
const id = accessors.resource(event) || NONE
let resourceEvents = eventsByResource.get(id) || []
Expand Down

0 comments on commit 91c6ec0

Please sign in to comment.