blob: 94eef113b15bef8694cfc862326f8eb3405f9fef [file] [log] [blame]
svishnev091edfd2018-03-19 12:15:19 +02001/*
2 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
svishnev091edfd2018-03-19 12:15:19 +02007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
svishnev091edfd2018-03-19 12:15:19 +020012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020016import { connect } from 'react-redux';
Michael Landoefa037d2017-02-19 12:57:33 +020017import LicenseModelCreationActionHelper from './LicenseModelCreationActionHelper.js';
18import LicenseModelCreationView from './LicenseModelCreationView.jsx';
svishnevea5e43c2018-04-15 09:06:57 +030019
AviZi280f8012017-06-09 02:39:56 +030020import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
21import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
talig8e9c0652017-12-20 14:30:43 +020022import VersionsPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023import { itemTypes as versionItemTypes } from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
talig8e9c0652017-12-20 14:30:43 +020024import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025import { enums, screenTypes } from 'sdc-app/onboarding/OnboardingConstants.js';
talig8e9c0652017-12-20 14:30:43 +020026import PermissionsActionHelper from 'sdc-app/onboarding/permissions/PermissionsActionHelper.js';
svishnevea5e43c2018-04-15 09:06:57 +030027import UniqueTypesHelper from 'sdc-app/common/helpers/UniqueTypesHelper.js';
28import i18n from 'nfvo-utils/i18n/i18n.js';
29import { itemType } from 'sdc-app/common/helpers/ItemsHelperConstants.js';
30
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031export const mapStateToProps = ({
32 users: { usersList },
33 licenseModelList,
34 finalizedLicenseModelList,
35 archivedLicenseModelList,
36 licenseModel: { licenseModelCreation }
37}) => {
38 let { genericFieldInfo } = licenseModelCreation;
39 let isFormValid = ValidationHelper.checkFormValid(genericFieldInfo);
40 let VLMNames = {};
svishnev091edfd2018-03-19 12:15:19 +020041
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020042 const allVlmList = [
43 ...licenseModelList,
44 ...finalizedLicenseModelList,
45 ...archivedLicenseModelList
46 ];
47 allVlmList.map(item => {
48 VLMNames[item.name.toLowerCase()] = item.id;
49 });
AviZi280f8012017-06-09 02:39:56 +030050
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020051 return {
52 ...licenseModelCreation,
53 isFormValid: isFormValid,
54 VLMNames,
55 usersList
56 };
AviZi280f8012017-06-09 02:39:56 +030057};
58
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020059export const mapActionsToProps = dispatch => {
60 return {
61 onDataChanged: (deltaData, formName, customValidations) =>
62 ValidationHelper.dataChanged(dispatch, {
63 deltaData,
64 formName,
65 customValidations
66 }),
67 onCancel: () => LicenseModelCreationActionHelper.close(dispatch),
68 onSubmit: (licenseModel, usersList) => {
69 LicenseModelCreationActionHelper.close(dispatch);
70 LicenseModelCreationActionHelper.createLicenseModel(dispatch, {
71 licenseModel
72 }).then(response => {
73 let { itemId, version } = response;
74 LicenseModelActionHelper.fetchLicenseModels(dispatch).then(() =>
75 PermissionsActionHelper.fetchItemUsers(dispatch, {
76 itemId,
77 allUsers: usersList
78 }).then(() =>
79 VersionsPageActionHelper.fetchVersions(dispatch, {
80 itemType: versionItemTypes.LICENSE_MODEL,
81 itemId
82 }).then(() =>
83 ScreensHelper.loadScreen(dispatch, {
84 screen: enums.SCREEN.LICENSE_MODEL_OVERVIEW,
85 screenType: screenTypes.LICENSE_MODEL,
86 props: { licenseModelId: itemId, version }
87 })
88 )
89 )
90 );
91 });
92 },
93 onValidateForm: formName =>
svishnevea5e43c2018-04-15 09:06:57 +030094 ValidationHelper.validateForm(dispatch, formName),
95 isNameUnique: (value, name, formName) =>
96 UniqueTypesHelper.isNameUnique(dispatch, {
97 value,
98 name,
99 formName,
100 errorText: `${i18n(
101 'License model by the name'
102 )} ${value} ${i18n('already exists')}. ${i18n(
103 'License model name must be unique'
104 )}`,
105 itemType: itemType.VLM
106 })
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200107 };
Michael Landoefa037d2017-02-19 12:57:33 +0200108};
109
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200110export default connect(mapStateToProps, mapActionsToProps)(
svishnev8ae8f7e2018-07-02 14:05:17 +0300111 LicenseModelCreationView
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200112);