Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reset table column resizing on ViewBar Cancel button click #1520

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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 { currentViewIdScopedState } from '@/ui/view-bar/states/currentViewIdScopedState';
Expand Down Expand Up @@ -38,14 +39,16 @@ export function TableHeader<SortField>({
const canPersistTableColumns = useRecoilValue(
canPersistTableColumnsScopedFamilySelector([tableScopeId, currentViewId]),
);
const tableColumns = useRecoilScopedValue(
const [tableColumns, setTableColumns] = useRecoilScopedState(
tableColumnsScopedState,
TableRecoilScopeContext,
);
const setSavedTableColumns = useSetRecoilState(
const [savedTableColumns, setSavedTableColumns] = useRecoilState(
savedTableColumnsFamilyState(currentViewId),
);

const handleViewBarReset = () => setTableColumns(savedTableColumns);
charlesBochet marked this conversation as resolved.
Show resolved Hide resolved

const handleViewSelect = useRecoilCallback(
({ set, snapshot }) =>
async (viewId: string) => {
Expand Down Expand Up @@ -79,6 +82,7 @@ export function TableHeader<SortField>({
<ViewBar
{...props}
canPersistViewFields={canPersistTableColumns}
onReset={handleViewBarReset}
onViewSelect={handleViewSelect}
onViewSubmit={handleViewSubmit}
OptionsDropdownButton={OptionsDropdownButton}
Expand Down
38 changes: 10 additions & 28 deletions front/src/modules/ui/view-bar/components/ViewBar.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import { ComponentProps, type ComponentType, type Context } from 'react';
import { useRecoilValue } from 'recoil';
import type { ComponentProps, ComponentType, Context } from 'react';

import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton';
import { TopBar } from '@/ui/top-bar/TopBar';
import { useContextScopeId } from '@/ui/utilities/recoil-scope/hooks/useContextScopeId';
import { useRecoilScopedValue } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedValue';

import { currentViewIdScopedState } from '../states/currentViewIdScopedState';
import { canPersistFiltersScopedFamilySelector } from '../states/selectors/canPersistFiltersScopedFamilySelector';
import { canPersistSortsScopedFamilySelector } from '../states/selectors/canPersistSortsScopedFamilySelector';
import { FiltersHotkeyScope } from '../types/FiltersHotkeyScope';
import { ViewsHotkeyScope } from '../types/ViewsHotkeyScope';

import { FilterDropdownButton } from './FilterDropdownButton';
import {
SortDropdownButton,
SortDropdownButtonProps,
type SortDropdownButtonProps,
} from './SortDropdownButton';
import {
UpdateViewButtonGroup,
UpdateViewButtonGroupProps,
type UpdateViewButtonGroupProps,
} from './UpdateViewButtonGroup';
import ViewBarDetails from './ViewBarDetails';
import ViewBarDetails, { type ViewBarDetailsProps } from './ViewBarDetails';
import {
ViewsDropdownButton,
ViewsDropdownButtonProps,
type ViewsDropdownButtonProps,
} from './ViewsDropdownButton';
charlesBochet marked this conversation as resolved.
Show resolved Hide resolved

export type ViewBarProps<SortField> = ComponentProps<'div'> & {
canPersistViewFields?: boolean;
OptionsDropdownButton: ComponentType;
optionsDropdownKey: string;
scopeContext: Context<string | null>;
Expand All @@ -37,12 +30,14 @@ export type ViewBarProps<SortField> = ComponentProps<'div'> & {
'defaultViewName' | 'onViewsChange' | 'onViewSelect'
> &
Pick<SortDropdownButtonProps<SortField>, 'availableSorts'> &
Pick<ViewBarDetailsProps, 'canPersistViewFields' | 'onReset'> &
Pick<UpdateViewButtonGroupProps, 'onViewSubmit'>;

export const ViewBar = <SortField,>({
availableSorts,
canPersistViewFields,
defaultViewName,
onReset,
onViewsChange,
onViewSelect,
onViewSubmit,
Expand All @@ -51,19 +46,6 @@ export const ViewBar = <SortField,>({
scopeContext,
...props
}: ViewBarProps<SortField>) => {
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,
});
Expand Down Expand Up @@ -100,13 +82,13 @@ export const ViewBar = <SortField,>({
}
bottomComponent={
<ViewBarDetails
canPersistView={
canPersistViewFields || canPersistFilters || canPersistSorts
}
canPersistViewFields={canPersistViewFields}
context={scopeContext}
hasFilterButton
onReset={onReset}
rightComponent={
<UpdateViewButtonGroup
canPersistViewFields={canPersistViewFields}
onViewEditModeChange={openOptionsDropdownButton}
onViewSubmit={onViewSubmit}
HotkeyScope={ViewsHotkeyScope.CreateDropdown}
Expand Down
32 changes: 27 additions & 5 deletions front/src/modules/ui/view-bar/components/ViewBarDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import type { Context, ReactNode } from 'react';
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil';

import {
IconArrowNarrowDown,
IconArrowNarrowUp,
IconPlus,
} from '@/ui/icon/index';
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 { useRemoveFilter } from '../hooks/useRemoveFilter';
import { availableFiltersScopedState } from '../states/availableFiltersScopedState';
import { currentViewIdScopedState } from '../states/currentViewIdScopedState';
import { filtersScopedState } from '../states/filtersScopedState';
import { isViewBarExpandedScopedState } from '../states/isViewBarExpandedScopedState';
import { canPersistFiltersScopedFamilySelector } from '../states/selectors/canPersistFiltersScopedFamilySelector';
import { canPersistSortsScopedFamilySelector } from '../states/selectors/canPersistSortsScopedFamilySelector';
import { sortsScopedState } from '../states/sortsScopedState';
import { FiltersHotkeyScope } from '../types/FiltersHotkeyScope';
import { SelectedSortType } from '../types/interface';
Expand All @@ -21,10 +27,11 @@ import { getOperandLabelShort } from '../utils/getOperandLabel';
import { FilterDropdownButton } from './FilterDropdownButton';
import SortOrFilterChip from './SortOrFilterChip';

type OwnProps = {
canPersistView?: boolean;
export type ViewBarDetailsProps = {
canPersistViewFields?: boolean;
context: Context<string | null>;
hasFilterButton?: boolean;
onReset?: () => void;
rightComponent?: ReactNode;
};

Expand Down Expand Up @@ -97,13 +104,18 @@ const StyledAddFilterContainer = styled.div`
`;

function ViewBarDetails<SortField>({
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,
Expand All @@ -112,11 +124,20 @@ function ViewBarDetails<SortField>({
availableFiltersScopedState,
context,
);
const canPersistFilters = useRecoilValue(
canPersistFiltersScopedFamilySelector([recoilScopeId, currentViewId]),
);

const [sorts, setSorts] = useRecoilScopedState<SelectedSortType<SortField>[]>(
sortsScopedState,
context,
);
const canPersistSorts = useRecoilValue(
canPersistSortsScopedFamilySelector([recoilScopeId, currentViewId]),
);

const canPersistView =
canPersistViewFields || canPersistFilters || canPersistSorts;

const [isViewBarExpanded] = useRecoilScopedState(
isViewBarExpandedScopedState,
Expand All @@ -137,6 +158,7 @@ function ViewBarDetails<SortField>({
const removeFilter = useRemoveFilter(context);

function handleCancelClick() {
onReset?.();
setFilters([]);
setSorts([]);
}
Expand Down Expand Up @@ -208,7 +230,7 @@ function ViewBarDetails<SortField>({
</StyledAddFilterContainer>
)}
</StyledFilterContainer>
{filters.length + sorts.length > 0 && (
{(filters.length + sorts.length > 0 || canPersistViewFields) && (
<StyledCancelButton
data-testid="cancel-button"
onClick={handleCancelClick}
Expand Down
Loading