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 'lodash'
22 import $ from 'jquery'
23 import alt
from '../alt'
24 import utils
from '../libraries/utils'
25 import serializers
from '../libraries/model/DescriptorModelSerializer'
26 import CatalogDataSourceActions
from '../actions/CatalogDataSourceActions'
27 let Utils
= require('utils/utils.js');
28 const CatalogDataSource
= {
30 loadCatalogs: function() {
33 return new Promise(function(resolve
, reject
) {
35 beforeSend
: Utils
.addAuthorizationStub
,
36 url
: 'api/catalog?api_server=' + utils
.getSearchParams(window
.location
).api_server
,
37 // url: '/composer/big-honking-catalog',
38 // url: '/composer/ping-pong-catalog',
39 // url: '/composer/empty-nsd-catalog',
40 success: function(data
) {
41 if (typeof data
== 'string') {
42 data
= JSON
.parse(data
);
44 const context
= Object
.assign({}, this, {data
: data
});
47 error: function(error
) {
48 if (typeof error
== 'string') {
49 error
= JSON
.parse(error
);
53 }).fail(function(xhr
){
54 //Authentication and the handling of fail states should be wrapped up into a connection class.
55 Utils
.checkAuthentication(xhr
.status
);
59 success
: CatalogDataSourceActions
.loadCatalogsSuccess
,
60 error
: CatalogDataSourceActions
.loadCatalogsError
64 deleteCatalogItem: function() {
66 remote: function(state
, catalogType
, itemId
) {
67 return new Promise(function(resolve
, reject
) {
69 url
: 'api/catalog/' + catalogType
+ '/' + itemId
+ '?api_server=' + utils
.getSearchParams(window
.location
).api_server
,
71 beforeSend
: utils
.addAuthorizationStub
,
72 success: function(data
) {
75 catalogType
: catalogType
,
79 error: function(error
) {
82 catalogType
: catalogType
,
89 success
: CatalogDataSourceActions
.deleteCatalogItemSuccess
,
90 error
: CatalogDataSourceActions
.deleteCatalogItemError
94 saveCatalogItem: function () {
96 remote: function (state
, item
= {}) {
97 const method
= item
.uiState
.isNew
? 'POST' : 'PUT';
98 const payload
= serializers
.serialize(item
);
99 return new Promise((resolve
, reject
) => {
100 if (payload
=== false) {
102 error
: 'unable to serialize item: ' + item
.id
,
103 catalogType
: item
.uiState
.type
,
107 if (method
=== 'POST') {
109 url
: 'api/catalog/' + item
.uiState
.type
+ '?api_server=' + utils
.getSearchParams(window
.location
).api_server
,
111 beforeSend
: utils
.addAuthorizationStub
,
114 success: function(data
) {
117 catalogType
: item
.uiState
.type
,
121 error: function(error
) {
124 catalogType
: item
.uiState
.type
,
131 url
: 'api/catalog/' + item
.uiState
.type
+ '/' + payload
.id
+ '?api_server=' + utils
.getSearchParams(window
.location
).api_server
,
133 beforeSend
: utils
.addAuthorizationStub
,
136 success: function(data
) {
139 catalogType
: item
.uiState
.type
,
143 error: function(error
) {
146 catalogType
: item
.uiState
.type
,
154 success
: CatalogDataSourceActions
.saveCatalogItemSuccess
,
155 error
: CatalogDataSourceActions
.saveCatalogItemError
160 export default CatalogDataSource
;