|
1 |
| -import { reactive, computed, toRefs, watch } from '@vue/composition-api' |
| 1 | +/* |
| 2 | +eslint |
| 3 | +@typescript-eslint/no-explicit-any: 0 |
| 4 | +*/ |
2 | 5 | import {
|
3 |
| - getServicePrefix, |
4 |
| - getServiceCapitalization, |
5 |
| - getQueryInfo, |
6 |
| - getItemsFromQueryInfo |
7 |
| -} from './utils' |
| 6 | + reactive, |
| 7 | + computed, |
| 8 | + toRefs, |
| 9 | + isRef, |
| 10 | + watch, |
| 11 | + Ref |
| 12 | +} from '@vue/composition-api' |
| 13 | +import { Params } from './utils' |
8 | 14 |
|
9 |
| -const defaults = { |
10 |
| - model: null, |
11 |
| - params: null, |
12 |
| - queryWhen: () => true, |
13 |
| - qid: 'default' |
| 15 | +interface UseGetOptions { |
| 16 | + model: Function |
| 17 | + id: null | string | number | Ref<null> | Ref<string> | Ref<number> |
| 18 | + params?: Params | Ref<Params> |
| 19 | + fetchParams?: Params | Ref<Params> |
| 20 | + queryWhen?: Ref<Function> |
| 21 | + local?: boolean |
| 22 | + lazy?: boolean |
| 23 | +} |
| 24 | +interface UseGetState { |
| 25 | + item: Ref<any> |
| 26 | + isPending: boolean |
| 27 | + hasBeenRequested: boolean |
| 28 | + hasLoaded: boolean |
| 29 | + error: null | Error |
| 30 | + isLocal: boolean |
| 31 | +} |
| 32 | +interface UseGetData { |
| 33 | + item: Ref<any> |
| 34 | + servicePath: Ref<string> |
| 35 | + isPending: Ref<boolean> |
| 36 | + hasBeenRequested: Ref<boolean> |
| 37 | + hasLoaded: Ref<boolean> |
| 38 | + isLocal: Ref<boolean> |
| 39 | + error: Ref<Error> |
| 40 | + get: Function |
14 | 41 | }
|
15 | 42 |
|
16 |
| -export default function find(options) { |
17 |
| - const { model, params, name, queryWhen, qid } = Object.assign( |
| 43 | +export default function get(options: UseGetOptions): UseGetData { |
| 44 | + const defaults = { |
| 45 | + model: null, |
| 46 | + id: null, |
| 47 | + params: null, |
| 48 | + queryWhen: computed((): boolean => true), |
| 49 | + local: false, |
| 50 | + lazy: false |
| 51 | + } |
| 52 | + const { model, id, params, queryWhen, local, lazy } = Object.assign( |
18 | 53 | {},
|
19 | 54 | defaults,
|
20 | 55 | options
|
21 | 56 | )
|
22 | 57 |
|
23 |
| - const nameToUse = (name || model.servicePath).replace('-', '_') |
24 |
| - const prefix = getServicePrefix(nameToUse) |
25 |
| - const capitalized = getServiceCapitalization(nameToUse) |
| 58 | + const _id = computed(() => id) |
| 59 | + const servicePath = computed<string>(() => model.servicePath) |
| 60 | + const state = reactive<UseGetState>({ |
| 61 | + item: computed(() => { |
| 62 | + const getterId = isRef(id) ? id.value : id |
| 63 | + const getterParams = isRef(params) |
| 64 | + ? Object.assign({}, params.value) |
| 65 | + : params == null |
| 66 | + ? params |
| 67 | + : { ...params } |
| 68 | + if (getterParams != null) { |
| 69 | + return model.getFromStore(getterId, getterParams) || null |
| 70 | + } else { |
| 71 | + return model.getFromStore(getterId) || null |
| 72 | + } |
| 73 | + // return model.store.state[model.servicePath].keyedById[id.value] || null |
| 74 | + // return model.getFromStore(id.value) || null |
| 75 | + }), |
| 76 | + isPending: false, |
| 77 | + hasBeenRequested: false, |
| 78 | + hasLoaded: false, |
| 79 | + error: null, |
| 80 | + isLocal: local |
| 81 | + }) |
26 | 82 |
|
27 |
| - const IS_FIND_PENDING = `isFind${capitalized}Pending` |
28 |
| - const GET_ACTION = `find${capitalized}` |
29 |
| - const GET_GETTER = `find${capitalized}InStore` |
30 |
| - const HAVE_ITEMS_BEEN_REQUESTED_ONCE = `have${capitalized}BeenRequestedOnce` |
31 |
| - const HAVE_ITEMS_LOADED_ONCE = `have${capitalized}LoadedOnce` |
32 |
| - const PAGINATION = `${prefix}PaginationData` |
33 |
| - // const MOST_RECENT_QUERY = `${prefix}LatestQuery` |
34 |
| - const ERROR = `${prefix}Error` |
| 83 | + function get<T>(id, params): T { |
| 84 | + const idToUse = isRef(id) ? id.value : id |
| 85 | + const paramsToUse = isRef(params) ? params.value : params |
35 | 86 |
|
36 |
| - const state = reactive({ |
37 |
| - [prefix]: computed(() => model.getFromStore(params).data), |
38 |
| - [IS_FIND_PENDING]: false, |
39 |
| - [HAVE_ITEMS_BEEN_REQUESTED_ONCE]: false, |
40 |
| - [HAVE_ITEMS_LOADED_ONCE]: false, |
41 |
| - [ERROR]: null, |
42 |
| - [PAGINATION]: computed(() => { |
43 |
| - return model.store.state[model.servicePath].pagination |
44 |
| - }) |
45 |
| - }) |
46 |
| - function get(params) { |
47 |
| - if (params != null) { |
48 |
| - console.log('finding') |
49 |
| - model.get(params) |
| 87 | + if (idToUse != null && !state.isLocal) { |
| 88 | + state.isPending = true |
| 89 | + state.hasBeenRequested = true |
| 90 | + |
| 91 | + const promise = |
| 92 | + paramsToUse != null |
| 93 | + ? model.get(idToUse, paramsToUse) |
| 94 | + : model.get(idToUse) |
| 95 | + |
| 96 | + return promise |
| 97 | + .then(response => { |
| 98 | + state.isPending = false |
| 99 | + state.hasLoaded = true |
| 100 | + return response |
| 101 | + }) |
| 102 | + .catch(error => { |
| 103 | + state.isPending = false |
| 104 | + state.error = error |
| 105 | + return error |
| 106 | + }) |
50 | 107 | }
|
51 | 108 | }
|
52 | 109 |
|
53 | 110 | watch(
|
54 |
| - () => params, |
55 |
| - params => { |
56 |
| - get(params) |
57 |
| - } |
| 111 | + () => [_id, params], |
| 112 | + ([_id, params]) => { |
| 113 | + get(_id, params) |
| 114 | + }, |
| 115 | + { lazy } |
58 | 116 | )
|
59 | 117 |
|
60 |
| - get(params) |
61 |
| - |
62 | 118 | return {
|
| 119 | + servicePath, |
63 | 120 | ...toRefs(state),
|
64 |
| - [GET_ACTION]: model.get, |
65 |
| - [GET_GETTER]: model.getFromStore |
| 121 | + get |
66 | 122 | }
|
67 | 123 | }
|
0 commit comments