Refactor and fix the bad URL param substitution method
This commit is contained in:
parent
0a86ae761b
commit
b70ceccd02
|
@ -47,18 +47,7 @@ class App extends React.PureComponent {
|
||||||
// modelRequest is an opinionated wrapper for model specific HTTP requests,
|
// modelRequest is an opinionated wrapper for model specific HTTP requests,
|
||||||
// including setting model states.
|
// including setting model states.
|
||||||
modelRequest = async (model, route, method, params) => {
|
modelRequest = async (model, route, method, params) => {
|
||||||
let url = route
|
let url = replaceParams(route, params)
|
||||||
|
|
||||||
// Replace :params in the URL with params in the array.
|
|
||||||
let uriParams = route.match(/:([a-z0-9\-_]+)/ig)
|
|
||||||
if(uriParams && uriParams.length > 0) {
|
|
||||||
uriParams.forEach((p) => {
|
|
||||||
let pName = p.slice(1) // Lose the ":" prefix
|
|
||||||
if(params && params.hasOwnProperty(pName)) {
|
|
||||||
url = url.replace(p, params[pName])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({ reqStates: { ...this.state.reqStates, [model]: cs.StatePending } })
|
this.setState({ reqStates: { ...this.state.reqStates, [model]: cs.StatePending } })
|
||||||
try {
|
try {
|
||||||
|
@ -97,9 +86,7 @@ class App extends React.PureComponent {
|
||||||
|
|
||||||
// request is a wrapper for generic HTTP requests.
|
// request is a wrapper for generic HTTP requests.
|
||||||
request = async (url, method, params, headers) => {
|
request = async (url, method, params, headers) => {
|
||||||
if (params && params.hasOwnProperty("id") && typeof params["id"] === "number") {
|
url = replaceParams(url, params)
|
||||||
url += "/" + params["id"]
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({ reqStates: { ...this.state.reqStates, [url]: cs.StatePending } })
|
this.setState({ reqStates: { ...this.state.reqStates, [url]: cs.StatePending } })
|
||||||
try {
|
try {
|
||||||
|
@ -137,4 +124,20 @@ class App extends React.PureComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function replaceParams (route, params) {
|
||||||
|
console.log(route, params)
|
||||||
|
// Replace :params in the URL with params in the array.
|
||||||
|
let uriParams = route.match(/:([a-z0-9\-_]+)/ig)
|
||||||
|
if(uriParams && uriParams.length > 0) {
|
||||||
|
uriParams.forEach((p) => {
|
||||||
|
let pName = p.slice(1) // Lose the ":" prefix
|
||||||
|
if(params && params.hasOwnProperty(pName)) {
|
||||||
|
route = route.replace(p, params[pName])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return route
|
||||||
|
}
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
|
Loading…
Reference in New Issue