Guest User

Untitled

a guest
May 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. // @flow
  2. /* Channels */
  3. import {createSelector} from 'reselect'
  4. import t from '../../localization/index';
  5. import type {State} from "../../reducers/index";
  6. import type {MostWatchedChannel} from "../../reducers/channels";
  7. import type {
  8. AllChannelsDeviation,
  9. FilterValues,
  10. MostPopularChannelsDeviation,
  11. MostWatchedChannelsDeviation,
  12. PromotedChannelsDeviation,
  13. RecentlyWatchedChannelsDeviation,
  14. SearchedChannelsDeviation,
  15. TvChannelsDeviation,
  16. TvChannelsFiltersDeviation,
  17. WatchingTvChannelDeviation
  18. } from "./TypeDefenition";
  19. import type {
  20. CategoryFilter,
  21. Channel,
  22. CountryFilter,
  23. GenreFilter,
  24. LanguageFilter,
  25. TvChannelsFilter
  26. } from "../../entities";
  27. import {DATE, NUMBER, RATING} from "../../constants/filterTypes";
  28. import type {EntityFiltersState} from "../../reducers/filters";
  29.  
  30. const TOP_CHANNELS_LIMIT = 20;
  31. const LAST_ADDED_CHANNELS_LIMIT = 20;
  32.  
  33. function isFavoriteTvChannel(id: string, favoritesIds: Array<string>) {
  34. return favoritesIds.indexOf(id) >= 0;
  35. }
  36.  
  37. export const getSelectedFilter = (state: State) => state.channels.tv.selectedFilter;
  38. export const getChannelsMap = (state: State) => state.channels.tv.channelsMap;
  39. export const getAllChannelsIds = (state: State) => state.channels.tv.allChannelsIds;
  40. export const getFavoritesChannelsIds = (state: State) => state.channels.tv.favoritesIds;
  41. export const getRecentlyWatchedIds = (state: State) => state.channels.tv.recentlyWatchedIds;
  42.  
  43. export const getSearchedChannels = createSelector(
  44. [getSelectedFilter, getChannelsMap, getAllChannelsIds],
  45. (selectedFilter, channelsMap, allChannelsIds): SearchedChannelsDeviation => {
  46. const searchText = selectedFilter.searchText.toLowerCase();
  47. console.log('Searched channels selector: ' + searchText);
  48. return allChannelsIds
  49. .map(id => channelsMap[id])
  50. .filter(channel => channel.title.toLowerCase().includes(searchText));
  51. }
  52. );
  53.  
  54. export const getMostPopularChannels = createSelector(
  55. [getChannelsMap, getAllChannelsIds],
  56. (channelsMap, allChannelsIds): MostPopularChannelsDeviation => {
  57. console.log('Most popular channels selector');
  58. return allChannelsIds
  59. .map(id => channelsMap[id])
  60. .sort((a, b) => b.rating - a.rating)
  61. .slice(0, TOP_CHANNELS_LIMIT);
  62. }
  63. );
  64.  
  65. export const getLastAddedChannels = createSelector(
  66. [getChannelsMap, getAllChannelsIds],
  67. (channelsMap, allChannelsIds) => {
  68. console.log('Last added channels selector');
  69.  
  70. return allChannelsIds
  71. .map(id => channelsMap[id])
  72. .sort((a, b) => Date.parse(b.created) - Date.parse(a.created))
  73. .slice(0, LAST_ADDED_CHANNELS_LIMIT);
  74. }
  75. );
  76.  
  77. export const getRecentlyWatchedChannels = createSelector(
  78. [getChannelsMap, getRecentlyWatchedIds],
  79. (channelsMap, recentlyWatchedIds): RecentlyWatchedChannelsDeviation => {
  80. console.log('Recently watched channels selector');
  81.  
  82. return recentlyWatchedIds
  83. .map(id => channelsMap[id])
  84. .filter(channel => !!channel);
  85.  
  86. }
  87. );
  88.  
  89. const getMostWatchedChannelsRow = (state: State) => state.channels.tv.mostWatchedChannels;
  90.  
  91. export const getMostWatchedChannels = createSelector(
  92. [getChannelsMap, getMostWatchedChannelsRow],
  93. (channelsMap, mostWatchedChannelsRow): MostWatchedChannelsDeviation => {
  94. console.log('Most watched channels selector');
  95.  
  96. return mostWatchedChannelsRow
  97. .map((mostWatchedChannel: MostWatchedChannel) => channelsMap[mostWatchedChannel._id])
  98. .filter(channel => !!channel);
  99. }
  100. );
  101.  
  102. export const getPromotedTvChannels = createSelector(
  103. [getRecentlyWatchedChannels, getMostPopularChannels, getMostWatchedChannels],
  104. (recentlyWatchedChannels, mostPopularChannels, mostWatchedChannels): PromotedChannelsDeviation => {
  105. console.log('Promoted channels selector');
  106. let promotedChannelsSet = {};
  107.  
  108. return mostWatchedChannels
  109. .concat(recentlyWatchedChannels)
  110. .concat(mostPopularChannels)
  111. .filter(channel => {
  112. if (promotedChannelsSet[channel._id] === undefined) {
  113. promotedChannelsSet[channel._id] = true;
  114. return true
  115. } else {
  116. return false
  117. }
  118. })
  119. .slice(0, 20);
  120. }
  121. );
  122.  
  123. export const getAllChannels = createSelector(
  124. [getSelectedFilter, getChannelsMap, getAllChannelsIds],
  125. (selectedFilter: TvChannelsFilter, channelsMap, allChannelsIds): AllChannelsDeviation => {
  126. console.log('All channels selector');
  127.  
  128. let allChannels = allChannelsIds
  129. .map(id => channelsMap[id])
  130. .filter(channel => selectedFilter.category === null || channel.categories.includes(selectedFilter.category))
  131. .filter(channel => selectedFilter.country === null || channel.countries.includes(selectedFilter.country))
  132. .filter(channel => selectedFilter.genre === null || channel.genres.includes(selectedFilter.genre))
  133. .filter(channel => selectedFilter.language === null || channel.languages.includes(selectedFilter.language));
  134.  
  135. switch (selectedFilter.sortBy) {
  136. case RATING:
  137. allChannels.sort((a, b) => b.rating - a.rating);
  138. break;
  139. case NUMBER:
  140. allChannels.sort((a, b) => a.number - b.number);
  141. break;
  142. case DATE:
  143. allChannels.sort((a, b) => Date.parse(b.created) - Date.parse(a.created));
  144. break;
  145. }
  146. return allChannels;
  147. }
  148. );
  149.  
  150. const getChannelsFiltersRow = (state: State): EntityFiltersState => state.filters.channels;
  151.  
  152. export const getTvChannelsFilters = createSelector(
  153. [getChannelsFiltersRow, getAllChannels],
  154. (channelsFilters: EntityFiltersState, allChannels: AllChannelsDeviation): TvChannelsFiltersDeviation => {
  155. console.log('Tv channels filters selector');
  156. let channelsFiltersState: EntityFiltersState = channelsFilters;
  157.  
  158. let availableCategoriesIds: Set<string> = new Set();
  159. let availableGenresIds: Set<string> = new Set();
  160. let availableLanguagesIds: Set<string> = new Set();
  161. let availableCountriesIds: Set<string> = new Set();
  162.  
  163. allChannels.forEach((channel: Channel) => {
  164. channel.categories.forEach(categoryId => {
  165. availableCategoriesIds.add(categoryId);
  166. });
  167. channel.genres.forEach(genreId => {
  168. availableGenresIds.add(genreId);
  169. });
  170. channel.languages.forEach(languageId => {
  171. availableLanguagesIds.add(languageId);
  172. });
  173. channel.countries.forEach(countryId => {
  174. availableCountriesIds.add(countryId);
  175. });
  176. });
  177.  
  178. let availableCategories: Array<FilterValues> = channelsFiltersState.category.data
  179. .map((categoryFilter: CategoryFilter) => ({
  180. _id: categoryFilter._id,
  181. title: categoryFilter.title,
  182. active: availableCategoriesIds.has(categoryFilter._id)
  183. }));
  184. let availableGenres: Array<FilterValues> = channelsFiltersState.genre.data
  185. .map((genreFitler: GenreFilter) => ({
  186. _id: genreFitler._id,
  187. title: genreFitler.title,
  188. active: availableGenresIds.has(genreFitler._id)
  189. }));
  190. let availableLanguages: Array<FilterValues> = channelsFiltersState.language.data
  191. .map((languageFilter: LanguageFilter) => ({
  192. _id: languageFilter._id,
  193. title: languageFilter.name,
  194. active: availableLanguagesIds.has(languageFilter._id)
  195. }));
  196. let availableCountries: Array<FilterValues> = channelsFiltersState.country.data
  197. .map((countryFilter: CountryFilter) => ({
  198. _id: countryFilter._id,
  199. title: countryFilter.commonName,
  200. active: availableLanguagesIds.has(countryFilter._id)
  201. }));
  202.  
  203. return {
  204. category: availableCategories.length > 0 ? {title: t.category, values: availableCategories} : null,
  205. genre: availableGenres.length > 0 ? {title: t.genre, values: availableGenres} : null,
  206. language: availableLanguages.length > 0 ? {title: t.language, values: availableLanguages} : null,
  207. country: availableCountries.length > 0 ? {title: t.country, values: availableCountries} : null,
  208. sortBy: {
  209. title: t.sort,
  210. values: [
  211. {_id: RATING, title: t.by_rating, active: true},
  212. {_id: NUMBER, title: t.by_number, active: true},
  213. {_id: DATE, title: t.by_date, active: true}
  214. ]
  215. },
  216. };
  217. }
  218. );
  219.  
  220.  
  221. export const getFavoritesChannels = createSelector(
  222. [getChannelsMap, getFavoritesChannelsIds],
  223. (channelsMap, favoritesChannelsIds) => {
  224. console.log('Favorites channels selector');
  225.  
  226. return favoritesChannelsIds
  227. .map(id => channelsMap[id])
  228. .filter(channel => !!channel);
  229. }
  230. );
  231.  
  232. export const getTvChannels = createSelector(
  233. [
  234. getSelectedFilter,
  235. getChannelsMap,
  236. getAllChannels,
  237. getFavoritesChannels,
  238. getRecentlyWatchedChannels,
  239. getLastAddedChannels,
  240. getMostPopularChannels,
  241. getPromotedTvChannels,
  242. getSearchedChannels
  243. ], (selectedFilter,
  244. channelsMap,
  245. allChannels,
  246. favoritesChannels,
  247. recentlyWatched,
  248. mostPopularChannels,
  249. lastAddedChannels,
  250. promotedTvChannels,
  251. searchedChannels): TvChannelsDeviation => {
  252. return {
  253. allChannels,
  254. favoritesChannels,
  255. recentlyWatched,
  256. mostPopularChannels,
  257. lastAddedChannels,
  258. promotedTvChannels,
  259. searchedChannels
  260. }
  261. }
  262. );
  263.  
  264. const getWatchingChannel = (state: State) => state.channels.tv.watchingChannel;
  265. const getChannelsSources = (state: State) => state.channels.tv.channelsSources;
  266.  
  267. export const getWatchingTvChannel = createSelector(
  268. [getWatchingChannel, getChannelsMap, getFavoritesChannelsIds, getChannelsSources],
  269. (watchingChannel, channelsMap, favoritesChannelsIds, channelsSources): WatchingTvChannelDeviation => {
  270. console.log('Watching channel selector');
  271. const watchingChannelId = watchingChannel !== null ? watchingChannel._id : null;
  272.  
  273. const isFavorite = favoritesChannelsIds.indexOf(watchingChannelId) >= 0;
  274.  
  275. return {
  276. ...channelsMap[watchingChannelId],
  277. isFavorite,
  278. sources: channelsSources[watchingChannelId]
  279. }
  280. }
  281. );
Add Comment
Please, Sign In to add comment