/
groups-main.js
119 lines (115 loc) · 4.01 KB
/
groups-main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Wazuh app - React component for groups.
* Copyright (C) 2015-2022 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import React, { Component } from 'react';
// Redux
import store from '../../../../../redux/store';
import WzReduxProvider from '../../../../../redux/wz-redux-provider';
//Wazuh groups overview
import WzGroupsOverview from './groups-overview';
import WzGroupDetail from './group-detail';
import WzGroupEditor from './groups-editor';
import { updateGroupDetail } from '../../../../../redux/actions/groupsActions';
import {
updateShowAddAgents,
resetGroup,
} from '../../../../../redux/actions/groupsActions';
import { connect } from 'react-redux';
import { WzRequest } from '../../../../../react-services/wz-request';
import { UI_LOGGER_LEVELS } from '../../../../../../common/constants';
import { UI_ERROR_SEVERITIES } from '../../../../../react-services/error-orchestrator/types';
import { getErrorOrchestrator } from '../../../../../react-services/common-services';
import { compose } from 'redux';
import { withGlobalBreadcrumb } from '../../../../../components/common/hocs';
import { endpointGroups } from '../../../../../utils/applications';
import { MultipleAgentSelector } from '../../../../../components/management/groups/multiple-agent-selector';
class WzGroups extends Component {
constructor(props) {
super(props);
this.state = {};
}
async componentDidMount() {
// Check if there is a group in the URL
const [_, group] =
window.location.href.match(new RegExp('group=' + '([^&]*)')) || [];
window.location.href = window.location.href.replace(
new RegExp('group=' + '[^&]*'),
'',
);
if (group) {
try {
// Try if the group can be accesed
const responseGroup = await WzRequest.apiReq('GET', '/groups', {
params: { groups_list: group },
});
const dataGroup = responseGroup?.data?.data?.affected_items?.[0];
this.props.updateGroupDetail(dataGroup);
} catch (error) {
const options = {
context: `${WzGroups.name}.componentDidMount`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.CRITICAL,
store: true,
error: {
error: error,
message: error.message || error,
title: `Error accessing the group`,
},
};
getErrorOrchestrator().handleError(options);
}
}
}
UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.state.showAddAgents) {
this.props.updateShowAddAgents(false);
}
}
componentWillUnmount() {
// When the component is going to be unmounted the groups state is reset
this.props.resetGroup();
}
render() {
const { itemDetail, showAddAgents, fileContent } = this.props.state;
return (
<WzReduxProvider>
{!showAddAgents &&
((itemDetail && !fileContent && <WzGroupDetail {...this.props} />) ||
(fileContent && <WzGroupEditor />) || <WzGroupsOverview />)}
{showAddAgents && itemDetail && (
<MultipleAgentSelector
currentGroup={itemDetail}
cancelButton={() => this.props.updateShowAddAgents(false)}
/>
)}
</WzReduxProvider>
);
}
}
const mapStateToProps = state => {
return {
state: state.groupsReducers,
};
};
const mapDispatchToProps = dispatch => {
return {
resetGroup: () => dispatch(resetGroup()),
updateShowAddAgents: showAddAgents =>
dispatch(updateShowAddAgents(showAddAgents)),
updateGroupDetail: groupDetail => dispatch(updateGroupDetail(groupDetail)),
};
};
export default compose(
connect(mapStateToProps, mapDispatchToProps),
withGlobalBreadcrumb(props => {
return [{ text: endpointGroups.breadcrumbLabel }];
}),
)(WzGroups);