2016-12-15 01:06:14 +01:00
|
|
|
import fetch from 'isomorphic-fetch'
|
2016-12-14 19:02:27 +01:00
|
|
|
import uuid from 'uuid/v4'
|
2016-12-17 06:26:04 +01:00
|
|
|
import {browserHistory} from 'react-router'
|
|
|
|
import {Status, StatusError} from './constants'
|
|
|
|
|
|
|
|
//All state transitions in the app happen in these methods
|
|
|
|
//this includes redux state changes, asyncronous data requests, and browser location changes
|
2016-12-14 19:02:27 +01:00
|
|
|
|
|
|
|
export const updateOriginalInput = (text) =>
|
|
|
|
(dispatch, getState) => {
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch({type: 'UPDATE_ORIGINAL_INPUT', data:text})
|
2016-12-14 19:02:27 +01:00
|
|
|
if (getState().input.original.length>0)
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch({type: 'STATUS_SET', data:Status.DIRTY})
|
2016-12-14 19:02:27 +01:00
|
|
|
else
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch({type: 'STATUS_SET', data:Status.EMPTY})
|
2016-12-14 19:02:27 +01:00
|
|
|
}
|
2016-12-17 06:26:04 +01:00
|
|
|
|
2016-12-14 19:02:27 +01:00
|
|
|
export const updateFinalInput = (text) =>
|
|
|
|
(dispatch, getState) => {
|
|
|
|
dispatch({ type: 'UPDATE_FINAL_INPUT', data:text})
|
|
|
|
if (getState().input.final.length>0)
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch({type: 'STATUS_SET', data:Status.DIRTY})
|
2016-12-14 19:02:27 +01:00
|
|
|
else
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch({type: 'STATUS_SET', data:Status.EMPTY})
|
2016-12-14 19:02:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const clearInput = () =>
|
|
|
|
(dispatch) => {
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch({type: 'CLEAR_INPUT'})
|
|
|
|
dispatch({type: 'STATUS_SET', data:Status.EMPTY})
|
2016-12-14 19:02:27 +01:00
|
|
|
}
|
2016-11-23 22:58:48 +01:00
|
|
|
|
2016-11-28 18:47:47 +01:00
|
|
|
export const setPlaintextFormat = () => ({ type: 'SET_PLAINTEXT_FORMAT'})
|
|
|
|
export const setMarkdownFormat = () => ({ type: 'SET_MARKDOWN_FORMAT'})
|
|
|
|
export const showOriginal = () => ({ type: 'SHOW_ORIGINAL'})
|
|
|
|
export const showFinal = () => ({ type: 'SHOW_FINAL'})
|
|
|
|
export const showDifference = () => ({ type: 'SHOW_DIFFERENCE'})
|
2016-12-14 19:02:27 +01:00
|
|
|
|
2016-12-15 01:06:14 +01:00
|
|
|
|
2016-12-17 06:26:04 +01:00
|
|
|
//if the input is dirty, saves it to the server
|
|
|
|
//creates a new uuid for the same,
|
|
|
|
//then changes the browser location to a comparison view with that id
|
|
|
|
export const compare = () =>
|
|
|
|
(dispatch, getState) => {
|
|
|
|
//!!! could test that the input is dirty before triggering a save
|
|
|
|
//if the input is empty, the compare should do nothing
|
|
|
|
//if the input is clean, the compare should not save and keep using the same id
|
|
|
|
|
|
|
|
//start saving the input to the server
|
|
|
|
const id = dispatch(save())
|
|
|
|
|
|
|
|
//we can use the id created by the save method to build a path
|
|
|
|
const comparePath = `/${id}`
|
|
|
|
browserHistory.replace(comparePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//clear the input and return to the edit page
|
|
|
|
export const reset = () =>
|
|
|
|
(dispatch, getState) => {
|
|
|
|
dispatch(clearInput())
|
|
|
|
browserHistory.push('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//switch to the edit view
|
|
|
|
export const edit = () =>
|
|
|
|
(dispatch, getState) => {
|
|
|
|
browserHistory.push('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-15 01:06:14 +01:00
|
|
|
//saves the current input fields to the server
|
2016-12-17 06:26:04 +01:00
|
|
|
//creates and returns a new id for the comparison
|
|
|
|
//should this method ensure that the initial state is valid? ('DIRTY')
|
2016-12-14 19:02:27 +01:00
|
|
|
export const save = () =>
|
|
|
|
(dispatch, getState) => {
|
|
|
|
|
|
|
|
//generate an id
|
|
|
|
const id = uuid()
|
|
|
|
|
|
|
|
//set waiting state
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch( {type: 'STATUS_SET', data:Status.SAVING})
|
2016-12-14 19:02:27 +01:00
|
|
|
|
2016-12-15 01:06:14 +01:00
|
|
|
const endpointUri = `/api/compare/${id}`
|
|
|
|
const fetchOptions = {
|
2016-12-14 19:02:27 +01:00
|
|
|
method: 'POST',
|
2016-12-15 01:06:14 +01:00
|
|
|
body: JSON.stringify({
|
2016-12-14 19:02:27 +01:00
|
|
|
a: getState().input.original,
|
|
|
|
b: getState().input.final
|
2016-12-15 01:06:14 +01:00
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
2016-12-14 19:02:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-12-15 01:06:14 +01:00
|
|
|
|
2016-12-14 19:02:27 +01:00
|
|
|
//dispatch post request
|
2016-12-15 01:06:14 +01:00
|
|
|
fetch(endpointUri, fetchOptions)
|
|
|
|
.then(response => {
|
2016-12-17 16:21:00 +01:00
|
|
|
if (response.ok)
|
|
|
|
dispatch({type: 'STATUS_SET', data: Status.CLEAN})
|
|
|
|
else {
|
|
|
|
response.text().then( (responseText) => {
|
|
|
|
const error = {message:`${response.status}: ${responseText}`}
|
|
|
|
dispatch({type: 'STATUS_SET', data: Status.DIRTY})
|
|
|
|
dispatch({type: 'STATUS_SET_ERROR', data: StatusError.SAVE_ERROR, error})
|
|
|
|
})
|
|
|
|
}
|
2016-12-14 19:02:27 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2016-12-17 16:21:00 +01:00
|
|
|
//!!! could use a better error message here
|
2016-12-17 06:26:04 +01:00
|
|
|
dispatch({type: 'STATUS_SET', data: Status.DIRTY})
|
|
|
|
dispatch({type: 'STATUS_SET_ERROR', data: StatusError.SAVE_ERROR, error})
|
2016-12-14 19:02:27 +01:00
|
|
|
})
|
2016-12-15 01:06:14 +01:00
|
|
|
|
|
|
|
//return the id after the request has been sent
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
const load = (id) =>
|
|
|
|
(dispatch, getState) => {
|
|
|
|
|
|
|
|
//set waiting state
|
|
|
|
dispatch( {type: 'SAVE_STATUS_WAITING'})
|
|
|
|
|
|
|
|
const endpointUri = `/api/compare/${id}`
|
|
|
|
const fetchOptions = {
|
|
|
|
method: 'GET'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//dispatch post request
|
|
|
|
fetch(endpointUri, fetchOptions)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(json => {
|
|
|
|
dispatch( {type: 'UPDATE_ORIGINAL_INPUT', data:json.a})
|
|
|
|
dispatch( {type: 'UPDATE_FINAL_INPUT', data:json.b})
|
|
|
|
dispatch( {type: 'LOAD_STATUS_LOADED'})
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
dispatch( {type: 'LOAD_STATUS_FAILED', error})
|
|
|
|
})
|
|
|
|
|
|
|
|
//return the id after the request has been sent
|
|
|
|
return id;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadIfNeeded = (id) =>
|
|
|
|
(dispatch, getState) => {
|
|
|
|
if
|
|
|
|
}
|
|
|
|
*/
|