Module: LucidComponent::Reducers

Defined in:
lib/lucid_component/reducers.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.component_reducers_addedObject (readonly)

Returns the value of attribute component_reducers_added.



4
5
6
# File 'lib/lucid_component/reducers.rb', line 4

def component_reducers_added
  @component_reducers_added
end

Class Method Details

.add_component_reducers_to_storeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lucid_component/reducers.rb', line 6

def add_component_reducers_to_store
  unless component_reducers_added
    @_component_reducers_added = true
    component_reducer = Redux.create_reducer do |prev_state, action|
      case action[:type]
      when 'COMPONENT_STATE'
        if action.key?(:set_state)
          action[:set_state]
        else
          new_state = {}.merge!(prev_state) # make a copy of state
          new_state[action[:object_id]] = {} unless new_state.key?(action[:object_id])
          new_state[action[:object_id]].merge!(action[:name] => action[:value])
          # new_state == prev_state ? prev_state : new_state
          new_state
        end
      else
        prev_state
      end
    end

    component_class_reducer = Redux.create_reducer do |prev_state, action|
      case action[:type]
      when 'COMPONENT_CLASS_STATE'
        if action.key?(:set_state)
          action[:set_state]
        else
          new_state = {}.merge!(prev_state) # make a copy of state
          new_state[action[:class]] = {} unless new_state.key?(action[:class])
          new_state[action[:class]].merge!(action[:name] => action[:value])
          # new_state == prev_state ? prev_state : new_state
          new_state
        end
      else
        prev_state
      end
    end
    Redux::Store.preloaded_state_merge!(component_state: {}, component_class_state: {})
    Redux::Store.add_reducers(component_state: component_reducer, component_class_state: component_class_reducer)
  end
end