{"size": 2289, "ext": "js", "max_stars_repo_path": "src/state/movies/index.js", "max_stars_repo_name": "What-to-watch/wtw-front", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/state/movies/index.js", "max_issues_repo_name": "What-to-watch/wtw-front", "max_issues_repo_issues_event_min_datetime": "2020-05-07T23:58:05.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T04:25:35.000Z", "max_forks_repo_path": "src/state/movies/index.js", "max_forks_repo_name": "What-to-watch/wtw-front", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 12, "max_forks_count": null, "avg_line_length": 44.8823529412, "max_line_length": 104, "alphanum_fraction": 0.749235474}
import { assocPath, propOr } from 'ramda'
import { createReducer, unaryActionCreator, nullaryActionCreator } from "redux-utility";
import { composeReducers as composeQueryOpts } from '../../utils/composeReducers';

export const SET_TITLE_SEARCH = 'SET_TITLE_SEARCH';
export const SET_GENRES_FILTER = 'SET_GENRES_FILTER';
export const SET_SORT_FIELD = 'SET_SORT_FIELD';
export const SET_SORT_ORDER = 'SET_SORT_ORDER';
export const SET_RESULTS_PER_PAGE ='SET_RESULTS_PER_PAGE';
export const SET_AFTER_CURSOR = 'SET_AFTER_CURSOR';
export const SET_BEFORE_CURSOR = 'SET_BEFORE_CURSOR';
export const RESET_CURSORS = 'RESET_CURSORS';

const setPath = (p,value,obj) => assocPath(p.split("."),value,obj)
const payloadOr = (or,action) => propOr(or,"payload",action)
const setQueryOption = (opt,or) => (state,action) => setPath(`query.${opt}`,payloadOr(or,action),state);

const moviesReducer = createReducer({
    [SET_TITLE_SEARCH]: setQueryOption("title",""),
    [SET_GENRES_FILTER]: setQueryOption("genres",[]),
    [SET_SORT_FIELD]: setQueryOption("sortField",'Title'),
    [SET_SORT_ORDER]: setQueryOption("sortOrder",'ASC'),
    [SET_RESULTS_PER_PAGE]: composeQueryOpts(
        setQueryOption("last",100),
        setQueryOption("first",100),
        setQueryOption("resultsPerPage",100),
    ),
    [SET_AFTER_CURSOR]: composeQueryOpts(
        setQueryOption("after",null),
        (state) => setQueryOption("before",null)(state,{})
    ),
    [SET_BEFORE_CURSOR]: composeQueryOpts(
        setQueryOption("before",null),
        (state) => setQueryOption("after",null)(state,{})
    ), 
    [RESET_CURSORS]: composeQueryOpts(
        setQueryOption("after" ,null),
        setQueryOption("before",null)
    )
})

export default moviesReducer;

export const setTitleSearch = unaryActionCreator(SET_TITLE_SEARCH);
export const setGenresFilter = unaryActionCreator(SET_GENRES_FILTER);
export const setSortField = unaryActionCreator(SET_SORT_FIELD);
export const setSortOrder = unaryActionCreator(SET_SORT_ORDER);
export const setResultsPerPage = unaryActionCreator(SET_RESULTS_PER_PAGE);
export const setAfterCursor = unaryActionCreator(SET_AFTER_CURSOR);
export const setBeforeCursor = unaryActionCreator(SET_BEFORE_CURSOR);
export const resetCursors = nullaryActionCreator(RESET_CURSORS);