|
| 1 | +/** |
| 2 | + * @module "actions.member-search" |
| 3 | + * @desc Actions for management of members search. |
| 4 | + */ |
| 5 | +import _ from 'lodash'; |
| 6 | +import { createActions } from 'redux-actions'; |
| 7 | +import { getService } from '../services/member-search'; |
| 8 | + |
| 9 | +/** |
| 10 | + * @desc Creates an action that fetchs the members data for a search term, and |
| 11 | + * adds result to the store cumulatively. |
| 12 | + * @param {String} searchTerm the search term |
| 13 | + * @param {Number} offset the number of records to skip |
| 14 | + * @param {Number} limit the maximum number of the return results |
| 15 | + * @return {Action} |
| 16 | + */ |
| 17 | +function loadMemberSearch(searchTerm, offset = 0, limit = 10) { |
| 18 | + return getService().getUsernameMatches(searchTerm, offset, limit); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * @static |
| 23 | + * @desc Creates an action that fetchs the members data for a search tag, and |
| 24 | + * adds result to the store. |
| 25 | + * @param {Object} tag the tag |
| 26 | + * @return {Action} |
| 27 | + */ |
| 28 | +function loadMemberSearchForTag(tag) { |
| 29 | + return getService().getTopMembers(tag); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * @static |
| 34 | + * @desc Creates an action that check if the term is a tag name. If it is unable to check, |
| 35 | + * or invalid data returned then resets the members data and search terms data in the store |
| 36 | + * to intial values. |
| 37 | + * @param {String} searchTerm the search term |
| 38 | + * @return {Action} |
| 39 | + */ |
| 40 | +function checkIfSearchTermIsATag(searchTerm) { |
| 41 | + return getService().checkIfSearchTermIsATag(searchTerm); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @static |
| 46 | + * @desc Creates an action that saves the current search term. |
| 47 | + * @param {String} searchTerm the search term |
| 48 | + * @return {Action} |
| 49 | + */ |
| 50 | +function setSearchTerm(searchTerm) { |
| 51 | + return { |
| 52 | + previousSearchTerm: searchTerm, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @static |
| 58 | + * @desc Creates an action that saves the current search tag. |
| 59 | + * @param {Object} searchTag the search tag |
| 60 | + * @return {Action} |
| 61 | + */ |
| 62 | +function setSearchTag(searchTag) { |
| 63 | + return { |
| 64 | + searchTermTag: searchTag, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +export default createActions({ |
| 69 | + MEMBER_SEARCH: { |
| 70 | + USERNAME_SEARCH_SUCCESS: loadMemberSearch, |
| 71 | + CHECK_IF_SEARCH_TERM_IS_A_TAG: checkIfSearchTermIsATag, |
| 72 | + TOP_MEMBER_SEARCH_SUCCESS: loadMemberSearchForTag, |
| 73 | + CLEAR_MEMBER_SEARCH: _.noop, |
| 74 | + LOAD_MORE_USERNAMES: _.noop, |
| 75 | + MEMBER_SEARCH_SUCCESS: _.noop, |
| 76 | + SET_SEARCH_TERM: setSearchTerm, |
| 77 | + SET_SEARCH_TAG: setSearchTag, |
| 78 | + RESET_SEARCH_TERM: _.noop, |
| 79 | + }, |
| 80 | +}); |
0 commit comments