김장현
8 months ago
4 changed files with 2 additions and 76 deletions
@ -1,26 +0,0 @@
|
||||
import axios from 'axios' |
||||
|
||||
// ** Get Bookmarks Array from @fakeDB
|
||||
export const getBookmarks = () => { |
||||
return dispatch => { |
||||
return axios.get('/api/bookmarks/data').then(response => { |
||||
dispatch({ |
||||
type: 'GET_BOOKMARKS', |
||||
data: response.data.suggestions, |
||||
bookmarks: response.data.bookmarks |
||||
}) |
||||
}) |
||||
} |
||||
} |
||||
|
||||
// ** Update & Get Updated Bookmarks Array
|
||||
export const updateBookmarked = id => { |
||||
return dispatch => { |
||||
return axios.post('/api/bookmarks/update', { id }).then(() => { |
||||
dispatch({ type: 'UPDATE_BOOKMARKED', id }) |
||||
}) |
||||
} |
||||
} |
||||
|
||||
// ** Handle Bookmarks & Main Search Queries
|
||||
export const handleSearchQuery = val => dispatch => dispatch({ type: 'HANDLE_SEARCH_QUERY', val }) |
@ -1,45 +0,0 @@
|
||||
// ** Initial State
|
||||
const initialState = { |
||||
suggestions: [], |
||||
bookmarks: [], |
||||
query: '', |
||||
headerMenu: 'basis' |
||||
}; |
||||
|
||||
const navbarReducer = (state = initialState, action) => { |
||||
switch (action.type) { |
||||
case 'HANDLE_SEARCH_QUERY': |
||||
return { ...state, query: action.val }; |
||||
case 'GET_BOOKMARKS': |
||||
return { |
||||
...state, |
||||
suggestions: action.data, |
||||
bookmarks: action.bookmarks |
||||
}; |
||||
case 'UPDATE_BOOKMARKED': |
||||
let objectToUpdate; |
||||
|
||||
// ** find & update object
|
||||
state.suggestions.find(item => { |
||||
if (item.id === action.id) { |
||||
item.isBookmarked = !item.isBookmarked; |
||||
objectToUpdate = item; |
||||
} |
||||
}); |
||||
case 'CLICK_HEADER_MENU': |
||||
// ** Get index to add or remove bookmark from array
|
||||
const bookmarkIndex = state.bookmarks.findIndex(x => x.id === action.id); |
||||
|
||||
if (bookmarkIndex === -1) { |
||||
state.bookmarks.push(objectToUpdate); |
||||
} else { |
||||
state.bookmarks.splice(bookmarkIndex, 1); |
||||
} |
||||
|
||||
return { ...state }; |
||||
default: |
||||
return state; |
||||
} |
||||
}; |
||||
|
||||
export default navbarReducer; |
Loading…
Reference in new issue