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 | |
| 17 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 18 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 19 | import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 20 | import { actionsEnum as vcActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 21 | |
| 22 | import Configuration from 'sdc-app/config/Configuration.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 23 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 24 | import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 25 | import { enums, screenTypes } from 'sdc-app/onboarding/OnboardingConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 26 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 27 | import { actionTypes } from './RevisionsConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 28 | |
| 29 | function baseUrl(itemId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 30 | const restPrefix = Configuration.get('restPrefix'); |
| 31 | return `${restPrefix}/v1.0/items/${itemId}/versions/${version.id}`; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 34 | function fetchRevisions(itemId, version) { |
| 35 | let fetchUrl = `${baseUrl(itemId, version)}/revisions`; |
| 36 | return RestAPIUtil.fetch(fetchUrl); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | function revertToRevision(itemId, version, revisionId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 40 | let putUrl = `${baseUrl(itemId, version)}/actions`; |
| 41 | let requestBody = { |
| 42 | action: vcActionsEnum.REVERT, |
| 43 | revisionRequest: { |
| 44 | revisionId: revisionId |
| 45 | } |
| 46 | }; |
| 47 | return RestAPIUtil.put(putUrl, requestBody); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | const RevisionaActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 51 | openRevisionsView(dispatch, { itemId, version, itemType }) { |
| 52 | this.fetchRevisions(dispatch, { itemId, version }).then(() => { |
| 53 | dispatch({ |
| 54 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 55 | data: { |
| 56 | modalComponentName: modalContentMapper.REVISIONS_LIST, |
| 57 | modalClassName: 'manage-revisions-modal', |
| 58 | title: i18n('Revert'), |
| 59 | modalComponentProps: { |
| 60 | itemId: itemId, |
| 61 | version: version, |
| 62 | itemType |
| 63 | } |
| 64 | } |
| 65 | }); |
| 66 | }); |
| 67 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 68 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 69 | closeRevisionsView(dispatch) { |
| 70 | dispatch({ |
| 71 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 72 | }); |
| 73 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 74 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 75 | fetchRevisions(dispatch, { itemId, version }) { |
| 76 | return fetchRevisions(itemId, version).then(response => { |
| 77 | dispatch({ |
| 78 | type: actionTypes.ITEM_REVISIONS_LOADED, |
| 79 | response: response |
| 80 | }); |
| 81 | }); |
| 82 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 83 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 84 | revertToRevision(dispatch, { itemId, version, revisionId, itemType }) { |
| 85 | return revertToRevision(itemId, version, revisionId).then(() => { |
| 86 | this.closeRevisionsView(dispatch); |
| 87 | if (itemType === screenTypes.LICENSE_MODEL) { |
| 88 | ScreensHelper.loadScreen(dispatch, { |
| 89 | screen: enums.SCREEN.LICENSE_MODEL_OVERVIEW, |
| 90 | screenType: screenTypes.LICENSE_MODEL, |
| 91 | props: { licenseModelId: itemId, version } |
| 92 | }); |
| 93 | } else { |
| 94 | ScreensHelper.loadScreen(dispatch, { |
| 95 | screen: enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE, |
| 96 | screenType: screenTypes.SOFTWARE_PRODUCT, |
| 97 | props: { softwareProductId: itemId, version } |
| 98 | }); |
| 99 | } |
| 100 | }); |
| 101 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | export default RevisionaActionHelper; |