diff --git a/bims/static/react/js/components/ContextFilterView.jsx b/bims/static/react/js/components/ContextFilterView.jsx index 0ad147201..f9da5e27f 100644 --- a/bims/static/react/js/components/ContextFilterView.jsx +++ b/bims/static/react/js/components/ContextFilterView.jsx @@ -3,7 +3,7 @@ import axios from "axios"; import SortableList, {SortableItem, SortableKnob} from "react-easy-sort"; import { arrayMoveImmutable } from "array-move"; import "bootstrap-icons/font/bootstrap-icons.css"; -import {Button, FormGroup, Input, Label, Modal, ModalBody, ModalFooter, ModalHeader} from "reactstrap"; +import {Button, Form, FormGroup, Input, Label, Modal, ModalBody, ModalFooter, ModalHeader} from "reactstrap"; import AddContextGroup from "./AddContextGroup"; @@ -17,6 +17,9 @@ const ContextFilterView = (props) => { const [isAddNewGroupModalOpen, setIsAddNewGroupModalOpen] = useState(false); const [selectedFilter, setSelectedFilter] = useState(null); const [contextGroups, setContextGroups] = useState([]); + const [isAddNewFilterModalOpen, setIsAddNewFilterModalOpen] = useState(false); + const [newFilterName, setNewFilterName] = useState(''); + const [savingNewFilter, setSavingNewFilter] = useState(false); const contextLayerFilterAPI = '/api/context-filter/'; const contextLayerGroupAPI = '/api/context-layer-group/'; @@ -43,6 +46,24 @@ const ContextFilterView = (props) => { } }; + const deleteContextFilter = async (filterId) => { + try { + const deleteUrl = `${contextLayerFilterAPI}?filter_id=${filterId}`; + await axios.delete(deleteUrl, { + headers: { + 'X-CSRFToken': props.csrfToken + } + }); + console.log(`Filter ${filterId} deleted successfully.`); + + // Optionally, refresh the context filters after deletion + setContextFilters([]); + fetchContextFilters(); + } catch (error) { + console.error(`Failed to delete filter ${filterId}:`, error); + } + }; + const fetchContextGroups = async () => { setContextGroups([]) try { @@ -79,6 +100,27 @@ const ContextFilterView = (props) => { } }; + const createContextFilter = async (newFilter) => { + try { + const response = await axios.post(contextLayerFilterAPI, newFilter, { + headers: { + 'X-CSRFToken': props.csrfToken, + 'Content-Type': 'application/json' + } + }); + console.log("New filter created successfully:", response.data.filter); + + setIsAddNewFilterModalOpen(false); + + setContextFilters([]); + fetchContextFilters(); + } catch (error) { + console.error("Failed to create new filter:", error); + } finally { + setSavingNewFilter(false); + } + }; + useEffect(() => { fetchContextFilters(); }, []); @@ -96,7 +138,7 @@ const ContextFilterView = (props) => { display_order: index + 1 })); - updateOrder(updatedFilters, []) + updateOrder(updatedFilters, {}) setContextFilters(arrayMoveImmutable(contextFilters, oldIndex, newIndex)) }; @@ -118,6 +160,16 @@ const ContextFilterView = (props) => { setFilterText(e.target.value); }; + const handleSaveAddNewFilter = () => { + const newFilter = { + display_order: contextFilters.length + 1, + location_context_groups: [], + title: newFilterName + } + setSavingNewFilter(true); + createContextFilter(newFilter); + } + const filteredContextFilters = contextFilters.filter(group => group.title.toLowerCase().includes(filterText.toLowerCase()) ); @@ -126,6 +178,13 @@ const ContextFilterView = (props) => { setIsAddNewGroupModalOpen(!isAddNewGroupModalOpen) } + const toggleAddNewFilterModal = () => { + if (!isAddNewFilterModalOpen) { + setNewFilterName(''); + } + setIsAddNewFilterModalOpen(!isAddNewFilterModalOpen) + } + const handleAddNewGroup = (e, contextFilter) => { e.stopPropagation(); toggleAddNewGropModal(); @@ -135,6 +194,11 @@ const ContextFilterView = (props) => { setSelectedFilter(contextFilter); } + const handleDeleteFilter = (e, contextFilter) => { + e.stopPropagation(); + deleteContextFilter(contextFilter.id); + } + const handleRemoveGroup = (filter, group) => { let groupId = group.group.id; const updatedGroups = { @@ -166,6 +230,11 @@ const ContextFilterView = (props) => { className="form-control mb-2" /> +