Skip to content

Commit

Permalink
repurpose createCase effect for updating
Browse files Browse the repository at this point in the history
  • Loading branch information
timwis committed Sep 30, 2016
1 parent e433b05 commit fec71ac
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/components/action-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = (action, onSubmitAction) => {
function onSubmit (e) {
if (onSubmitAction) {
const formData = getFormData(e.target)
onSubmitAction(formData)
onSubmitAction(action.slug, formData)
}
e.preventDefault()
}
Expand Down
4 changes: 2 additions & 2 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ if (process.env.NODE_ENV !== 'production') {
app.model(require('./models/case'))

app.router((route) => [
route('/:workflow/:caseId', Layout(CaseView)),
route('/:workflow', Layout(CaseView))
route('/:workflowSlug/:caseId', Layout(CaseView)),
route('/:workflowSlug', Layout(CaseView))
])

const tree = app.start()
Expand Down
16 changes: 10 additions & 6 deletions client/models/case.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = {
},
effects: {
fetchCase: (data, state, send, done) => {
const { workflow, caseId, token } = data
let uri = `${endpoint}${workflow}/`
const { workflowSlug, caseId, token } = data
let uri = `${endpoint}${workflowSlug}/`
if (caseId) uri += `${caseId}/`
if (token) uri += `?token=${token}`

Expand All @@ -37,12 +37,16 @@ module.exports = {
send('receiveCase', body, done)
})
},
createCase: (data, state, send, done) => {
const { workflow, payload } = data
const uri = `${endpoint}${workflow}/`
updateCase: (data, state, send, done) => {
const { workflowSlug, actionSlug, payload, caseId, token } = data
let uri = `${endpoint}${workflowSlug}/`
if (caseId) uri += `${caseId}/`
if (actionSlug) uri += `${actionSlug}/`
if (token) uri += `?token=${token}`

http.post(uri, { json: payload }, (err, response, body) => {
if (err || response.statusCode !== 200) {
return done(new Error('Error creating case'))
return done(new Error('Error updating case'))
}
send('receiveCase', body, done)
})
Expand Down
8 changes: 4 additions & 4 deletions client/views/case.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ActionButtons = require('../components/action-buttons')
const ActionForm = require('../components/action-form')

module.exports = (state, prev, send) => {
const { workflow, caseId } = state.params
const { workflowSlug, caseId } = state.params
const token = qs(window.location.search).token // choo v4 makes this easier

// Determine what to show in action section
Expand Down Expand Up @@ -57,14 +57,14 @@ module.exports = (state, prev, send) => {
}

function onLoad () {
send('fetchCase', { workflow, caseId, token })
send('fetchCase', { workflowSlug, caseId, token })
}

function onClickAction (actionName) {
send('setCurrentAction', actionName)
}

function onSubmitAction (payload) {
send('createCase', { workflow, payload })
function onSubmitAction (actionSlug, payload) {
send('updateCase', { workflowSlug, actionSlug, payload, caseId, token })
}
}

0 comments on commit fec71ac

Please sign in to comment.