summaryrefslogtreecommitdiff
path: root/frontend/app/views/page/page.actions.js
blob: d2bbbe2f318e202fc17077dab9023acf22fd5bf5 (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
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 })
}

// 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)