All files / src/actions users.js

46.15% Statements 6/13
100% Branches 0/0
22.22% Functions 2/9
55.56% Lines 5/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                  4x     3x     4x         4x                             4x                            
import fetch from 'isomorphic-fetch'
 
import {
  FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE,
  CREATE_USER_REQUEST, CREATE_USER_SUCCESS, CREATE_USER_FAILURE,
  LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE
} from '../actionTypes'
import { thunkCreator } from './utils'
 
export const fetchUser = (username) => thunkCreator({
  types: [ FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE ],
  promise: fetch(`http://localhost:8080/api/users/${username}`)
           .then(response => response.json())
})
 
export const fetchUsersByUsernames = (usernames) => (dispatch) =>
  Promise.all(usernames.map(username =>
    fetchUser(username)(dispatch)
  ))
 
export const createUser = (username, realname, password) => thunkCreator({
  types: [ CREATE_USER_REQUEST, CREATE_USER_SUCCESS, CREATE_USER_FAILURE ],
  promise: fetch('http://localhost:8080/api/users', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      username, realname, password
    })
  })
  .then(response => response.json())
})
 
export const login = (username, password) => thunkCreator({
  types: [ LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE ],
  promise: fetch('http://localhost:8080/api/login', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      username, password
    })
  })
  .then(response => response.json())
})