| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 RIFT.IO Inc |
| 4 | * |
| 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 |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 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. |
| 16 | * |
| 17 | */ |
| 18 | import rw from 'utils/rw' |
| 19 | import modelActions from './modelActions' |
| 20 | import Utils from 'utils/utils' |
| 21 | import $ from 'jquery'; |
| 22 | |
| 23 | const source = { |
| 24 | loadModel: function () { |
| 25 | return { |
| 26 | remote: function (state, path) { |
| 27 | return new Promise(function (resolve, reject) { |
| 28 | $.ajax({ |
| 29 | url: '/model?path=' + path, |
| 30 | type: 'GET', |
| 31 | success: function (result) { |
| 32 | resolve(result); |
| 33 | }, |
| 34 | error: function (xhr, errorType, errorMessage) { |
| 35 | console.log("There was an error updating the model: ", errorType, errorMessage, xhr); |
| 36 | reject({response: xhr.responseJSON, errorType, errorMessage}); |
| 37 | } |
| 38 | }); |
| 39 | }) |
| 40 | }, |
| 41 | success: modelActions.processRequestSuccess, |
| 42 | loading: modelActions.processRequestInitiated, |
| 43 | error: modelActions.processRequestFailure |
| 44 | } |
| 45 | }, |
| 46 | updateModel: function () { |
| 47 | return { |
| 48 | remote: function (state, path, data) { |
| 49 | const url = path.reduce((url, node) => { |
| 50 | url += node[0] !== '[' ? '/' : ''; |
| 51 | return url + node |
| 52 | }, `/model?path=/${state.path}`); |
| 53 | return new Promise(function (resolve, reject) { |
| 54 | $.ajax({ |
| 55 | url: url, |
| 56 | type: 'PATCH', |
| 57 | data: data, |
| 58 | success: function (result) { |
| 59 | resolve(result); |
| 60 | }, |
| 61 | error: function (xhr, errorType, errorMessage) { |
| 62 | console.log("There was an error updating the model: ", errorType, errorMessage, xhr); |
| 63 | reject({response: xhr.responseJSON, errorType, errorMessage}); |
| 64 | } |
| 65 | }); |
| 66 | }) |
| 67 | }, |
| 68 | success: modelActions.processRequestSuccess, |
| 69 | loading: modelActions.processRequestInitiated, |
| 70 | error: modelActions.processRequestFailure |
| 71 | } |
| 72 | }, |
| 73 | createModel: function () { |
| 74 | return { |
| 75 | remote: function (state, path, data) { |
| 76 | const url = path.reduce((url, node) => { |
| 77 | url += node[0] !== '[' ? '/' : ''; |
| 78 | return url + node |
| 79 | }, `/model?path=/${state.path}`); |
| 80 | return new Promise(function (resolve, reject) { |
| 81 | $.ajax({ |
| 82 | url: url, |
| 83 | type: 'PUT', |
| 84 | data: data, |
| 85 | success: function (result) { |
| 86 | resolve(result); |
| 87 | }, |
| 88 | error: function (xhr, errorType, errorMessage) { |
| 89 | console.log("There was an error updating the model: ", errorType, errorMessage, xhr); |
| 90 | reject({response: xhr.responseJSON, errorType, errorMessage}); |
| 91 | } |
| 92 | }); |
| 93 | }) |
| 94 | }, |
| 95 | success: modelActions.processRequestSuccess, |
| 96 | loading: modelActions.processRequestInitiated, |
| 97 | error: modelActions.processRequestFailure |
| 98 | } |
| 99 | }, |
| 100 | |
| 101 | deleteModel: function () { |
| 102 | return { |
| 103 | remote: function (state, path) { |
| 104 | const url = path.reduce((url, node) => { |
| 105 | url += node[0] !== '[' ? '/' : ''; |
| 106 | return url + node |
| 107 | }, `/model?path=/${state.path}`); |
| 108 | return new Promise(function (resolve, reject) { |
| 109 | $.ajax({ |
| 110 | url: url, |
| 111 | type: 'DELETE', |
| 112 | success: function (result) { |
| 113 | resolve(result); |
| 114 | }, |
| 115 | error: function (xhr, errorType, errorMessage) { |
| 116 | console.log("There was an error updating the model: ", errorType, errorMessage, xhr); |
| 117 | reject({response: xhr.responseJSON, errorType, errorMessage}); |
| 118 | } |
| 119 | }); |
| 120 | }) |
| 121 | }, |
| 122 | success: modelActions.processRequestSuccess, |
| 123 | loading: modelActions.processRequestInitiated, |
| 124 | error: modelActions.processRequestFailure |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | module.exports = source; |