vigetlabs/microcosm

onNext - A new action callback

Closed this issue · 1 comments

I want a callback that fires on every single status change. This callback would be passed the latest revision entry from the action:

let action = repo.push(uploadFile, '...')

action.onNext(function({ status, payload }) {
  console.log(status, payload)
})

// { status: 'open', payload: null }
// { status: 'update', payload: { progress: 0 } }
// { status: 'update', payload: { progress: 0.17 } }
// { status: 'update', payload: { progress: 0.34 } }
// { status: 'update', payload: { progress: 0.72 } }
// { status: 'resolve', payload: { id: 1, url: "http://file-path.com" } }

This would be particularly useful for ActionForms, which you could use to very quickly write a file uploader:

export default class FileUploader extends React.Component {
  state = {
    status: 'inactive',
    payload: null
  }

  onNext = (revision) {
    this.setState(revision)
  }

  render() {
    const { status, payload } = this.state
    
    if (status === 'update') {
      return <progress value={payload.progress}
    }

    return (
      <ActionForm action={uploadFile} onNext={this.onNext}>
        <input type="file" name="file" />
        <button>Submit</button>
      </ActionForm>
    )
  }
}

This is added in #413