examples/http: update for ns
This commit is contained in:
@@ -5,7 +5,7 @@ const mainView = require('./views/main')
|
||||
const app = choo()
|
||||
|
||||
app.model(require('./models/error'))
|
||||
app.model('api', require('./models/api'))
|
||||
app.model(require('./models/api'))
|
||||
|
||||
app.router((route) => [
|
||||
route('/', mainView)
|
||||
|
||||
+13
-26
@@ -1,45 +1,32 @@
|
||||
const http = require('../../../http')
|
||||
|
||||
module.exports = {
|
||||
namespace: 'api',
|
||||
state: {
|
||||
title: 'Button pushing machine 3000'
|
||||
},
|
||||
reducers: {
|
||||
set: function (action, state) {
|
||||
return { 'api:title': action.payload }
|
||||
}
|
||||
set: (action, state) => ({ 'title': action.payload })
|
||||
},
|
||||
effects: {
|
||||
good: performGoodRequest,
|
||||
bad: performBadRequest
|
||||
good: (action, state, send) => request('/good', send),
|
||||
bad: (action, state, send) => request('/bad', send)
|
||||
}
|
||||
}
|
||||
|
||||
function performGoodRequest (action, state, send) {
|
||||
http('/good', { json: true }, function (err, res, body) {
|
||||
if (err) return send('error', { payload: 'HTTP error' })
|
||||
if (res.statusCode !== 200) {
|
||||
return send('error', { payload: body.payload })
|
||||
}
|
||||
if (!body) {
|
||||
return send('error', { payload: 'fatal: no body received' })
|
||||
}
|
||||
send('api:set', { payload: body.message })
|
||||
})
|
||||
}
|
||||
|
||||
function performBadRequest (action, state, send) {
|
||||
http('/bad', { json: true }, function (err, res, body) {
|
||||
if (err) return send('error', { payload: 'HTTP error' })
|
||||
function request (uri, send) {
|
||||
http(uri, { json: true }, function (err, res, body) {
|
||||
if (err) return send('app:error', { payload: 'HTTP error' })
|
||||
if (res.statusCode !== 200) {
|
||||
const message = (body && body.message)
|
||||
? body.message
|
||||
: 'unknown server error'
|
||||
return send('error', { payload: message })
|
||||
? body.message
|
||||
: 'unknown server error'
|
||||
return send('app:error', { payload: message })
|
||||
}
|
||||
if (!body) {
|
||||
return send('error', { payload: 'fatal: no body received' })
|
||||
console.log('req made!')
|
||||
return send('app:error', { payload: 'fatal: no body received' })
|
||||
}
|
||||
send('api:set', { payload: body.title })
|
||||
send('api:set', { payload: body.message || body.title })
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
const ERROR_TIMEOUT = 1000
|
||||
|
||||
module.exports = {
|
||||
namespace: 'app',
|
||||
state: {
|
||||
error: [],
|
||||
errorTimeDone: null,
|
||||
@@ -33,9 +34,9 @@ module.exports = {
|
||||
},
|
||||
effects: {
|
||||
error: function (action, state, send) {
|
||||
const timeout = state.errorTimeDone - Date.now()
|
||||
const timeout = state.app.errorTimeDone - Date.now()
|
||||
setTimeout(function () {
|
||||
send('error:delete')
|
||||
send('app:error:delete')
|
||||
}, timeout)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
const choo = require('../../../')
|
||||
|
||||
module.exports = function (params, state, send) {
|
||||
const error = state.app.error[0]
|
||||
const title = state.api.title
|
||||
return choo.view`
|
||||
<section>
|
||||
<h1>${state['api:title']}</h1>
|
||||
<h2>Latest error: ${state.error[0]}</h2>
|
||||
<h1>${title}</h1>
|
||||
<h2>Latest error: ${error}</h2>
|
||||
<button onclick=${(e) => send('api:good')}>OK!</button>
|
||||
<button onclick=${(e) => send('api:bad')}>Naughty</button>
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user