ce0c984fe29f98d865a11661a36394b8efdcefd2
[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.users);
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 });
34 });
35 },
36 interceptResponse: interceptResponse({
37 'error': 'There was an error retrieving the resource orchestrator information.'
38 }),
39 success: Alt.actions.global.getUsersSuccess,
40 loading: Alt.actions.global.showScreenLoader,
41 error: Alt.actions.global.showNotification
42 },
43 getProjects: {
44 remote: function() {
45 return new Promise(function(resolve, reject) {
46 $.ajax({
47 url: `/project?api_server=${API_SERVER}`,
48 type: 'GET',
49 beforeSend: Utils.addAuthorizationStub,
50 success: function(data, textStatus, jqXHR) {
51 resolve(data.project);
52 }
53 }).fail(function(xhr){
54 //Authentication and the handling of fail states should be wrapped up into a connection class.
55 Utils.checkAuthentication(xhr.status);
56 });
57 });
58 },
59 interceptResponse: interceptResponse({
60 'error': 'There was an error retrieving the resource orchestrator information.'
61 }),
62 success: Alt.actions.global.getProjectsSuccess,
63 loading: Alt.actions.global.showScreenLoader,
64 error: Alt.actions.global.showNotification
65 },
66 updateProject: {
67 remote: function(state, project) {
68 return new Promise(function(resolve, reject) {
69 $.ajax({
70 url: `/project?api_server=${API_SERVER}`,
71 type: 'PUT',
72 data: project,
73 beforeSend: Utils.addAuthorizationStub,
74 success: function(data, textStatus, jqXHR) {
75 resolve(data);
76 }
77 }).fail(function(xhr){
78 //Authentication and the handling of fail states should be wrapped up into a connection class.
79 Utils.checkAuthentication(xhr.status);
80 });
81 });
82 },
83 interceptResponse: interceptResponse({
84 'error': 'There was an error updating the project.'
85 }),
86 success: Alt.actions.global.updateProjectSuccess,
87 loading: Alt.actions.global.showScreenLoader,
88 error: Alt.actions.global.showNotification
89 },
90 deleteProject: {
91 remote: function(state, project) {
92 return new Promise(function(resolve, reject) {
93 $.ajax({
94 url: `/project/${project['name']}?api_server=${API_SERVER}`,
95 type: 'DELETE',
96 beforeSend: Utils.addAuthorizationStub,
97 success: function(data, textStatus, jqXHR) {
98 resolve(data);
99 }
100 }).fail(function(xhr){
101 //Authentication and the handling of fail states should be wrapped up into a connection class.
102 Utils.checkAuthentication(xhr.status);
103 });
104 });
105 },
106 interceptResponse: interceptResponse({
107 'error': 'There was an error deleting the user.'
108 }),
109 success: Alt.actions.global.deleteProjectSuccess,
110 loading: Alt.actions.global.showScreenLoader,
111 error: Alt.actions.global.showNotification
112 },
113 createProject: {
114 remote: function(state, project) {
115
116 return new Promise(function(resolve, reject) {
117 $.ajax({
118 url: `/project?api_server=${API_SERVER}`,
119 type: 'POST',
120 data: project,
121 beforeSend: Utils.addAuthorizationStub,
122 success: function(data, textStatus, jqXHR) {
123 resolve(data);
124 }
125 }).fail(function(xhr){
126 //Authentication and the handling of fail states should be wrapped up into a connection class.
127 Utils.checkAuthentication(xhr.status);
128 });
129 });
130 },
131 interceptResponse: interceptResponse({
132 'error': 'There was an error updating the account.'
133 }),
134 success: Alt.actions.global.createProjectSuccess,
135 loading: Alt.actions.global.showScreenLoader,
136 error: Alt.actions.global.showNotification
137 }
138 }
139 }
140
141 function interceptResponse (responses) {
142 return function(data, action, args) {
143 if(responses.hasOwnProperty(data)) {
144 return {
145 type: data,
146 msg: responses[data]
147 }
148 } else {
149 return data;
150 }
151 }
152 }
153