/ src / modules / Submit / Submit.reducer.js
Submit.reducer.js
  1  import submitInitialState from '../../common/data/submit'
  2  import reducerUtil from '../../common/utils/reducer'
  3  import {
  4    onReceiveTransactionInfoAction,
  5    checkTransactionStatusAction,
  6    onStartProgressAction,
  7    hideAction,
  8  } from '../TransactionStatus/TransactionStatus.recuder'
  9  import { TYPE_SUBMIT } from '../TransactionStatus/TransactionStatus.utilities'
 10  import { showAlertAction } from '../Alert/Alert.reducer'
 11  
 12  import BlockchainSDK from '../../common/blockchain'
 13  
 14  const SHOW_SUBMIT_AFTER_CHECK = 'SUBMIT_SHOW_SUBMIT_AFTER_CHECK'
 15  const CLOSE_SUBMIT = 'SUBMIT_CLOSE_SUBMIT'
 16  const ON_INPUT_NAME = 'SUBMIT_ON_INPUT_NAME'
 17  const ON_INPUT_DESC = 'SUBMIT_ON_INPUT_DESC'
 18  const ON_INPUT_URL = 'SUBMIT_ON_INPUT_URL'
 19  const ON_SELECT_CATEGORY = 'SUBMIT_ON_SELECT_CATEGORY'
 20  const ON_IMG_READ = 'SUBMIT_ON_IMG_READ'
 21  const ON_IMG_ZOOM = 'SUBMIT_ON_IMG_ZOOM'
 22  const ON_IMG_MOVE_CONTROL = 'SUBMIT_ON_IMG_MOVE_CONTROL'
 23  const ON_IMG_MOVE = 'SUBMIT_ON_IMG_MOVE'
 24  const ON_IMG_CANCEL = 'SUBMIT_ON_IMG_CANCEL'
 25  const ON_IMG_DONE = 'SUBMIT_ON_IMG_DONE'
 26  
 27  const SWITCH_TO_RATING = 'SUBMIT_SWITCH_TO_RATING'
 28  const ON_INPUT_SNT_VALUE = 'SUBMIT_ON_INPUT_SNT_VALUE'
 29  
 30  export const showSubmitActionAfterCheck = () => {
 31    window.location.hash = 'submit'
 32    return {
 33      type: SHOW_SUBMIT_AFTER_CHECK,
 34      payload: null,
 35    }
 36  }
 37  
 38  export const showSubmitAction = () => {
 39    return (dispatch, getState) => {
 40      const state = getState()
 41      if (state.transactionStatus.progress) {
 42        dispatch(
 43          showAlertAction(
 44            'There is an active transaction. Please wait for it to finish and then you could be able to create your Ðapp',
 45          ),
 46        )
 47      } else dispatch(showSubmitActionAfterCheck())
 48    }
 49  }
 50  
 51  export const closeSubmitAction = () => {
 52    window.history.back()
 53    return {
 54      type: CLOSE_SUBMIT,
 55      payload: null,
 56    }
 57  }
 58  
 59  export const onInputNameAction = name => ({
 60    type: ON_INPUT_NAME,
 61    payload: name,
 62  })
 63  
 64  export const onInputDescAction = desc => ({
 65    type: ON_INPUT_DESC,
 66    payload: desc.substring(0, 140),
 67  })
 68  
 69  export const onInputUrlAction = url => ({
 70    type: ON_INPUT_URL,
 71    payload: url,
 72  })
 73  
 74  export const onSelectCategoryAction = category => ({
 75    type: ON_SELECT_CATEGORY,
 76    payload: category,
 77  })
 78  
 79  export const onImgReadAction = imgBase64 => ({
 80    type: ON_IMG_READ,
 81    payload: imgBase64,
 82  })
 83  
 84  export const onImgZoomAction = zoom => ({
 85    type: ON_IMG_ZOOM,
 86    payload: zoom,
 87  })
 88  
 89  export const onImgMoveControlAction = move => ({
 90    type: ON_IMG_MOVE_CONTROL,
 91    payload: move,
 92  })
 93  
 94  export const onImgMoveAction = (x, y) => ({
 95    type: ON_IMG_MOVE,
 96    payload: { x, y },
 97  })
 98  
 99  export const onImgCancelAction = () => ({
100    type: ON_IMG_CANCEL,
101    payload: null,
102  })
103  
104  export const onImgDoneAction = imgBase64 => ({
105    type: ON_IMG_DONE,
106    payload: imgBase64,
107  })
108  
109  export const submitAction = (dapp, sntValue) => {
110    return async dispatch => {
111      dispatch(closeSubmitAction())
112      dispatch(
113        onStartProgressAction(
114          dapp.name,
115          dapp.img,
116          'Status is an open source mobile DApp browser and messenger build for #Etherium',
117          TYPE_SUBMIT,
118        ),
119      )
120      try {
121        const blockchain = await BlockchainSDK.getInstance()
122        const { tx, id } = await blockchain.DiscoverService.createDApp(sntValue, {
123          name: dapp.name,
124          url: dapp.url,
125          desc: dapp.desc,
126          category: dapp.category,
127          image: dapp.img,
128        })
129        dispatch(onReceiveTransactionInfoAction(id, tx))
130        dispatch(checkTransactionStatusAction(tx))
131      } catch (e) {
132        dispatch(hideAction())
133        dispatch(showAlertAction(e.message))
134      }
135    }
136  }
137  
138  export const switchToRatingAction = () => ({
139    type: SWITCH_TO_RATING,
140    paylaod: null,
141  })
142  
143  export const onInputSntValueAction = sntValue => ({
144    type: ON_INPUT_SNT_VALUE,
145    payload: sntValue,
146  })
147  
148  const showSubmitAfterCheck = state => {
149    return Object.assign({}, state, {
150      visible_submit: true,
151      visible_rating: false,
152      id: '',
153      name: '',
154      desc: '',
155      url: '',
156      img: '',
157      category: '',
158      imgControl: false,
159      imgControlZoom: 0,
160      imgControlMove: false,
161      imgControlX: 0,
162      imgControlY: 0,
163      sntValue: '0',
164    })
165  }
166  
167  const closeSubmit = state => {
168    return Object.assign({}, state, {
169      visible: false,
170    })
171  }
172  
173  const onInputName = (state, name) => {
174    return Object.assign({}, state, {
175      name,
176    })
177  }
178  
179  const onInputDesc = (state, desc) => {
180    return Object.assign({}, state, {
181      desc,
182    })
183  }
184  
185  const onInputUrl = (state, url) => {
186    return Object.assign({}, state, {
187      url,
188    })
189  }
190  
191  const onSelectCategory = (state, category) => {
192    return Object.assign({}, state, {
193      category,
194    })
195  }
196  
197  const onImgRead = (state, imgBase64) => {
198    return Object.assign({}, state, {
199      img: imgBase64,
200      imgControl: true,
201      imgControlZoom: 0,
202      imgControlMove: false,
203      imgControlX: 0,
204      imgControlY: 0,
205    })
206  }
207  
208  const onImgZoom = (state, zoom) => {
209    return Object.assign({}, state, {
210      imgControlZoom: zoom,
211    })
212  }
213  
214  const onImgMoveControl = (state, move) => {
215    return Object.assign({}, state, {
216      imgControlMove: move,
217    })
218  }
219  
220  const onImgMove = (state, payload) => {
221    return Object.assign({}, state, {
222      imgControlX: payload.x,
223      imgControlY: payload.y,
224    })
225  }
226  
227  const onImgCancel = state => {
228    return Object.assign({}, state, {
229      img: '',
230      imgControl: false,
231    })
232  }
233  
234  const onImgDone = (state, imgBase64) => {
235    return Object.assign({}, state, {
236      img: imgBase64,
237      imgControl: false,
238    })
239  }
240  
241  const switchToRating = state => {
242    return Object.assign({}, state, {
243      visible_submit: false,
244      visible_rating: true,
245    })
246  }
247  
248  const onInputSntValue = (state, sntValue) => {
249    return Object.assign({}, state, {
250      sntValue,
251    })
252  }
253  
254  const map = {
255    [SHOW_SUBMIT_AFTER_CHECK]: showSubmitAfterCheck,
256    [CLOSE_SUBMIT]: closeSubmit,
257    [ON_INPUT_NAME]: onInputName,
258    [ON_INPUT_DESC]: onInputDesc,
259    [ON_INPUT_URL]: onInputUrl,
260    [ON_SELECT_CATEGORY]: onSelectCategory,
261    [ON_IMG_READ]: onImgRead,
262    [ON_IMG_ZOOM]: onImgZoom,
263    [ON_IMG_MOVE_CONTROL]: onImgMoveControl,
264    [ON_IMG_MOVE]: onImgMove,
265    [ON_IMG_CANCEL]: onImgCancel,
266    [ON_IMG_DONE]: onImgDone,
267    [SWITCH_TO_RATING]: switchToRating,
268    [ON_INPUT_SNT_VALUE]: onInputSntValue,
269  }
270  
271  export default reducerUtil(map, submitInitialState)