|
1 | 1 | import { useState, useEffect, useMemo, useCallback } from 'react' |
2 | 2 |
|
3 | 3 | import { AppActions, StateDispatch } from 'states/stateProvider/reducer' |
4 | | -import actionCreators from 'states/stateProvider/actionCreators' |
| 4 | +import { Message, MAX_NETWORK_NAME_LENGTH, Routes } from 'utils/const' |
| 5 | +import { createNetwork, updateNetwork } from 'services/remote' |
5 | 6 |
|
6 | 7 | import i18n from 'utils/i18n' |
7 | 8 |
|
@@ -119,11 +120,89 @@ export const useHandleSubmit = ( |
119 | 120 | name: string = '', |
120 | 121 | remote: string = '', |
121 | 122 | networks: State.Network[] = [], |
| 123 | + history: any, |
122 | 124 | dispatch: StateDispatch |
123 | 125 | ) => |
124 | | - useCallback(() => { |
125 | | - dispatch(actionCreators.createOrUpdateNetwork({ id, name, remote }, networks)) |
126 | | - }, [id, name, remote, networks, dispatch]) |
| 126 | + useCallback(async () => { |
| 127 | + const warning = { |
| 128 | + type: 'warning', |
| 129 | + timestamp: Date.now(), |
| 130 | + content: '', |
| 131 | + } |
| 132 | + let res |
| 133 | + if (!name) { |
| 134 | + return dispatch({ |
| 135 | + type: AppActions.AddNotification, |
| 136 | + payload: { |
| 137 | + ...warning, |
| 138 | + content: i18n.t(Message.NameRequired), |
| 139 | + }, |
| 140 | + }) |
| 141 | + } |
| 142 | + if (name.length > MAX_NETWORK_NAME_LENGTH) { |
| 143 | + return dispatch({ |
| 144 | + type: AppActions.AddNotification, |
| 145 | + payload: { |
| 146 | + ...warning, |
| 147 | + content: i18n.t(Message.LengthOfNameShouldBeLessThanOrEqualTo, { |
| 148 | + length: MAX_NETWORK_NAME_LENGTH, |
| 149 | + }), |
| 150 | + }, |
| 151 | + }) |
| 152 | + } |
| 153 | + if (!remote) { |
| 154 | + return dispatch({ |
| 155 | + type: AppActions.AddNotification, |
| 156 | + payload: { |
| 157 | + ...warning, |
| 158 | + content: i18n.t(Message.URLRequired), |
| 159 | + }, |
| 160 | + }) |
| 161 | + } |
| 162 | + if (!remote.startsWith('http')) { |
| 163 | + return dispatch({ |
| 164 | + type: AppActions.AddNotification, |
| 165 | + payload: { |
| 166 | + ...warning, |
| 167 | + content: i18n.t(Message.ProtocolRequired), |
| 168 | + }, |
| 169 | + }) |
| 170 | + } |
| 171 | + // verification, for now, only name is unique |
| 172 | + if (id === 'new') { |
| 173 | + if (networks.some(network => network.name === name)) { |
| 174 | + return dispatch({ |
| 175 | + type: AppActions.AddNotification, |
| 176 | + payload: { |
| 177 | + ...warning, |
| 178 | + content: i18n.t(Message.NetworkNameUsed), |
| 179 | + }, |
| 180 | + }) |
| 181 | + } |
| 182 | + res = await createNetwork({ |
| 183 | + name, |
| 184 | + remote, |
| 185 | + }) |
| 186 | + } else { |
| 187 | + if (networks.some(network => network.name === name && network.id !== id)) { |
| 188 | + return dispatch({ |
| 189 | + type: AppActions.AddNotification, |
| 190 | + payload: { |
| 191 | + ...warning, |
| 192 | + content: i18n.t(Message.NetworkNameUsed), |
| 193 | + }, |
| 194 | + }) |
| 195 | + } |
| 196 | + res = await updateNetwork(id!, { |
| 197 | + name, |
| 198 | + remote, |
| 199 | + }) |
| 200 | + } |
| 201 | + if (res && res.status) { |
| 202 | + history.push(Routes.SettingsNetworks) |
| 203 | + } |
| 204 | + return res |
| 205 | + }, [id, name, remote, networks, history, dispatch]) |
127 | 206 |
|
128 | 207 | export default { |
129 | 208 | useInitialize, |
|
0 commit comments