Skip to content

Commit

Permalink
wzApiTable optimization + obsolete redux cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
asteriscos committed Jun 28, 2022
1 parent c1856de commit e5e7d11
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 280 deletions.
21 changes: 18 additions & 3 deletions public/components/common/tables/table-default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Find more information about this on the LICENSE file.
*/

import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useRef } from 'react';
import { EuiBasicTable } from '@elastic/eui';
import { UI_ERROR_SEVERITIES } from '../../../react-services/error-orchestrator/types';
import { UI_LOGGER_LEVELS } from '../../../../common/constants';
Expand Down Expand Up @@ -43,6 +43,8 @@ export function TableDefault({
}
});

const isMounted = useRef(false);

function tableOnChange({ page = {}, sort = {} }){
const { index: pageIndex, size: pageSize } = page;
const { field, direction } = sort;
Expand All @@ -59,9 +61,14 @@ export function TableDefault({
};

useEffect(() => {
// Reset the page index when the endpoint changes.
// This will cause that onSearch function is triggered because to changes in pagination in the another effect.
// This effect is triggered when the component is mounted because of how to the useEffect hook works.
// We don't want to set the pagination state because there is another effect that has this dependency
// and will cause the effect is triggered (redoing the onSearch function).
if (isMounted.current) {
// Reset the page index when the endpoint changes.
// This will cause that onSearch function is triggered because to changes in pagination in the another effect.
setPagination({pageIndex: 0, pageSize: pagination.pageSize});
}
}, [endpoint]);

useEffect(() => {
Expand Down Expand Up @@ -90,6 +97,14 @@ export function TableDefault({
})()
}, [endpoint, pagination, sorting, reload]);


// It is required that this effect runs after other effects that use isMounted
// to avoid that these effects run when the component is mounted, only running
// when one of its dependencies changes.
useEffect(() => {
isMounted.current = true;
}, []);

const tablePagination = {
...pagination,
totalItemCount: totalItems,
Expand Down
129 changes: 61 additions & 68 deletions public/components/common/tables/table-with-search-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Find more information about this on the LICENSE file.
*/

import React, { useState, useEffect, useCallback, useLayoutEffect } from 'react';
import React, { useState, useEffect, useRef } from 'react';
import { EuiBasicTable, EuiSpacer } from '@elastic/eui';
import _ from 'lodash';
import { WzSearchBar } from '../../wz-search-bar/';
Expand Down Expand Up @@ -41,12 +41,6 @@ export function TableWithSearchBar({
pageIndex: 0,
pageSize: tablePageSizeOptions[0],
});
let timeoutId = 0;

function abortRequest(){
clearTimeout(timeoutId);
timeoutId = 0;
}

const [sorting, setSorting] = useState({
sort: {
Expand All @@ -55,79 +49,78 @@ export function TableWithSearchBar({
},
});

function tableOnChange({ page = {}, sort = {} }) {
const { index: pageIndex, size: pageSize } = page;
const { field, direction } = sort;
setPagination({
pageIndex,
pageSize,
});
setSorting({
sort: {
field,
direction,
},
});
}

useEffect(() => {
// Reset the page index when the endpoint changes.
// This will cause that onSearch function is triggered because to changes in pagination in the another effect.

setPagination({pageIndex: 0, pageSize: pagination.pageSize});
}, [endpoint, reload]);
const isMounted = useRef(false);


const loadRecords = useCallback(async function () {
console.log('onSearch')
try {
setLoading(true);
const { items, totalItems } = await onSearch(endpoint, filters, pagination, sorting);
setItems(items);
setTotalItems(totalItems);
} catch (error) {
setItems([]);
setTotalItems(0);
const options = {
context: `${TableWithSearchBar.name}.useEffect`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
error: {
error: error,
message: error.message || error,
title: `${error.name}: Error fetching items`,
function tableOnChange({ page = {}, sort = {} }) {
if (isMounted.current) {
const { index: pageIndex, size: pageSize } = page;
const { field, direction } = sort;
setPagination({
pageIndex,
pageSize,
});
setSorting({
sort: {
field,
direction,
},
};
getErrorOrchestrator().handleError(options);
});
}
setLoading(false);
}, [filters, pagination, sorting])
}

useEffect(() => {
if (!timeoutId)
timeoutId = setTimeout((() => {
loadRecords()
timeoutId = 0;
}), 100);
}, [loadRecords]);
// This effect is triggered when the component is mounted because of how to the useEffect hook works.
// We don't want to set the pagination state because there is another effect that has this dependency
// and will cause the effect is triggered (redoing the onSearch function).
if (isMounted.current) {
// Reset the page index when the endpoint changes.
// This will cause that onSearch function is triggered because to changes in pagination in the another effect.
setPagination({ pageIndex: 0, pageSize: pagination.pageSize });
}
}, [endpoint, reload]);

useEffect(() => {
console.log('filters', filters)
}, [filters])
useEffect(() => {
console.log('pagination', pagination)
}, [pagination])
useEffect(() => {
console.log('sorting', sorting)
}, [sorting])
useEffect(function () {
(async () => {
try {
setLoading(true);
const { items, totalItems } = await onSearch(endpoint, filters, pagination, sorting);
setItems(items);
setTotalItems(totalItems);
} catch (error) {
setItems([]);
setTotalItems(0);
const options = {
context: `${TableWithSearchBar.name}.useEffect`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
error: {
error: error,
message: error.message || error,
title: `${error.name}: Error fetching items`,
},
};
getErrorOrchestrator().handleError(options);
}
setLoading(false);
})();
}, [filters, pagination, sorting]);

useEffect(() => {
if (!_.isEqual(rest.filters, filters)){
console.log('rest.filters', rest.filters)
// This effect is triggered when the component is mounted because of how to the useEffect hook works.
// We don't want to set the filters state because there is another effect that has this dependency
// and will cause the effect is triggered (redoing the onSearch function).
if (isMounted.current && !_.isEqual(rest.filters, filters)) {
setFilters(rest.filters || []);
}
}, [rest.filters]);

// It is required that this effect runs after other effects that use isMounted
// to avoid that these effects run when the component is mounted, only running
// when one of its dependencies changes.
useEffect(() => {
isMounted.current = true;
}, []);

const tablePagination = {
...pagination,
totalItemCount: totalItems,
Expand Down
1 change: 0 additions & 1 deletion public/components/common/welcome/components/menu-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ class WzMenuAgent extends Component {

const mapStateToProps = (state) => {
return {
state: state.rulesetReducers,
currentAgentData: state.appStateReducers.currentAgentData,
currentTab: state.appStateReducers.currentTab,
};
Expand Down
1 change: 0 additions & 1 deletion public/components/wz-menu/wz-menu-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ class WzMenuAgent extends Component {

const mapStateToProps = state => {
return {
state: state.rulesetReducers,
currentAgentData: state.appStateReducers.currentAgentData,
currentTab: state.appStateReducers.currentTab
};
Expand Down
1 change: 0 additions & 1 deletion public/components/wz-menu/wz-menu-overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ class WzMenuOverview extends Component {

const mapStateToProps = state => {
return {
state: state.rulesetReducers,
currentAgentData: state.appStateReducers.currentAgentData,
currentTab: state.appStateReducers.currentTab
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,6 @@ class WzListEditor extends Component {
}
}

const mapStateToProps = (state) => {
return {
state: state.rulesetReducers,
};
};

const mapDispatchToProps = (dispatch) => {
return {
Expand All @@ -645,4 +640,4 @@ const mapDispatchToProps = (dispatch) => {
};
};

export default connect(mapStateToProps, mapDispatchToProps)(WzListEditor);
export default connect(mapDispatchToProps)(WzListEditor);

This comment has been minimized.

Copy link
@Desvelao

Desvelao Jun 29, 2022

Member

nitpick: I think you have to pass the mapStateToProps argument. Use:

export default connect(null,mapDispatchToProps)(WzListEditor);
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,14 @@ class WzFileEditor extends Component {

const mapStateToProps = state => {
return {
state: state.rulesetReducers,
wazuhNotReadyYet: state.appStateReducers.wazuhNotReadyYet,
showFlyout: state.appStateReducers.showFlyoutLogtest,
};
};

const mapDispatchToProps = dispatch => {
return {
// cleanInfo: () => dispatch(cleanInfo()),
// updateFileContent: content => dispatch(updateFileContent(content)),
updateWazuhNotReadyYet: wazuhNotReadyYet => dispatch(updateWazuhNotReadyYet(wazuhNotReadyYet)),
// showFlyoutLogtest: showFlyout => dispatch(showFlyoutLogtest(showFlyout)),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,7 @@ function WzRulesetOverview(props) {

}

const mapStateToProps = state => {
return {
state: state.rulesetReducers
};
};

export default compose(
connect(
mapStateToProps
),
withGlobalBreadcrumb(props => {
return [
{ text: '' },
Expand Down
Loading

0 comments on commit e5e7d11

Please sign in to comment.