{"size": 1032, "ext": "js", "max_stars_repo_path": "web/src/shorten/shorten.duck.js", "max_stars_repo_name": "ron-liu/shorten-url", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "web/src/shorten/shorten.duck.js", "max_issues_repo_name": "ron-liu/shorten-url", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "web/src/shorten/shorten.duck.js", "max_forks_repo_name": "ron-liu/shorten-url", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.1707317073, "max_line_length": 61, "alphanum_fraction": 0.6947674419}
// actions
import {shorten} from "../services";
import {addShortenedUrl} from '../histories/histories.duck'

const SHORTENING = 'shorten/shortening'
const SHORTENED = 'shorten/shortened'
const ERROR = 'shorten/error'
const READY = 'shorten/ready'
const TO_SHORTEN = 'shorten/shorten'

const initState = {status: 'READY'}

export default (state = initState, action) => {
	switch (action.type) {
		case SHORTENING:
			return {status: 'SHORTENING'}
		case SHORTENED:
			return {status: 'SHORTENED'}
		case ERROR:
			return {status: 'ERROR', message: action.message}
		case READY:
			return {status: 'READY'}
		default:
			return state
	}
}

export const setError = (message) => ({type: ERROR, message})

export const toShorten = originalUrl => dispatch => {
	dispatch({type: SHORTENING})
	return shorten(originalUrl)
	.then(({url}) => {
		dispatch({type: SHORTENED})
		dispatch(addShortenedUrl({originalUrl, shortenedUrl: url}))
		setTimeout(() => dispatch({type: READY}), 2000)
	})
	.catch(err => dispatch(setError( err.message)))
}

