|
| 1 | +import { ActionTree, MutationTree, GetterTree } from "vuex" |
| 2 | +import Vue from "vue" |
| 3 | + |
| 4 | +import { StoreStateRoot, StoreStateFeed, Item } from "~/lib/models" |
| 5 | + |
| 6 | +import { apiBasePath } from "~/store" |
| 7 | +import { lazy } from "~/common/utils" |
| 8 | + |
| 9 | +import { validFeeds } from "~/common/api" |
| 10 | + |
| 11 | +export const namespaced = true |
| 12 | + |
| 13 | +export const name = "feed" |
| 14 | + |
| 15 | +export { StoreStateFeed as State } |
| 16 | + |
| 17 | +export const types = { |
| 18 | + SELECT: "SELECT", |
| 19 | + FEED: "SET_FEED", |
| 20 | + ITEM: "SET_ITEM", |
| 21 | + ITEMS: "SET_ITEMS" |
| 22 | +} |
| 23 | + |
| 24 | +export const state = (): StoreStateFeed => { |
| 25 | + const out: StoreStateFeed = { |
| 26 | + feeds: { |
| 27 | + // [page: number] : [ [id: number] ] |
| 28 | + }, |
| 29 | + items: { |
| 30 | + // [id: number]: Item |
| 31 | + }, |
| 32 | + selected: null |
| 33 | + } |
| 34 | + |
| 35 | + validFeeds.forEach((feed: string) => { |
| 36 | + // @ts-ignore |
| 37 | + out.feeds[feed] = {} |
| 38 | + }) |
| 39 | + |
| 40 | + return out |
| 41 | +} |
| 42 | + |
| 43 | +export const actions: ActionTree<StoreStateFeed, StoreStateRoot> = { |
| 44 | + FETCH_FEED(actionContext, { feed, page, prefetch = false }) { |
| 45 | + // Don't priorotize already fetched feeds |
| 46 | + if ( |
| 47 | + actionContext.state.feeds[feed][page] && |
| 48 | + actionContext.state.feeds[feed][page].length |
| 49 | + ) { |
| 50 | + prefetch = true |
| 51 | + } |
| 52 | + if (!prefetch) { |
| 53 | + if ((this as any).feedCancelSource) { |
| 54 | + ;(this as any).feedCancelSource.cancel( |
| 55 | + "prioritize feed: " + feed + " page: " + page |
| 56 | + ) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return lazy( |
| 61 | + items => { |
| 62 | + const ids = items.map(item => item.id) |
| 63 | + actionContext.commit(types.FEED, { feed, ids, page }) |
| 64 | + actionContext.commit(types.ITEMS, { items }) |
| 65 | + }, |
| 66 | + () => |
| 67 | + (this as any).$axios.$get(`${apiBasePath}/${feed}/${page}.json`, { |
| 68 | + cancelToken: |
| 69 | + (this as any).feedCancelSource && |
| 70 | + (this as any).feedCancelSource.token |
| 71 | + }), |
| 72 | + (actionContext.state.feeds[feed][page] || []).map( |
| 73 | + id => actionContext.state.items[id] |
| 74 | + ) |
| 75 | + ) |
| 76 | + }, |
| 77 | + |
| 78 | + FETCH_ITEM(actionContext, { id }) { |
| 79 | + return lazy( |
| 80 | + item => { |
| 81 | + actionContext.commit(types.SELECT, item.id) |
| 82 | + actionContext.commit(types.ITEM, { item }) |
| 83 | + }, |
| 84 | + () => (this as any).$axios.$get(`${apiBasePath}/item/${id}.json`), |
| 85 | + Object.assign( |
| 86 | + { id, loading: true, comments: [] }, |
| 87 | + actionContext.state.items[id] |
| 88 | + ) |
| 89 | + ) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export const getters: GetterTree<StoreStateFeed, StoreStateRoot> = {} |
| 94 | + |
| 95 | +export const mutations: MutationTree<StoreStateFeed> = { |
| 96 | + [types.FEED]: (mutationStateContext, { feed, ids, page }) => { |
| 97 | + Vue.set(mutationStateContext.feeds[feed], page, ids) |
| 98 | + }, |
| 99 | + [types.ITEM]: (mutationStateContext, { item }: { item: Item }) => { |
| 100 | + if (item) { |
| 101 | + Vue.set(mutationStateContext.items as Item[], item.id, item) |
| 102 | + } |
| 103 | + }, |
| 104 | + [types.ITEMS]: (mutationStateContext, { items }: { items: Item[] }) => { |
| 105 | + items.forEach(item => { |
| 106 | + if (item) { |
| 107 | + Vue.set(mutationStateContext.items as Item[], item.id, item) |
| 108 | + } |
| 109 | + }) |
| 110 | + }, |
| 111 | + [types.SELECT](mutationStateContext, id: number | null = null) { |
| 112 | + mutationStateContext.selected = id |
| 113 | + } |
| 114 | +} |
0 commit comments