summaryrefslogtreecommitdiff
path: root/frontend/app/views/page/page.actions.js
blob: 0ae38e0391767cccaabf37cef4f27f0be5c6e99a (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as types from 'app/types'
import { store } from 'app/store'
import actions from 'app/actions'
import { default as debounce } from 'lodash.debounce'
import { post } from 'app/utils'

const url = {
  sortTiles: (id) => '/api/v1/page/' + id + '/sort',
}

// Add tile form

export const showAddTileForm = () => dispatch => {
  dispatch({ type: types.page.show_add_tile_form })
}

export const hideAddTileForm = () => dispatch => {
  dispatch({ type: types.page.hide_add_tile_form })
}

export const toggleAddTileForm = () => dispatch => {
  dispatch({ type: types.page.toggle_add_tile_form })
}

// Edit tile form

export const showEditTileForm = tile_id => dispatch => {
  dispatch({ type: types.page.show_edit_tile_form, tile_id })
}

export const hideEditTileForm = () => dispatch => {
  dispatch({ type: types.page.hide_edit_tile_form })
}

export const toggleEditTileForm = () => dispatch => {
  dispatch({ type: types.page.toggle_edit_tile_form })
}

// Tile list

export const showTileList = () => dispatch => {
  dispatch({ type: types.page.show_tile_list })
}

export const hideTileList = () => dispatch => {
  dispatch({ type: types.page.hide_tile_list })
}

export const toggleTileList = () => dispatch => {
  dispatch({ type: types.page.toggle_tile_list })
}

// Popups

export const loadPopups = (page, popups) => dispatch => {
  const state = store.getState()
  page = page || state.page.show.res
  popups = popups || state.page.editor.popups
  popups = page.tiles.reduce((acc, tile) => {
    const { is_popup, popup_group } = tile.settings
    if (is_popup) {
      acc[popup_group] = acc[popup_group] || false
    }
    return acc
  }, { ...popups })
  console.log(popups)
  dispatch({ type: types.page.load_popups, popups })
}
export const togglePopups = () => dispatch => {
  dispatch({ type: types.page.toggle_popups })
}

export const toggleSidebarSide = () => dispatch => {
  dispatch({ type: types.page.toggle_sidebar_side })
}

// Update local page tile state when we change it

export const updatePageTile = tile => dispatch => {
  dispatch({ type: types.page.update_page_tile, tile })
}

// Fetch graph/page when loading a new URL

export const showGraphAndPageIfUnloaded = ({ graph_name, page_name }) => dispatch => (
  new Promise((resolve, reject) => {
    showGraphIfUnloaded({ graph_name })(dispatch)
    .then(graph => (
      actions.page.show('name/'  + graph_name + '/' + page_name)
      .then(resolve)
      .catch(reject)
    ))
    .catch(reject)
  })
)

export const showGraphIfUnloaded = ({ graph_name }) => dispatch => (
  new Promise((resolve, reject) => {
    const { res: graph } = store.getState().graph.show
    if (graph && graph.path === graph_name) {
      return resolve(graph)
    }
    actions.graph.show('name/'  + graph_name)
    .then(resolve)
    .catch(reject)
  })
)

// Sorting tiles in the tile list

export const setTileSortOrder = (tiles) => dispatch => {
  let oldTiles = store.getState().page.show.res.tiles
  if (!isSameTileOrder(tiles, oldTiles)) {
    updateTileSortOrder(tiles, dispatch)
  }
  dispatch({ type: types.page.set_tile_sort_order, tiles })
}

export const isSameTileOrder = (newTiles, oldTiles) => {
  for (let i = 0; i < newTiles.length; i++) {
    if (newTiles[i].id !== oldTiles[i].id) {
      return false
    }
  }
  return true
}

export const updateTileSortOrder = debounce((tiles, dispatch) => {
  const { page_id } = tiles[0]
  const order = tiles.map(tile => (tile.id))
  console.log(page_id, order)
  post(dispatch, types.page, 'sort', url.sortTiles(page_id), order)
}, 1000)