4 * Copyright 2016 RIFT.IO Inc
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
= {
29 loadCatalogs: function() {
32 return new Promise(function(resolve
, reject
) {
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
);
43 const context
= Object
.assign({}, this, {data
: data
});
46 error: function(error
) {
47 if (typeof error
== 'string') {
48 error
= JSON
.parse(error
);
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
);
58 success
: CatalogDataSourceActions
.loadCatalogsSuccess
,
59 error
: CatalogDataSourceActions
.loadCatalogsError
63 deleteCatalogItem: function() {
65 remote: function(state
, catalogType
, itemId
) {
66 return new Promise(function(resolve
, reject
) {
68 url
: 'api/catalog/' + catalogType
+ '/' + itemId
+ '?api_server=' + utils
.getSearchParams(window
.location
).api_server
,
70 beforeSend
: utils
.addAuthorizationStub
,
71 success: function(data
) {
74 catalogType
: catalogType
,
78 error: function(error
) {
81 catalogType
: catalogType
,
88 success
: CatalogDataSourceActions
.deleteCatalogItemSuccess
,
89 error
: CatalogDataSourceActions
.deleteCatalogItemError
93 saveCatalogItem: function () {
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) {
101 error
: 'unable to serialize item: ' + item
.id
,
102 catalogType
: item
.uiState
.type
,
106 if (method
=== 'POST') {
108 url
: 'api/catalog/' + item
.uiState
.type
+ '?api_server=' + utils
.getSearchParams(window
.location
).api_server
,
110 beforeSend
: utils
.addAuthorizationStub
,
113 success: function(data
) {
116 catalogType
: item
.uiState
.type
,
120 error: function(error
) {
123 catalogType
: item
.uiState
.type
,
130 url
: 'api/catalog/' + item
.uiState
.type
+ '/' + payload
.id
+ '?api_server=' + utils
.getSearchParams(window
.location
).api_server
,
132 beforeSend
: utils
.addAuthorizationStub
,
135 success: function(data
) {
138 catalogType
: item
.uiState
.type
,
142 error: function(error
) {
145 catalogType
: item
.uiState
.type
,
153 success
: CatalogDataSourceActions
.saveCatalogItemSuccess
,
154 error
: CatalogDataSourceActions
.saveCatalogItemError
159 export default CatalogDataSource
;