aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/ui/flow.js
blob: 22a8c22d7a20d1d659711c02f2162979d912b591 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import * as flowsActions from '../flows'
import { getDiff } from "../../utils"

import _ from 'lodash'

export const SET_CONTENT_VIEW               = 'UI_FLOWVIEW_SET_CONTENT_VIEW',
             DISPLAY_LARGE                  = 'UI_FLOWVIEW_DISPLAY_LARGE',
             SET_TAB                        = "UI_FLOWVIEW_SET_TAB",
             START_EDIT                     = 'UI_FLOWVIEW_START_EDIT',
             UPDATE_EDIT                    = 'UI_FLOWVIEW_UPDATE_EDIT',
             UPLOAD_CONTENT                 = 'UI_FLOWVIEW_UPLOAD_CONTENT',
             SET_SHOW_FULL_CONTENT          = 'UI_SET_SHOW_FULL_CONTENT',
             SET_CONTENT_VIEW_DESCRIPTION   = "UI_SET_CONTENT_VIEW_DESCRIPTION",
             SET_CONTENT                    = "UI_SET_CONTENT"


const defaultState = {
    displayLarge: false,
    contentViewDescription: '',
    showFullContent: false,
    modifiedFlow: false,
    contentView: 'Auto',
    tab: 'request',
    content: [],
    maxContentLines: 80,
}

export default function reducer(state = defaultState, action) {
    let wasInEditMode = !!(state.modifiedFlow)
    switch (action.type) {

        case START_EDIT:
            return {
                ...state,
                modifiedFlow: action.flow,
                contentView: 'Edit',
                showFullContent: true
            }

        case UPDATE_EDIT:
            return {
                ...state,
                modifiedFlow: _.merge({}, state.modifiedFlow, action.update)
            }

        case flowsActions.SELECT:
            return {
                ...state,
                modifiedFlow: false,
                displayLarge: false,
                contentView: (wasInEditMode ? 'Auto' : state.contentView),
                viewDescription: '',
                showFullContent: false,
            }

        case flowsActions.UPDATE:
            // There is no explicit "stop edit" event.
            // We stop editing when we receive an update for
            // the currently edited flow from the server
            if (action.item.id === state.modifiedFlow.id) {
                return {
                    ...state,
                    modifiedFlow: false,
                    displayLarge: false,
                    contentView: (wasInEditMode ? 'Auto' : state.contentView),
                    viewDescription: '',
                    showFullContent: false
                }
            } else {
                return state
            }

        case SET_CONTENT_VIEW_DESCRIPTION:
            return {
                ...state,
                viewDescription: action.description
            }

        case SET_SHOW_FULL_CONTENT:
            return {
                ...state,
                showFullContent: action.show
            }

        case SET_TAB:
            return {
                ...state,
                tab: action.tab ? action.tab : 'request',
                displayLarge: false,
                showFullContent: false
            }

        case SET_CONTENT_VIEW:
            return {
                ...state,
                contentView: action.contentView,
                showFullContent: action.contentView == 'Edit'
            }

        case SET_CONTENT:
            let isFullContentShown = action.content.length < state.maxContentLines
            return {
                ...state,
                content: action.content,
                showFullContent: isFullContentShown
            }

        case DISPLAY_LARGE:
            return {
                ...state,
                displayLarge: true,
            }
        default:
            return state
    }
}

export function setContentView(contentView) {
    return { type: SET_CONTENT_VIEW, contentView }
}

export function displayLarge() {
    return { type: DISPLAY_LARGE }
}

export function selectTab(tab) {
    return { type: SET_TAB, tab }
}

export function startEdit(flow) {
    return { type: START_EDIT, flow }
}

export function updateEdit(update) {
    return { type: UPDATE_EDIT, update }
}

export function setContentViewDescription(description) {
    return { type: SET_CONTENT_VIEW_DESCRIPTION, description }
}

export function setShowFullContent(show) {
    return { type: SET_SHOW_FULL_CONTENT, show }
}

export function updateEdit(update) {
    return { type: UPDATE_EDIT, update }
}

export function setContent(content){
    return { type: SET_CONTENT, content}
}

export function stopEdit(flow, modifiedFlow) {
    let diff = getDiff(flow, modifiedFlow)
    return flowsActions.update(flow, diff)
}