/**
 * This file contains "high level" action creators. An action creator is
 * considered high level if it:
 *   - Doesn't create actions directly
 *   - Calls one or more other action creators
 */

import _ from 'lodash';

import fetch from '../helpers/fetch';
import simpleActionCreators from './simpleActionCreators';


const actionCreators = _.clone(simpleActionCreators.notebooks);

const flattenResource = (resource) =>
  _.assign({ id: parseInt(resource.id, 10) }, resource.attributes);

actionCreators.updateFrame = (frame) => (dispatch) =>
  new Promise((resolve, reject) => {
    fetch(`/api/frame/${frame.id}`, {
      method: 'put',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ frame: _.pick(frame, ['x', 'y', 'width', 'height']) })
    }).then((res) => {
      if(!res.ok) throw new Error(res.statusText);
      res.json().then((data) => {
        dispatch(actionCreators.modifyFrame(data));
        resolve(data.frame);
      }).catch(reject);
    }).catch((err) => {
      console.error(err);
      alert('Failed to update frame');
      reject(err);
    });
  });

actionCreators.updateNotebook = (notebook) => (dispatch) =>
  new Promise((resolve, reject) => {
    fetch(`/api/notebook/${notebook.id}`, {
      method: 'put',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ notebook })
    }).then((res) => {
      if(!res.ok) throw new Error(res.statusText);
      res.json().then((data) => {
        dispatch(actionCreators.addNotebook(data.notebook));
        dispatch(simpleActionCreators.tags.removeTagsFromNotebook(data.notebook.id));
        dispatch(simpleActionCreators.tags.addTags(data.notebook.tags));
        resolve(data.notebook);
      }).catch(reject);
    }).catch((err) => {
      console.error(err);
      alert('Failed to update notebook');
      reject(err);
    });
  });

actionCreators.loadNotebook = (notebookId) => (dispatch) =>
  new Promise((resolve, reject) => {
    fetch(`/api/notebook/${notebookId}`).then((res) => {
      if(!res.ok) throw new Error(res.statusText);
      res.json().then((data) => {
        dispatch(actionCreators.addNotebook(data.notebook));
        resolve(data.notebook);
      }).catch(reject);
    }).catch((err) => {
      console.error(err);
      alert('Failed to load notebook');
      reject(err);
    });
  });

actionCreators.loadNotebooksShallow = () => (dispatch) =>
  new Promise((resolve, reject) => {
    fetch('/api/v2/notebooks').then((res) => {
      if(!res.ok) throw new Error(res.statusText);
      res.json().then((data) => {
        const notebooks = data.data.map(flattenResource);
        dispatch(actionCreators.addNotebooks(notebooks));
        resolve(notebooks);
      }).catch(reject);
    }).catch((err) => {
      console.error(err);
      alert('Failed to load notebook');
      reject(err);
    });
  });

actionCreators.deleteNotebook = (notebookId) => (dispatch) =>
  fetch(`/api/v2/notebooks/${notebookId}`, { method: 'delete' }).then((res) => {
    if(!res.ok) throw new Error(res.statusText);
    dispatch(simpleActionCreators.tags.removeTagsFromNotebook(notebookId));
    dispatch(actionCreators.removeNotebook(notebookId));
  }).catch((err) => {
    console.error(err);
    alert('Failed to delete notebook');
  });


export default actionCreators;

@@ -1946,47 +1946,8 @@
  =%3E%0A
-  new Promise((resolve, reject) =%3E %7B%0A  
   fe
@@ -1952,32 +1952,35 @@
 fetch(%60/api/
+v2/
 notebook
 /$%7BnotebookI
@@ -1959,32 +1959,33 @@
 /api/v2/notebook
+s
 /$%7BnotebookId%7D%60)
@@ -1984,16 +1984,21 @@
 ookId%7D%60)
+%0A    
 .then((r
@@ -2055,32 +2055,39 @@
 tusText);%0A      
+return 
 res.json().then(
@@ -2072,32 +2072,45 @@
 eturn res.json()
+;%0A    %7D)%0A    
 .then((data) =%3E 
@@ -2105,32 +2105,259 @@
 n((data) =%3E %7B%0A  
+    const notebook = flattenResource(data.data);%0A      notebook.frames = data.included%0A        .filter(f =%3E f.type === 'frames')%0A        .map(flattenResource)%0A        .map(f =%3E _.assign(%7B%7D, f, %7B content: f.renderedContent %7D));%0A
       dispatch(a
@@ -2374,37 +2374,32 @@
 ors.addNotebook(
-data.
 notebook));%0A    
@@ -2400,39 +2400,31 @@
 ;%0A      
-  resolve(data.
+return 
 notebook
 );%0A     
@@ -2419,41 +2419,21 @@
 book
-)
 ;%0A    
-  %7D).catch(reject);
+%7D)
 %0A    
-%7D)
 .cat
@@ -2450,34 +2450,8 @@
 %3E %7B%0A
-      console.error(err);%0A
     
@@ -2488,35 +2488,33 @@
 ok');%0A      
-reject(
+throw 
 err
-)
 ;%0A    %7D);%0A  
@@ -2502,38 +2502,32 @@
 row err;%0A    %7D);
-%0A  %7D);
 %0A%0AactionCreators

Update loadNotebook to API v2