{"size": 2130, "ext": "js", "max_stars_repo_path": "src/store.js", "max_stars_repo_name": "shreya0721/Space-X-Program", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/store.js", "max_issues_repo_name": "shreya0721/Space-X-Program", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/store.js", "max_forks_repo_name": "shreya0721/Space-X-Program", "max_forks_repo_forks_event_min_datetime": "2020-09-30T16:20:03.000Z", "max_forks_repo_forks_event_max_datetime": "2020-09-30T16:20:03.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 24.4827586207, "max_line_length": 124, "alphanum_fraction": 0.6272300469}
import { createStore, combineReducers, applyMiddleware } from "redux";
import thunkMiddleware from "redux-thunk";
import { fetchInitialData } from "./api";

export const initializeSession = () => ({
    type: "INITIALIZE_SESSION",
});

const storeData = (data) => ({
    type: "STORE_DATA",
    data,
});

const updateFilterValue = (k, val) => ({
    type: "SET_YEAR",
    payload: {
        key: k,
        value: val
    },
});

export const fetchData = (year, launchSuccess, landSuccess) => (dispatch, getState) => {

    let selectedYear = year
    let selectedLaunchSuccess = launchSuccess
    let selectedLandSuccess = landSuccess

    if (getState().filter.year) {
        selectedYear = getState().filter.year
    }
    if (getState().filter.launchSuccess) {
        selectedLaunchSuccess = getState().filter.launchSuccess
    }
    if (getState().filter.landSuccess) {
        selectedLandSuccess = getState().filter.landSuccess
    }
    return fetchInitialData(selectedYear, selectedLaunchSuccess, selectedLandSuccess).then(res => dispatch(storeData(res)));
}



export const filterYearData = (key, value) => (dispatch) =>
    dispatch(updateFilterValue(key, value));


const sessionReducer = (state = false, action) => {
    switch (action.type) {
        case "INITIALIZE_SESSION":
            return true;
        default: return state;
    }
};
let initialState = {
    year: null,
    launchSuccess: null,
    landSuccess: null
}
const filterReducer = (state = initialState, action) => {

    switch (action.type) {
        case "SET_YEAR":
            return {
                ...state,
                [action.payload.key]: action.payload.value
            };
            
        default: return state;
    }
};

const dataReducer = (state = [], action) => {
    switch (action.type) {
        case "STORE_DATA":
            return action.data;
        default: return state;
    }
};

const reducer = combineReducers({
    loggedIn: sessionReducer,
    data: dataReducer,
    filter: filterReducer
});

export default (initialState) =>
    createStore(reducer, initialState, applyMiddleware(thunkMiddleware));
