Merge branch '14277-search-view-editing-saved-queries'
[arvados-workbench2.git] / src / store / search-bar / search-bar-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { searchBarActions, SearchBarActions } from '~/store/search-bar/search-bar-actions';
6 import { GroupContentsResource } from '~/services/groups-service/groups-service';
7 import { SearchBarAdvanceFormData } from '~/models/search-bar';
8
9 interface SearchBar {
10     currentView: string;
11     open: boolean;
12     searchResults: GroupContentsResource[];
13     searchValue: string;
14     savedQueries: SearchBarAdvanceFormData[];
15 }
16
17 export enum SearchView {
18     BASIC = 'basic',
19     ADVANCED = 'advanced',
20     AUTOCOMPLETE = 'autocomplete'
21 }
22
23 const initialState: SearchBar = {
24     currentView: SearchView.BASIC,
25     open: false,
26     searchResults: [],
27     searchValue: '',
28     savedQueries: []
29 };
30
31 export const searchBarReducer = (state = initialState, action: SearchBarActions): SearchBar =>
32     searchBarActions.match(action, {
33         SET_CURRENT_VIEW: currentView => ({ ...state, currentView }),
34         OPEN_SEARCH_VIEW: () => ({ ...state, open: true }),
35         CLOSE_SEARCH_VIEW: () => ({ ...state, open: false }),
36         SET_SEARCH_RESULTS: (searchResults) => ({ ...state, searchResults }),
37         SET_SEARCH_VALUE: (searchValue) => ({ ...state, searchValue }),
38         SET_SAVED_QUERIES: savedQueries => ({ ...state, savedQueries }),
39         UPDATE_SAVED_QUERY: searchQuery => ({ ...state, savedQueries: searchQuery }),
40         default: () => state
41     });