talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | * or implied. See the License for the specific language governing |
| 14 | * permissions and limitations under the License. |
| 15 | */ |
| 16 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
| 17 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 18 | import {actionTypes} from './VersionsPageCreationConstants.js'; |
| 19 | import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 20 | import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 21 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 22 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
| 23 | import {actionTypes as VersionsPageActionTypes} from '../VersionsPageConstants.js'; |
| 24 | |
| 25 | function baseUrl({itemId, baseVersion}) { |
| 26 | const restPrefix = Configuration.get('restPrefix'); |
| 27 | return `${restPrefix}/v1.0/items/${itemId}/versions/${baseVersion.id}/`; |
| 28 | } |
| 29 | |
| 30 | function createVersion({itemId, baseVersion, payload: {description, creationMethod} }) { |
| 31 | return RestAPIUtil.post(baseUrl({itemId, baseVersion}), {description, creationMethod}); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | export default { |
| 36 | |
| 37 | open(dispatch, {itemType, itemId, additionalProps, baseVersion}) { |
| 38 | dispatch({ |
| 39 | type: actionTypes.OPEN |
| 40 | }); |
| 41 | |
| 42 | dispatch({ |
| 43 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 44 | data: { |
| 45 | modalComponentName: modalContentMapper.VERSION_CREATION, |
| 46 | modalComponentProps: {itemType, itemId, additionalProps, baseVersion}, |
| 47 | title: i18n('New Version - From {name}', {name: baseVersion.name}) |
| 48 | } |
| 49 | }); |
| 50 | }, |
| 51 | |
| 52 | close(dispatch){ |
| 53 | dispatch({ |
| 54 | type: actionTypes.CLOSE |
| 55 | }); |
| 56 | |
| 57 | dispatch({ |
| 58 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 59 | }); |
| 60 | }, |
| 61 | |
| 62 | createVersion(dispatch, {itemId, baseVersion, payload}){ |
| 63 | return createVersion({itemId, baseVersion, payload}).then(result => { |
| 64 | return ItemsHelper.fetchVersions({itemId}).then(response => { |
| 65 | dispatch({ |
| 66 | type: VersionsPageActionTypes.VERSIONS_LOADED, |
| 67 | versions: response.results, |
| 68 | itemId |
| 69 | }); |
| 70 | dispatch({ |
| 71 | type: actionTypes.VERSION_CREATED, |
| 72 | result |
| 73 | }); |
| 74 | return result; |
| 75 | }); |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | }; |