3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 import $ from 'jquery';
19 var Utils
= require('utils/utils.js');
20 let API_SERVER
= require('utils/rw.js').getSearchParams(window
.location
).api_server
;
21 let HOST
= API_SERVER
;
22 let NODE_PORT
= require('utils/rw.js').getSearchParams(window
.location
).api_port
|| ((window
.location
.protocol
== 'https:') ? 8443 : 8000);
23 let DEV_MODE
= require('utils/rw.js').getSearchParams(window
.location
).dev_mode
|| false;
26 HOST
= window
.location
.protocol
+ '//' + window
.location
.hostname
;
30 module
.exports = function(Alt
) {
33 remote: function(state
) {
34 return new Promise(function(resolve
, reject
) {
35 //If socket connection already exists, eat the request.
37 return resolve(false);
40 url
: '//' + window
.location
.hostname
+ ':' + NODE_PORT
+ '/socket-polling?api_server=' + API_SERVER
,
42 beforeSend
: Utils
.addAuthorizationStub
,
44 url
:window
.location
.protocol
+ '//' + window
.location
.host
+ '/accounts/all?api_server=' + API_SERVER
46 success: function(data
, textStatus
, jqXHR
) {
47 Utils
.checkAndResolveSocketRequest(data
, resolve
, reject
);
49 }).fail(function(xhr
){
50 //Authentication and the handling of fail states should be wrapped up into a connection class.
51 Utils
.checkAuthentication(xhr
.status
);
55 loading
: Alt
.actions
.global
.openAccountSocketLoading
,
56 success
: Alt
.actions
.global
.openAccountSocketSuccess
,
57 error
: Alt
.actions
.global
.openAccountSocketError
60 remote: function(state
, accounts
) {
61 return new Promise(function(resolve
, reject
) {
62 let Refreshing
= syncRefresh();
64 function* syncRefresh() {
65 for (let t
of accounts
) {
66 if (t
.data
.constructor.name
== "Array") {
67 for (let u
of t
.data
) {
68 console
.log('Refreshing: ' + u
.name
);
69 yield refreshStatus(t
.type
, u
.name
);
76 function refreshStatus(type
, name
) {
77 let url
= type
+ '/' + name
+ '/refresh?api_server=' + API_SERVER
;
78 setTimeout(function(){
84 beforeSend
: Utils
.addAuthorizationStub
,
95 loading
: Alt
.actions
.global
.refreshAllAccountsLoading
,
96 success
: Alt
.actions
.global
.refreshAllAccountsSuccess
,
97 error
: Alt
.actions
.global
.refreshAllAccountsError
100 remote: function(state
, name
, type
) {
101 return new Promise(function(resolve
, reject
) {
103 url
: type
+ '/' + name
+ '/refresh?api_server=' + API_SERVER
,
105 beforeSend
: Utils
.addAuthorizationStub
,
106 success: function(account
) {
112 success
: Alt
.actions
.global
.refreshCloudAccountSuccess
,
113 error
: Alt
.actions
.global
.refreshCloudAccountFail
116 remote: function(state
, account
, type
, cb
) {
117 delete account
['connection-status'];
118 return new Promise(function(resolve
, reject
) {
120 url
: type
+ '?api_server=' + API_SERVER
,
122 beforeSend
: Utils
.addAuthorizationStub
,
123 data
: JSON
.stringify(account
),
124 contentType
: "application/json",
125 success: function(data
) {
128 error: function(error
) {
129 console
.log("There was an error creating the account: ", arguments
);
132 }).fail(function(xhr
){
133 //Authentication and the handling of fail states should be wrapped up into a connection class.
134 Utils
.checkAuthentication(xhr
.status
);
140 success
: Alt
.actions
.global
.createAccountSuccess
,
141 loading
: Alt
.actions
.global
.createAccountLoading
,
142 error
: Alt
.actions
.global
.createAccountFail
145 remote: function(state
, account
, type
, cb
) {
146 var payload
= Object
.assign({}, account
);
147 delete payload
['connection-status'];
148 delete payload
['params'];
149 delete payload
['pools'];
151 (payload
.nestedParams
== null) &&
152 delete payload
.nestedParams
155 payload
.nestedParams
&&
156 payload
.nestedParams
['container-name'] &&
157 delete payload
[payload
.nestedParams
['container-name']] &&
158 delete payload
.nestedParams
162 return new Promise(function(resolve
, reject
) {
164 url
: type
+ '/' + account
.name
+ '?api_server=' + API_SERVER
,
166 beforeSend
: Utils
.addAuthorizationStub
,
167 data
: JSON
.stringify(payload
),
168 contentType
: "application/json",
169 success: function(data
) {
172 error: function(error
) {
173 console
.log("There was an error updating the account: ", arguments
);
176 }).fail(function(xhr
){
177 //Authentication and the handling of fail states should be wrapped up into a connection class.
178 Utils
.checkAuthentication(xhr
.status
);
184 interceptResponse
: interceptResponse({
185 'error': 'There was an error updating the account.'
187 success
: Alt
.actions
.global
.createAccountSuccess
,
188 loading
: Alt
.actions
.global
.createAccountLoading
,
189 error
: Alt
.actions
.global
.showNotification
192 remote: function(state
, type
, name
, cb
) {
193 return new Promise(function(resolve
, reject
) {
195 url
: type
+ '/' + name
+ '/?api_server=' + API_SERVER
,
198 beforeSend
: Utils
.addAuthorizationStub
,
199 success: function(data
) {
200 console
.log('Account deleted');
203 }).fail(function(xhr
){
204 //Authentication and the handling of fail states should be wrapped up into a connection class.
205 Utils
.checkAuthentication(xhr
.status
);
210 interceptResponse
: interceptResponse({
211 'error': 'Something went wrong while trying to delete the account. Check the error logs for more information' }),
212 success
: Alt
.actions
.global
.deleteAccountSuccess
,
213 loading
: Alt
.actions
.global
.deleteAccountLoading
,
214 error
: Alt
.actions
.global
.showNotification
219 function interceptResponse (responses
) {
220 return function(data
, action
, args
) {
221 if(responses
.hasOwnProperty(data
)) {