267387a4dea0ad7dbf21a51d2e6aef326ef206bf
[osm/UI.git] / skyquake / plugins / user_management / src / userProfile / userProfileSource.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
18 module.exports = function(Alt) {
19 return {
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.showNotification
47 },
48 updateUser: {
49 remote: function(state, user) {
50 return new Promise(function(resolve, reject) {
51 $.ajax({
52 url: `/user?api_server=${API_SERVER}`,
53 type: 'PUT',
54 data: user,
55 beforeSend: Utils.addAuthorizationStub,
56 success: function(data, textStatus, jqXHR) {
57 resolve(data);
58 }
59 }).fail(function(xhr){
60 //Authentication and the handling of fail states should be wrapped up into a connection class.
61 Utils.checkAuthentication(xhr.status);
62 let msg = xhr.responseText;
63 if(xhr.errorMessage) {
64 msg = xhr.errorMessage
65 }
66 reject(msg);
67 });
68 });
69 },
70 interceptResponse: interceptResponse({
71 'error': 'There was an error updating the user.'
72 }),
73 success: Alt.actions.global.updateUserSuccess,
74 loading: Alt.actions.global.showScreenLoader,
75 error: Alt.actions.global.showNotification
76 },
77 deleteUser: {
78 remote: function(state, user) {
79 return new Promise(function(resolve, reject) {
80 $.ajax({
81 url: `/user/${user['user-name']}/${user['user-domain']}?api_server=${API_SERVER}`,
82 type: 'DELETE',
83 data: user,
84 beforeSend: Utils.addAuthorizationStub,
85 success: function(data, textStatus, jqXHR) {
86 resolve(data);
87 }
88 }).fail(function(xhr){
89 //Authentication and the handling of fail states should be wrapped up into a connection class.
90 Utils.checkAuthentication(xhr.status);
91 let msg = xhr.responseText;
92 if(xhr.errorMessage) {
93 msg = xhr.errorMessage
94 }
95 reject(msg);
96 });
97 });
98 },
99 interceptResponse: interceptResponse({
100 'error': 'There was an error deleting the user.'
101 }),
102 success: Alt.actions.global.deleteUserSuccess,
103 loading: Alt.actions.global.showScreenLoader,
104 error: Alt.actions.global.showNotification
105 },
106 createUser: {
107 remote: function(state, user) {
108
109 return new Promise(function(resolve, reject) {
110 $.ajax({
111 url: `/user?api_server=${API_SERVER}`,
112 type: 'POST',
113 data: user,
114 beforeSend: Utils.addAuthorizationStub,
115 success: function(data, textStatus, jqXHR) {
116 resolve(data);
117 }
118 }).fail(function(xhr){
119 //Authentication and the handling of fail states should be wrapped up into a connection class.
120 Utils.checkAuthentication(xhr.status);
121 let msg = xhr.responseText;
122 if(xhr.errorMessage) {
123 msg = xhr.errorMessage
124 }
125 reject(msg);
126 });
127 });
128 },
129 interceptResponse: interceptResponse({
130 'error': 'There was an error updating the account.'
131 }),
132 success: Alt.actions.global.createUserSuccess,
133 loading: Alt.actions.global.showScreenLoader,
134 error: Alt.actions.global.showNotification
135 }
136 }
137 }
138
139 function interceptResponse (responses) {
140 return function(data, action, args) {
141 if(responses.hasOwnProperty(data)) {
142 return {
143 type: data,
144 msg: responses[data]
145 }
146 } else {
147 return data;
148 }
149 }
150 }
151