소스 검색

fixes. EventApi computed props

Adam Shaw 7 년 전
부모
커밋
2e1701a050
2개의 변경된 파일49개의 추가작업 그리고 41개의 파일을 삭제
  1. 40 22
      src/api/EventApi.ts
  2. 9 19
      src/reducers/eventStore.ts

+ 40 - 22
src/api/EventApi.ts

@@ -1,5 +1,6 @@
 import Calendar from '../Calendar'
 import { EventDef, EventInstance } from '../structs/event'
+import { EventMutation } from '../structs/event-mutation'
 
 export default class EventApi {
 
@@ -14,42 +15,45 @@ export default class EventApi {
   }
 
   updateProp(name: string, val: string) {
-    let { instance } = this
+    if (name.match(/^(start|end|date|isAllDay)$/)) {
+      // error. date-related props need other methods
+    } else {
+      let props
 
-    if (instance) {
-      if (name.match(/^(start|end|date|isAllDay)$/)) {
-        // error. date-related props need other methods
+      // TODO: consolidate this logic with event struct?
+      if (name === 'editable') {
+        props = { startEditable: val, durationEditable: val }
+      } else if (name === 'color') {
+        props = { backgroundColor: val, borderColor: val }
       } else {
-        let props
-
-        if (name === 'color') { // TODO: consolidate this logic with event struct?
-          props = { backgroundColor: val, borderColor: val }
-        } else {
-          props = { [name]: val }
-        }
-
-        this.calendar.dispatch({
-          type: 'MUTATE_EVENTS',
-          instanceId: instance.instanceId,
-          mutation: {
-            standardProps: props
-          }
-        })
+        props = { [name]: val }
       }
+
+      this.mutate({
+        standardProps: props
+      })
     }
   }
 
   updateExtendedProp(name: string, val: string) {
+    this.mutate({
+      extendedProps: { [name]: val }
+    })
+  }
+
+  private mutate(mutation: EventMutation) {
     let { instance } = this
 
     if (instance) {
       this.calendar.dispatch({
         type: 'MUTATE_EVENTS',
         instanceId: instance.instanceId,
-        mutation: {
-          extendedProps: { [name]: val }
-        }
+        mutation
       })
+
+      let eventStore = this.calendar.state.eventStore
+      this.def = eventStore.defs[this.def.defId]
+      this.instance = eventStore.instances[this.instance.instanceId]
     }
   }
 
@@ -64,4 +68,18 @@ export default class EventApi {
     }
   }
 
+  get title(): string {
+    return this.def.title
+  }
+
+  get start(): Date {
+    return this.calendar.dateEnv.toDate(this.instance.range.start)
+  }
+
+  get end(): Date {
+    return this.def.hasEnd ?
+      this.calendar.dateEnv.toDate(this.instance.range.end) :
+      null
+  }
+
 }

+ 9 - 19
src/reducers/eventStore.ts

@@ -1,5 +1,5 @@
 import Calendar from '../Calendar'
-import { filterHash, arrayToHash } from '../util/object'
+import { filterHash } from '../util/object'
 import { EventMutation, applyMutationToEventStore } from '../structs/event-mutation'
 import { EventDef, EventInstance, EventInput, EventInstanceHash } from '../structs/event'
 import { EventStore, parseEventStore, mergeEventStores, getRelatedEvents } from '../structs/event-store'
@@ -32,10 +32,6 @@ export default function(eventStore: EventStore, action: Action, sourceHash: Even
     case 'REMOVE_EVENT_SOURCE':
       return excludeSource(eventStore, action.sourceId)
 
-    case 'FETCH_EVENT_SOURCES':
-      // when refetching happens for a source, clear the temporary events in it
-      return excludeTemporary(eventStore, action.sourceIds)
-
     default:
       return eventStore
   }
@@ -60,7 +56,11 @@ function receiveEvents(
       eventSource.sourceId,
       fetchRange,
       calendar,
-      excludeSource(eventStore, eventSource.sourceId) // dest
+      excludeSource( // dest
+        eventStore,
+        eventSource.sourceId,
+        true // also remove events with isTemporary:true
+      )
     )
   }
 
@@ -76,9 +76,10 @@ function excludeInstances(eventStore: EventStore, removals: EventInstanceHash):
   }
 }
 
-function excludeSource(eventStore: EventStore, sourceId: string): EventStore {
+// has extra bonus feature of removing temporary events
+function excludeSource(eventStore: EventStore, sourceId: string, temporaryMatch?: boolean): EventStore {
   let defs = filterHash(eventStore.defs, function(def: EventDef) {
-    return def.sourceId !== sourceId
+    return def.sourceId !== sourceId || def.isTemporary === temporaryMatch
   })
   let instances = filterHash(eventStore.instances, function(instance: EventInstance) {
     return defs[instance.defId] // still exists?
@@ -91,14 +92,3 @@ function applyMutationToRelated(eventStore: EventStore, instanceId: string, muta
   related = applyMutationToEventStore(related, mutation, calendar)
   return mergeEventStores(eventStore, related)
 }
-
-function excludeTemporary(eventStore: EventStore, sourceIds: string[]): EventStore {
-  let sourceIdHash = arrayToHash(sourceIds)
-  let defs = filterHash(eventStore.defs, function(def: EventDef) {
-    return !(sourceIdHash[def.sourceId] && def.isTemporary)
-  })
-  let instances = filterHash(eventStore.instances, function(instance: EventInstance) {
-    return defs[instance.defId] // still exists?
-  })
-  return { defs, instances }
-}