RIFT-15726 - optimize download size -> lodash usage in UI
[osm/UI.git] / skyquake / plugins / composer / src / src / sources / CatalogDataSource.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 'use strict';
20
21 import $ from 'jquery'
22 import alt from '../alt'
23 import utils from '../libraries/utils'
24 import serializers from '../libraries/model/DescriptorModelSerializer'
25 import CatalogDataSourceActions from '../actions/CatalogDataSourceActions'
26 let Utils = require('utils/utils.js');
27 const CatalogDataSource = {
28
29 loadCatalogs: function() {
30 return {
31 remote: function() {
32 return new Promise(function(resolve, reject) {
33 $.ajax({
34 beforeSend: Utils.addAuthorizationStub,
35 url: 'api/catalog?api_server=' + utils.getSearchParams(window.location).api_server,
36 // url: '/composer/big-honking-catalog',
37 // url: '/composer/ping-pong-catalog',
38 // url: '/composer/empty-nsd-catalog',
39 success: function(data) {
40 if (typeof data == 'string') {
41 data = JSON.parse(data);
42 }
43 const context = Object.assign({}, this, {data: data});
44 resolve(context);
45 },
46 error: function(error) {
47 if (typeof error == 'string') {
48 error = JSON.parse(error);
49 }
50 reject(error);
51 }
52 }).fail(function(xhr){
53 //Authentication and the handling of fail states should be wrapped up into a connection class.
54 Utils.checkAuthentication(xhr.status);
55 });
56 });
57 },
58 success: CatalogDataSourceActions.loadCatalogsSuccess,
59 error: CatalogDataSourceActions.loadCatalogsError
60 }
61 },
62
63 deleteCatalogItem: function() {
64 return {
65 remote: function(state, catalogType, itemId) {
66 return new Promise(function(resolve, reject) {
67 $.ajax({
68 url: 'api/catalog/' + catalogType + '/' + itemId + '?api_server=' + utils.getSearchParams(window.location).api_server,
69 type: 'DELETE',
70 beforeSend: utils.addAuthorizationStub,
71 success: function(data) {
72 resolve({
73 data: data,
74 catalogType: catalogType,
75 itemId: itemId
76 });
77 },
78 error: function(error) {
79 reject({
80 error: error,
81 catalogType: catalogType,
82 itemId: itemId
83 });
84 }
85 });
86 });
87 },
88 success: CatalogDataSourceActions.deleteCatalogItemSuccess,
89 error: CatalogDataSourceActions.deleteCatalogItemError
90 }
91 },
92
93 saveCatalogItem: function () {
94 return {
95 remote: function (state, item = {}) {
96 const method = item.uiState.isNew ? 'POST' : 'PUT';
97 const payload = serializers.serialize(item);
98 return new Promise((resolve, reject) => {
99 if (payload === false) {
100 reject({
101 error: 'unable to serialize item: ' + item.id,
102 catalogType: item.uiState.type,
103 itemId: payload.id
104 });
105 }
106 if (method === 'POST') {
107 $.ajax({
108 url: 'api/catalog/' + item.uiState.type + '?api_server=' + utils.getSearchParams(window.location).api_server,
109 type: method,
110 beforeSend: utils.addAuthorizationStub,
111 data: payload,
112 dataType: 'json',
113 success: function(data) {
114 resolve({
115 data: data,
116 catalogType: item.uiState.type,
117 itemId: payload.id
118 });
119 },
120 error: function(error) {
121 reject({
122 error: error,
123 catalogType: item.uiState.type,
124 itemId: payload.id
125 });
126 }
127 });
128 } else {
129 $.ajax({
130 url: 'api/catalog/' + item.uiState.type + '/' + payload.id + '?api_server=' + utils.getSearchParams(window.location).api_server,
131 type: method,
132 beforeSend: utils.addAuthorizationStub,
133 data: payload,
134 dataType: 'json',
135 success: function(data) {
136 resolve({
137 data: data,
138 catalogType: item.uiState.type,
139 itemId: payload.id
140 });
141 },
142 error: function(error) {
143 reject({
144 error: error,
145 catalogType: item.uiState.type,
146 itemId: payload.id
147 });
148 }
149 });
150 }
151 });
152 },
153 success: CatalogDataSourceActions.saveCatalogItemSuccess,
154 error: CatalogDataSourceActions.saveCatalogItemError
155 };
156 }
157 };
158
159 export default CatalogDataSource;