update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[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 updateUser: {
21 remote: function(state, user) {
22 return new Promise(function(resolve, reject) {
23 $.ajax({
24 url: `/user?api_server=${API_SERVER}`,
25 type: 'PUT',
26 data: user,
27 beforeSend: Utils.addAuthorizationStub,
28 success: function(data, textStatus, jqXHR) {
29 resolve(data);
30 }
31 }).fail(function(xhr){
32 //Authentication and the handling of fail states should be wrapped up into a connection class.
33 Utils.checkAuthentication(xhr.status);
34 let msg = xhr.responseText;
35 if(xhr.errorMessage) {
36 msg = xhr.errorMessage
37 }
38 reject(msg);
39 });
40 });
41 },
42 interceptResponse: interceptResponse({
43 'error': 'There was an error updating the user.'
44 }),
45 success: Alt.actions.global.updateUserSuccess,
46 loading: Alt.actions.global.showScreenLoader,
47 error: Alt.actions.global.handleServerReportedError
48 },
49 createUser: {
50 remote: function(state, user) {
51
52 return new Promise(function(resolve, reject) {
53 $.ajax({
54 url: `/user?api_server=${API_SERVER}`,
55 type: 'POST',
56 data: user,
57 beforeSend: Utils.addAuthorizationStub,
58 success: function(data, textStatus, jqXHR) {
59 resolve(data);
60 }
61 }).fail(function(xhr){
62 //Authentication and the handling of fail states should be wrapped up into a connection class.
63 Utils.checkAuthentication(xhr.status);
64 let msg = xhr.responseText;
65 if(xhr.errorMessage) {
66 msg = xhr.errorMessage
67 }
68 reject(msg);
69 });
70 });
71 },
72 interceptResponse: interceptResponse({
73 'error': 'There was an error updating the account.'
74 }),
75 success: Alt.actions.global.createUserSuccess,
76 loading: Alt.actions.global.showScreenLoader,
77 error: Alt.actions.global.handleServerReportedError
78 }
79 }
80 }
81
82 function interceptResponse (responses) {
83 return function(data, action, args) {
84 if(responses.hasOwnProperty(data)) {
85 return {
86 type: data,
87 msg: responses[data]
88 }
89 } else {
90 return data;
91 }
92 }
93 }
94