From 8ea4e6a51cb919a8592b273d253037cd91d4f691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tha=C3=AFs?= Date: Mon, 11 Sep 2023 03:51:24 +0200 Subject: [PATCH] feat: reset table column resizing on ViewBar Cancel button click (#1520) * feat: reset table column resizing on ViewBar Cancel button click Closes #1500 * Fix according to PR --------- Co-authored-by: Charles Bochet --- .../table-header/components/TableHeader.tsx | 22 +++++++---- .../ui/view-bar/components/ViewBar.tsx | 38 +++++-------------- .../ui/view-bar/components/ViewBarDetails.tsx | 32 +++++++++++++--- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/front/src/modules/ui/table/table-header/components/TableHeader.tsx b/front/src/modules/ui/table/table-header/components/TableHeader.tsx index 9c464d795..be287c364 100644 --- a/front/src/modules/ui/table/table-header/components/TableHeader.tsx +++ b/front/src/modules/ui/table/table-header/components/TableHeader.tsx @@ -1,11 +1,12 @@ import { useCallback } from 'react'; -import { useRecoilCallback, useRecoilValue, useSetRecoilState } from 'recoil'; +import { useRecoilCallback, useRecoilState, useRecoilValue } from 'recoil'; import { DropdownRecoilScopeContext } from '@/ui/dropdown/states/recoil-scope-contexts/DropdownRecoilScopeContext'; import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope'; import { useContextScopeId } from '@/ui/utilities/recoil-scope/hooks/useContextScopeId'; +import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState'; import { useRecoilScopedValue } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedValue'; -import { ViewBar, type ViewBarProps } from '@/ui/view-bar/components/ViewBar'; +import { ViewBar, ViewBarProps } from '@/ui/view-bar/components/ViewBar'; import { currentViewIdScopedState } from '@/ui/view-bar/states/currentViewIdScopedState'; import { TableOptionsDropdown } from '../../options/components/TableOptionsDropdown'; @@ -38,14 +39,18 @@ export function TableHeader({ const canPersistTableColumns = useRecoilValue( canPersistTableColumnsScopedFamilySelector([tableScopeId, currentViewId]), ); - const tableColumns = useRecoilScopedValue( + const [tableColumns, setTableColumns] = useRecoilScopedState( tableColumnsScopedState, TableRecoilScopeContext, ); - const setSavedTableColumns = useSetRecoilState( + const [savedTableColumns, setSavedTableColumns] = useRecoilState( savedTableColumnsFamilyState(currentViewId), ); + function handleViewBarReset() { + setTableColumns(savedTableColumns); + } + const handleViewSelect = useRecoilCallback( ({ set, snapshot }) => async (viewId: string) => { @@ -57,11 +62,13 @@ export function TableHeader({ [tableScopeId], ); - const handleViewSubmit = async () => { - if (canPersistTableColumns) setSavedTableColumns(tableColumns); + async function handleViewSubmit() { + if (canPersistTableColumns) { + setSavedTableColumns(tableColumns); + } await onViewSubmit?.(); - }; + } const OptionsDropdownButton = useCallback( () => ( @@ -79,6 +86,7 @@ export function TableHeader({ = ComponentProps<'div'> & { - canPersistViewFields?: boolean; OptionsDropdownButton: ComponentType; optionsDropdownKey: string; scopeContext: Context; @@ -37,12 +30,14 @@ export type ViewBarProps = ComponentProps<'div'> & { 'defaultViewName' | 'onViewsChange' | 'onViewSelect' > & Pick, 'availableSorts'> & + Pick & Pick; export const ViewBar = ({ availableSorts, canPersistViewFields, defaultViewName, + onReset, onViewsChange, onViewSelect, onViewSubmit, @@ -51,19 +46,6 @@ export const ViewBar = ({ scopeContext, ...props }: ViewBarProps) => { - const recoilScopeId = useContextScopeId(scopeContext); - - const currentViewId = useRecoilScopedValue( - currentViewIdScopedState, - scopeContext, - ); - const canPersistFilters = useRecoilValue( - canPersistFiltersScopedFamilySelector([recoilScopeId, currentViewId]), - ); - const canPersistSorts = useRecoilValue( - canPersistSortsScopedFamilySelector([recoilScopeId, currentViewId]), - ); - const { openDropdownButton: openOptionsDropdownButton } = useDropdownButton({ key: optionsDropdownKey, }); @@ -100,13 +82,13 @@ export const ViewBar = ({ } bottomComponent={ ; hasFilterButton?: boolean; + onReset?: () => void; rightComponent?: ReactNode; }; @@ -96,13 +103,18 @@ const StyledAddFilterContainer = styled.div` `; function ViewBarDetails({ - canPersistView, + canPersistViewFields, context, hasFilterButton = false, + onReset, rightComponent, -}: OwnProps) { +}: ViewBarDetailsProps) { const theme = useTheme(); + const recoilScopeId = useContextScopeId(context); + + const currentViewId = useRecoilScopedValue(currentViewIdScopedState, context); + const [filters, setFilters] = useRecoilScopedState( filtersScopedState, context, @@ -111,11 +123,20 @@ function ViewBarDetails({ availableFiltersScopedState, context, ); + const canPersistFilters = useRecoilValue( + canPersistFiltersScopedFamilySelector([recoilScopeId, currentViewId]), + ); const [sorts, setSorts] = useRecoilScopedState[]>( sortsScopedState, context, ); + const canPersistSorts = useRecoilValue( + canPersistSortsScopedFamilySelector([recoilScopeId, currentViewId]), + ); + + const canPersistView = + canPersistViewFields || canPersistFilters || canPersistSorts; const [isViewBarExpanded] = useRecoilScopedState( isViewBarExpandedScopedState, @@ -136,6 +157,7 @@ function ViewBarDetails({ const removeFilter = useRemoveFilter(context); function handleCancelClick() { + onReset?.(); setFilters([]); setSorts([]); } @@ -207,7 +229,7 @@ function ViewBarDetails({ )} - {filters.length + sorts.length > 0 && ( + {(filters.length + sorts.length > 0 || canPersistViewFields) && (