-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates typescript definitions to play a _bit_ nicer with generic mod…
…el interfaces
- Loading branch information
Showing
2 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-disable */ | ||
|
||
import { | ||
createStore, | ||
computed, | ||
Computed, | ||
action, | ||
Action, | ||
thunk, | ||
Thunk, | ||
} from 'easy-peasy'; | ||
|
||
interface ObjectWithId { | ||
id: number; | ||
} | ||
|
||
interface Nested { | ||
save: Thunk<Nested>; | ||
} | ||
|
||
interface DataModel<DataItem extends ObjectWithId> { | ||
data: { [key: number]: DataItem }; | ||
sortBy: keyof DataItem | 'none'; | ||
ids: Computed<DataModel<DataItem>, string[]>; | ||
fetched: Action<DataModel<DataItem>, DataItem[]>; | ||
fetch: Thunk<DataModel<DataItem>, string>; | ||
getItemById: Computed< | ||
DataModel<DataItem>, | ||
(id: number) => DataItem | undefined | ||
>; | ||
nested: Nested; | ||
} | ||
|
||
const dataModel = <Item extends ObjectWithId>( | ||
endpoint: () => Promise<Item[]>, | ||
): DataModel<Item> => { | ||
const result: DataModel<Item> = { | ||
data: {}, | ||
ids: computed(state => Object.keys(state.data)), | ||
fetched: action((state, items) => { | ||
items.forEach((item, idx) => { | ||
state.data[idx] = item; | ||
}); | ||
84656; | ||
}), | ||
fetch: thunk(async (actions, payload) => { | ||
const data = await endpoint(); | ||
actions.fetched(data); | ||
// Nested actions do not work on generics :( | ||
// typings:expect-error | ||
actions.nested.save(); | ||
}), | ||
getItemById: computed(state => (id: number) => | ||
Object.values(state.data).find(item => item.id === id), | ||
), | ||
sortBy: 'id', | ||
nested: { | ||
save: thunk(() => {}), | ||
}, | ||
}; | ||
return result; | ||
}; | ||
|
||
interface Person extends ObjectWithId { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
const personModel = dataModel<Person>(() => | ||
Promise.resolve([{ id: 1, name: 'bob' }]), | ||
); | ||
|
||
const store = createStore(personModel); | ||
|
||
store.getState().sortBy; | ||
|
||
store.getActions().fetched([]); | ||
store.getActions().data; | ||
// typings:expect-error | ||
store.getActions().sortBy; | ||
store.getActions().nested.save(); |