Skip to content

Commit

Permalink
Add filter
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentD06 committed Jul 19, 2024
1 parent 33b8f2e commit 9ba1aa5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/web/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"wizard.title": "Titre",
"wizard.filterlists": "Filtrer les listes",
"wizard.filter": "Filtrer les règles d'alerte",
"wizard.noAlertFound": "Aucune règle d'alerte n'a été trouvé",
"wizard.cancel": "Annuler",
"wizard.save": "Sauvegarder",
"wizard.titleParameters": "Paramètres de la règle d'alerte",
Expand Down
64 changes: 45 additions & 19 deletions src/web/wizard/components/rules/AlertRulesContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import React from 'react';
import { useState, useCallback, useEffect } from 'react';
import { EntityDataTable, NoSearchResult, Timestamp, IfPermitted } from 'components/common';
import { EntityDataTable, NoSearchResult, Timestamp, IfPermitted, SearchForm } from 'components/common';
import { LinkContainer } from 'react-router-bootstrap';
import { Button } from 'components/bootstrap';
import AlertRuleActions from "../../actions/AlertRuleActions";
Expand Down Expand Up @@ -106,6 +106,8 @@ const AlertRulesContainer = ({ fieldOrder }) => {

const [alerts, setAlerts] = useState([]);
const [elements, setElements] = useState([]);
const [fullElements, setFullElements] = useState([]);
const [query, setQuery] = useState('');
const [visibleColumn, setVisibleColumn] = useState([...['title'], ...fieldOrder.filter(field => field.enabled).map((field) => field.name).map((fieldName) => fieldsTitle.find(x => x.config === fieldName).key)]);

const columnDefinitions= fieldsTitle.map(field => {return {id: field.key, title: field.label, sortable: false};});
Expand Down Expand Up @@ -143,10 +145,9 @@ const AlertRulesContainer = ({ fieldOrder }) => {
},
});
const onColumnsChange = useCallback((displayedAttributes) => {
console.log(displayedAttributes);
const newVisibleColumns = computeVisibleColumn(fieldOrder, fieldsTitle, displayedAttributes);
setVisibleColumn(newVisibleColumns);
}, []);
}, [setVisibleColumn]);
const renderBulkActions = (
selectedAlertRuleIds,
setSelectedAlertRuleIds,
Expand Down Expand Up @@ -175,12 +176,25 @@ const AlertRulesContainer = ({ fieldOrder }) => {
</div>);
}, []);

const onSearch = useCallback((newQuery, allElements = null) => {
// TODO filter on ['title', 'priority', 'created_at', 'last_modified', 'creator_user_id'];
const newElements = allElements ? allElements.filter((elt) => elt.title.includes(newQuery)) : fullElements.filter((elt) => elt.title.includes(newQuery));
setElements(newElements);
setQuery(newQuery);
}, [query, elements]);

const onReset = useCallback(() => {
onSearch('');
}, [onSearch]);

const _loadAlertRules = useCallback(() => {
AlertRuleActions.list().then(newAlerts => {
setAlerts(newAlerts);
setElements(newAlerts.map(_convertAlertToElement));
const allElements = newAlerts.map(_convertAlertToElement);
setFullElements(allElements);
onSearch(query, allElements);
});
}, [alerts, setAlerts, elements, setElements]);
}, [alerts, fullElements, onSearch]);

const deleteAlertRules = (alertRulesTitles) => {
const promises = alertRulesTitles.map(name => AlertRuleActions.deleteByName(name));
Expand Down Expand Up @@ -249,24 +263,36 @@ const AlertRulesContainer = ({ fieldOrder }) => {
}, []);

return (
<>
<div style={{marginBottom: 5}}>
<SearchForm onSearch={onSearch}
onReset={onReset}
placeholder={intl.formatMessage({
id: 'wizard.filter',
defaultMessage: 'Filter alert rules'
})}/>
</div>
<div>
{alerts?.length === 0 ? (
<NoSearchResult>No Alert Rule has been found</NoSearchResult>
<NoSearchResult>
<FormattedMessage id="wizard.noAlertFound" defaultMessage="No Alert Rule has been found" />
</NoSearchResult>
) : (
<EntityDataTable data={elements}
visibleColumns={visibleColumn}
columnsOrder={visibleColumn}
onColumnsChange={onColumnsChange}
onSortChange={onSortChange}
bulkActions={renderBulkActions}
columnDefinitions={columnDefinitions}
columnRenderers={columnRenderers()}
actionsCellWidth={500}
rowActions={renderAlertRuleActions}
entityAttributesAreCamelCase={false} />
)}
</div>
);
visibleColumns={visibleColumn}
columnsOrder={visibleColumn}
onColumnsChange={onColumnsChange}
onSortChange={onSortChange}
bulkActions={renderBulkActions}
columnDefinitions={columnDefinitions}
columnRenderers={columnRenderers()}
actionsCellWidth={500}
rowActions={renderAlertRuleActions}
entityAttributesAreCamelCase={false}/>
)}
</div>
</>
);
};

export default AlertRulesContainer;

0 comments on commit 9ba1aa5

Please sign in to comment.