Skip to content

Commit 0e752a8

Browse files
committed
Get useGet tests passing
1 parent 87407a1 commit 0e752a8

File tree

6 files changed

+285
-84
lines changed

6 files changed

+285
-84
lines changed

src/service-module/service-module.getters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default function makeServiceGetters() {
7777
const tempRecord = tempsById[id]
7878
? select(params, tempIdField)(tempsById[id])
7979
: undefined
80-
return record || tempRecord
80+
return record || tempRecord || null
8181
},
8282
getCopyById: state => id => {
8383
const { servicePath, keepCopiesInStore, serverAlias } = state

src/service-module/service-module.mutations.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export default function makeServiceMutations() {
2323
const Model = _get(models, `[${serverAlias}][${modelName}]`)
2424
const BaseModel = _get(models, `[${state.serverAlias}].BaseModel`)
2525

26+
const newKeyedById = { ...state.keyedById }
27+
const newTempsById = { ...state.tempsById }
28+
2629
for (let item of items) {
2730
const id = getId(item, idField)
2831
const isTemp = id === null || id === undefined
@@ -42,14 +45,16 @@ export default function makeServiceMutations() {
4245
tempId = assignTempId(state, item)
4346
}
4447
item.__isTemp = true
45-
Vue.set(state.tempsById, tempId, item)
48+
newTempsById[tempId] = item
4649
} else {
4750
// Only add the id if it's not already in the `ids` list.
4851
if (!state.ids.includes(id)) {
4952
state.ids.push(id)
5053
}
51-
Vue.set(state.keyedById, id, item)
54+
newKeyedById[id] = item
5255
}
56+
state.keyedById = newKeyedById
57+
state.tempsById = newTempsById
5358
}
5459
}
5560

src/useGet.ts

Lines changed: 102 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,123 @@
1-
import { reactive, computed, toRefs, watch } from '@vue/composition-api'
1+
/*
2+
eslint
3+
@typescript-eslint/no-explicit-any: 0
4+
*/
25
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'
814

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
1441
}
1542

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(
1853
{},
1954
defaults,
2055
options
2156
)
2257

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+
})
2682

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
3586

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+
})
50107
}
51108
}
52109

53110
watch(
54-
() => params,
55-
params => {
56-
get(params)
57-
}
111+
() => [_id, params],
112+
([_id, params]) => {
113+
get(_id, params)
114+
},
115+
{ lazy }
58116
)
59117

60-
get(params)
61-
62118
return {
119+
servicePath,
63120
...toRefs(state),
64-
[GET_ACTION]: model.get,
65-
[GET_GETTER]: model.getFromStore
121+
get
66122
}
67123
}

test/use/InstrumentComponent.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import useGet from '../../src/useGet'
2+
3+
export default {
4+
name: 'InstrumentComponent',
5+
template: '<div id="test"> {{ instrument }} </div>',
6+
props: {
7+
id: {
8+
type: String,
9+
default: ''
10+
}
11+
},
12+
setup(props, context) {
13+
const { Instrument } = context.root.$FeathersVuex
14+
15+
const instrumentData = useGet({ model: Instrument, id: props.id })
16+
17+
return {
18+
instrument: instrumentData.item
19+
}
20+
}
21+
}

test/use/InstrumentComponent.vue

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)