// API calls in this file :
// - declare_page
// - set_webentity_homepage

import jsonrpc from '../utils/jsonrpc'

// when opening a page
export const DECLARE_PAGE = '§_DECLARE_PAGE'
export const DECLARE_PAGE_REQUEST = '§_DECLARE_PAGE_REQUEST'
export const DECLARE_PAGE_SUCCESS = '§_DECLARE_PAGE_SUCCESS'
export const DECLARE_PAGE_FAILURE = '§_DECLARE_PAGE_FAILURE'


export const declarePage = (serverUrl, corpusId, url) => (dispatch) => {
  dispatch({ type: DECLARE_PAGE_REQUEST, payload: { serverUrl, corpusId, url } })

  return jsonrpc(serverUrl)('declare_page', [url, corpusId])
    /*.then((res) => {
      if (!res.homepage) {
        // Set homepage to requested URL
        return jsonrpc(serverUrl)('set_webentity_homepage', [res.id, url, corpusId]).then(() => res)
      }
    })*/
    .then((res) => dispatch({ type: DECLARE_PAGE_SUCCESS, payload: { serverUrl, corpusId, url, webentity: res.result } }))
    .catch((error) => dispatch({ type: DECLARE_PAGE_FAILURE, payload: { serverUrl, corpusId, url, error } }))
}

@@ -44,16 +44,22 @@
 ge%0A// - 
+store.
 set_webe
@@ -592,20 +592,58 @@
     
-/*
 .then((
+%7B 
 res
+ult %7D) =%3E result)%0A    .then((webentity
 ) =%3E
@@ -656,19 +656,25 @@
    if (!
-res
+webentity
 .homepag
@@ -718,16 +718,35 @@
 sted URL
+ if not defined yet
 %0A       
@@ -773,16 +773,22 @@
 erUrl)('
+store.
 set_webe
@@ -805,19 +805,25 @@
 page', %5B
-res
+webentity
 .id, url
@@ -846,20 +846,130 @@
 n(() =%3E 
-res)
+%7B%0A          webentity.homepage = url%0A          return webentity%0A        %7D)%0A      %7D else %7B%0A        return webentity
 %0A      %7D
@@ -979,10 +979,8 @@
   %7D)
-*/
 %0A   
@@ -987,19 +987,25 @@
  .then((
-res
+webentity
 ) =%3E dis
@@ -1090,20 +1090,8 @@
 tity
-: res.result
  %7D %7D

set homepage after declare_page