Skip to content
Snippets Groups Projects
Store.js 874 B
Newer Older
import React from 'react'

export const Store = React.createContext()

const initialState = {
  searchOpen: false,
  search: {
    query: '*',
    class: 'LearningObject'
  },
  windowSize: {
    width: 0,
    height: 0 
  }
}

function reducer(state, action) {
  switch (action.type){
    case 'SAVE_SEARCH':
      return {
        ...state,
        search: action.newSearch
      }
    case 'HANDLE_SEARCH_BAR':
      return {
        ...state,
        searchOpen: action.opened
      }
    case 'WINDOW_SIZE':
      return {
        ...state,
        windowSize: action.innerWindow
      }
    default: 
      return state
  }
}

export function StoreProvider(props) {
  const [state, dispatch] = React.useReducer(reducer, initialState);
  const value = { state, dispatch };
  return (
    <Store.Provider value={value}>
      {props.children}
    </Store.Provider>
  )
}