update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / project_management / src / dashboard / projectMgmtSource.js
1 /*
2 * STANDARD_RIFT_IO_COPYRIGHT
3 */
4 import $ from 'jquery';
5 var Utils = require('utils/utils.js');
6 let API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
7 let HOST = API_SERVER;
8 let NODE_PORT = require('utils/rw.js').getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
9 let DEV_MODE = require('utils/rw.js').getSearchParams(window.location).dev_mode || false;
10
11 if (DEV_MODE) {
12 HOST = window.location.protocol + '//' + window.location.hostname;
13 }
14
15
16
17 module.exports = function(Alt) {
18 return {
19
20 getUsers: {
21 remote: function() {
22 return new Promise(function(resolve, reject) {
23 $.ajax({
24 url: `/user?api_server=${API_SERVER}`,
25 type: 'GET',
26 beforeSend: Utils.addAuthorizationStub,
27 success: function(data, textStatus, jqXHR) {
28 resolve(data.user);
29 }
30 }).fail(function(xhr){
31 //Authentication and the handling of fail states should be wrapped up into a connection class.
32 Utils.checkAuthentication(xhr.status);
33 let msg = xhr.responseText;
34 if(xhr.errorMessage) {
35 msg = xhr.errorMessage
36 }
37 reject(msg);
38 });
39 });
40 },
41 interceptResponse: interceptResponse({
42 'error': 'There was an error retrieving the resource orchestrator information.'
43 }),
44 success: Alt.actions.global.getUsersSuccess,
45 loading: Alt.actions.global.showScreenLoader,
46 error: Alt.actions.global.handleServerReportedError
47 },
48 getProjects: {
49 remote: function() {
50 return new Promise(function(resolve, reject) {
51 $.ajax({
52 url: `/project?api_server=${API_SERVER}`,
53 type: 'GET',
54 beforeSend: Utils.addAuthorizationStub,
55 success: function(data, textStatus, jqXHR) {
56 resolve(data.project);
57 }
58 }).fail(function(xhr){
59 //Authentication and the handling of fail states should be wrapped up into a connection class.
60 Utils.checkAuthentication(xhr.status);
61 let msg = xhr.responseText;
62 if(xhr.errorMessage) {
63 msg = xhr.errorMessage
64 }
65 reject(msg);
66 });
67 });
68 },
69 interceptResponse: interceptResponse({
70 'error': 'There was an error retrieving the resource orchestrator information.'
71 }),
72 success: Alt.actions.global.getProjectsSuccess,
73 loading: Alt.actions.global.showScreenLoader,
74 error: Alt.actions.global.handleServerReportedError
75 },
76 updateProject: {
77 remote: function(state, project) {
78 return new Promise(function(resolve, reject) {
79 $.ajax({
80 url: `/project?api_server=${API_SERVER}`,
81 type: 'PUT',
82 data: project,
83 beforeSend: Utils.addAuthorizationStub,
84 success: function(data, textStatus, jqXHR) {
85 resolve(data);
86 }
87 }).fail(function(xhr){
88 //Authentication and the handling of fail states should be wrapped up into a connection class.
89 Utils.checkAuthentication(xhr.status);
90 let msg = xhr.responseText;
91 if(xhr.errorMessage) {
92 msg = xhr.errorMessage
93 }
94 reject(msg);
95 });
96 });
97 },
98 interceptResponse: interceptResponse({
99 'error': 'There was an error updating the project.'
100 }),
101 success: Alt.actions.global.updateProjectSuccess,
102 loading: Alt.actions.global.showScreenLoader,
103 error: Alt.actions.global.handleServerReportedError
104 },
105 deleteProject: {
106 remote: function(state, project) {
107 return new Promise(function(resolve, reject) {
108 $.ajax({
109 url: `/project/${encodeURIComponent(project['name'])}?api_server=${API_SERVER}`,
110 type: 'DELETE',
111 beforeSend: Utils.addAuthorizationStub,
112 success: function(data, textStatus, jqXHR) {
113 resolve(data);
114 }
115 }).fail(function(xhr){
116 //Authentication and the handling of fail states should be wrapped up into a connection class.
117 Utils.checkAuthentication(xhr.status);
118 let msg = xhr.responseText;
119 if(xhr.errorMessage) {
120 msg = xhr.errorMessage
121 }
122 reject(msg);
123 });
124 });
125 },
126 interceptResponse: interceptResponse({
127 'error': 'There was an error deleting the user.'
128 }),
129 success: Alt.actions.global.deleteProjectSuccess,
130 loading: Alt.actions.global.showScreenLoader,
131 error: Alt.actions.global.handleServerReportedError
132 },
133 createProject: {
134 remote: function(state, project) {
135
136 return new Promise(function(resolve, reject) {
137 $.ajax({
138 url: `/project?api_server=${API_SERVER}`,
139 type: 'POST',
140 data: project,
141 beforeSend: Utils.addAuthorizationStub,
142 success: function(data, textStatus, jqXHR) {
143 resolve(data);
144 }
145 }).fail(function(xhr){
146 //Authentication and the handling of fail states should be wrapped up into a connection class.
147 Utils.checkAuthentication(xhr.status);
148 let msg = xhr.responseText;
149 if(xhr.errorMessage) {
150 msg = xhr.errorMessage
151 }
152 reject(msg);
153 });
154 });
155 },
156 interceptResponse: interceptResponse({
157 'error': 'There was an error updating the account.'
158 }),
159 success: Alt.actions.global.createProjectSuccess,
160 loading: Alt.actions.global.showScreenLoader,
161 error: Alt.actions.global.handleServerReportedError
162 }
163 }
164 }
165
166 function interceptResponse (responses) {
167 return function(data, action, args) {
168 if(responses.hasOwnProperty(data)) {
169 return {
170 type: data,
171 msg: responses[data]
172 }
173 } else {
174 return data;
175 }
176 }
177 }
178