-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix a couple of bugs in models and data classes
- Loading branch information
Showing
11 changed files
with
249 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,92 @@ | ||
import produce from 'immer'; | ||
import {createSelector} from 'reselect'; | ||
import {AnyAction, Reducer} from 'redux'; | ||
import {toPairs,} from 'lodash'; | ||
|
||
interface ActionTypes { | ||
started: string; | ||
failed: string; | ||
succeeded: string; | ||
} | ||
|
||
interface Actions { | ||
start: Function; | ||
fail: Function; | ||
succeed: Function; | ||
} | ||
|
||
export class AsyncResolver { | ||
public readonly namespace: string; | ||
|
||
public constructor() { | ||
this.namespace = 'async'; | ||
|
||
this.actionTypes = this.actionTypes.bind(this); | ||
this.selectors = this.selectors.bind(this); | ||
this.reducers = this.reducers.bind(this); | ||
} | ||
|
||
public actionTypes(): ActionTypes { | ||
return { | ||
started: `${this.namespace}.started`, | ||
failed: `${this.namespace}.failed`, | ||
succeeded: `${this.namespace}.succeeded`, | ||
}; | ||
} | ||
|
||
public actions(): Actions { | ||
const actionTypes = this.actionTypes(); | ||
|
||
return { | ||
start: (id: string, metadata={}) => { | ||
return { type: actionTypes.started, id, metadata }; | ||
}, | ||
fail: (id: string, error, metadata={}) => { | ||
return { type: actionTypes.failed, id, payload: error, metadata }; | ||
}, | ||
succeed: (id: string, metadata={}) => { | ||
return { type: actionTypes.succeeded, id, metadata }; | ||
} | ||
}; | ||
} | ||
|
||
public selectors(id: string): SelectorFunction { | ||
const selectorFunc = state => state[id] || {isLoading: true, error: null}; | ||
|
||
return createSelector([selectorFunc], data => data); | ||
} | ||
|
||
public reducers(): Reducer<unknown, AnyAction> { | ||
const actionTypes = this.actionTypes(); | ||
|
||
return produce((draft: object, { | ||
type, id, payload, metadata, | ||
}) => { | ||
switch (type) { | ||
case actionTypes.started: | ||
draft[id] = {isLoading: true, error: null, metadata}; | ||
return; | ||
|
||
case actionTypes.failed: | ||
draft[id].isLoading = false; | ||
draft[id].error = payload; | ||
|
||
for (const [key, value] of toPairs(metadata)) { | ||
draft[id].metadata[key] = value; | ||
} | ||
|
||
return; | ||
|
||
case actionTypes.succeeded: | ||
draft[id].isLoading = false; | ||
draft[id].error = null; | ||
|
||
for (const [key, value] of toPairs(metadata)) { | ||
draft[id].metadata[key] = value; | ||
} | ||
|
||
return; | ||
} | ||
}, {}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { useModel } from './useModel'; | ||
export { useService } from './useService'; |
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,90 @@ | ||
import * as React from 'react'; | ||
import * as urlComposer from 'url-composer'; | ||
import {Model} from '../model'; | ||
import {AsyncResolver} from '../asyncResolver'; | ||
import {Method} from "axios"; | ||
import axios from 'axios'; | ||
import {useDispatch, useSelector, batch} from "react-redux"; | ||
|
||
interface ServiceSpec { | ||
model: Model; | ||
host: string; | ||
urls: Record<string, Record<ScopeId, string>>; | ||
} | ||
|
||
interface AxiosConfig { | ||
data?: object | object[]; | ||
headers?: object; | ||
pathParams?: object; | ||
queryParams?: object; | ||
} | ||
|
||
export function useService(serviceSpec: ServiceSpec): Record<string, (ScopeId) => any> { | ||
const dispatch = useDispatch(); | ||
const asyncResolver = new AsyncResolver(); | ||
const state = useSelector( | ||
(state: Record<string, any>) => state[asyncResolver.namespace] | ||
) as object; | ||
|
||
function buildAxiousCall(scope: string, scopeId: ScopeId, method: Method) { | ||
return (config: AxiosConfig): SelectorFunction => { | ||
const url = urlComposer.build({ | ||
host: serviceSpec.host, | ||
path: serviceSpec.urls[scope], | ||
params: config.pathParams, | ||
query: config.queryParams, | ||
}); | ||
|
||
React.useEffect(() => { | ||
dispatch(asyncResolver.actions().start(url)); | ||
|
||
axios({ | ||
...config, | ||
method, | ||
url, | ||
}).then( response => { | ||
const data = response.data; | ||
const responseMetadata = { | ||
status: response.status, | ||
headers: response.headers, | ||
}; | ||
|
||
batch(() => { | ||
if (['get', 'post', 'put'].includes(method)) { | ||
dispatch(serviceSpec.model.actions().set(scope, scopeId, data)); | ||
} else if (['delete'].includes(method)) { | ||
dispatch(serviceSpec.model.actions().remove(scope, scopeId)); | ||
} | ||
dispatch(asyncResolver.actions().succeed(url, responseMetadata)); | ||
}); | ||
}).catch(error => { | ||
const responseMetadata = error.response ? { | ||
status: error.response.status, | ||
headers: error.response.headers, | ||
} : {}; | ||
dispatch(asyncResolver.actions().fail(url, error.message, responseMetadata)); | ||
}); | ||
}, [url]); | ||
|
||
return asyncResolver.selectors(url)(state); | ||
}; | ||
}; | ||
|
||
return React.useMemo(() => { | ||
const data = {}; | ||
|
||
[serviceSpec.model.defaultScope, ...serviceSpec.model.scopes].forEach((scope: string) => { | ||
data[scope] = (scopeId: ScopeId) => ({ | ||
get: buildAxiousCall(scope, scopeId, 'get'), | ||
post: buildAxiousCall(scope, scopeId, 'post'), | ||
put: buildAxiousCall(scope, scopeId, 'put'), | ||
patch: buildAxiousCall(scope, scopeId, 'patch'), | ||
delete: buildAxiousCall(scope, scopeId, 'delete'), | ||
options: buildAxiousCall(scope, scopeId, 'options'), | ||
head: buildAxiousCall(scope, scopeId, 'head'), | ||
}); | ||
}); | ||
|
||
return data; | ||
}, [serviceSpec, state]); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { Model } from './model'; | ||
export { Data } from './data'; | ||
export { useModel } from './hooks'; | ||
export { AsyncResolver } from './asyncResolver'; | ||
export { useModel } from './hooks/useModel'; | ||
export { useService } from './hooks/useService'; | ||
export { combineModelReducers } from './redux'; |
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 |
---|---|---|
@@ -1,6 +1,15 @@ | ||
import { Reducer, AnyAction, compose } from 'redux'; | ||
import { Reducer, AnyAction } from 'redux'; | ||
import { Model } from './model'; | ||
|
||
export function combineModelReducers(models: Model[]): Reducer<unknown, AnyAction> { | ||
return compose(...models.map(model => model.reducers())); | ||
const reducers = models.map(model => model.reducers()); | ||
return (state, action) => { | ||
let reducedState = state; | ||
|
||
for (const reducer of reducers) { | ||
reducedState = reducer(reducedState, action); | ||
} | ||
|
||
return reducedState; | ||
} | ||
} |