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

[Cleanup] Implementing New Approach For Saving Table Filters #2300

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion src/common/hooks/useDataTablePreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { TableFiltersPreference } from './useReactSettings';
import { dataTableFiltersAtom } from './useStoreSessionTableFilters';

interface Params {
tableKey: string;
tableKey: string | undefined;
}
export function useDataTablePreference(params: Params) {
const user = useUserChanges();
Expand All @@ -24,6 +24,10 @@ export function useDataTablePreference(params: Params) {
const storeSessionTableFilters = useAtomValue(dataTableFiltersAtom);

return (filterKey: keyof TableFiltersPreference) => {
if (!tableKey) {
return '';
}

if (filterKey === 'filter' || filterKey === 'currentPage') {
return storeSessionTableFilters?.[tableKey]?.[filterKey]
? storeSessionTableFilters[tableKey][filterKey]
Expand Down
16 changes: 14 additions & 2 deletions src/common/hooks/useDataTablePreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { useStoreSessionTableFilters } from './useStoreSessionTableFilters';
interface Params {
apiEndpoint: URL;
customFilters?: SelectOption[];
tableKey: string;
tableKey: string | undefined;
isInitialConfiguration: boolean;
customFilter: string[] | undefined;
setFilter: Dispatch<SetStateAction<string>>;
Expand All @@ -38,6 +38,7 @@ interface Params {
setStatus: Dispatch<SetStateAction<string[]>>;
setPerPage: Dispatch<SetStateAction<PerPage>>;
withoutStoringPerPage: boolean;
enableSavingFilterPreference?: boolean;
}

export function useDataTablePreferences(params: Params) {
Expand All @@ -58,6 +59,7 @@ export function useDataTablePreferences(params: Params) {
setStatus,
setPerPage,
withoutStoringPerPage,
enableSavingFilterPreference,
} = params;

const getPreference = useDataTablePreference({ tableKey });
Expand Down Expand Up @@ -85,7 +87,7 @@ export function useDataTablePreferences(params: Params) {
status: string[],
perPage: PerPage
) => {
if (!customFilter) {
if (!customFilter || !tableKey || !enableSavingFilterPreference) {
return;
}

Expand Down Expand Up @@ -124,6 +126,16 @@ export function useDataTablePreferences(params: Params) {
const updatedUser = cloneDeep(user) as User;

if (updatedUser) {
// @Todo: This is a temporary solution for creating the table_filters object. It can be removed after some time.
const tableFilters =
updatedUser.company_user?.react_settings.table_filters || {};

Object.keys(tableFilters).forEach((key) => {
if (key.includes('/')) {
delete tableFilters[key];
}
});

set(
updatedUser,
`company_user.react_settings.table_filters.${tableKey}`,
Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useDataTableUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import collect from 'collect.js';
interface Params {
apiEndpoint: URL;
isInitialConfiguration: boolean;
tableKey: string;
tableKey: string | undefined;
customFilters?: SelectOption[];
customFilter?: string[] | undefined;
}
Expand Down
6 changes: 5 additions & 1 deletion src/common/hooks/useStoreSessionTableFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ export const dataTableFiltersAtom = atomWithStorage<SessionDataTableFilters>(
);

interface Params {
tableKey: string;
tableKey: string | undefined;
}
export function useStoreSessionTableFilters(params: Params) {
const { tableKey } = params;

const setDataTableFilters = useSetAtom(dataTableFiltersAtom);

return (filter: string, currentPage: number) => {
if (!tableKey) {
return;
}

setDataTableFilters((current) => ({
...current,
[tableKey]: {
Expand Down
11 changes: 5 additions & 6 deletions src/components/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import { Guard } from '$app/common/guards/Guard';
import { EntityState } from '$app/common/enums/entity-state';
import { GenericSingleResourceResponse } from '$app/common/interfaces/generic-api-response';
import { refetchByUrl } from '$app/common/hooks/useRefetch';
import { useLocation } from 'react-router-dom';
import { useDataTableOptions } from '$app/common/hooks/useDataTableOptions';
import { useDataTableUtilities } from '$app/common/hooks/useDataTableUtilities';
import { useDataTablePreferences } from '$app/common/hooks/useDataTablePreferences';
Expand Down Expand Up @@ -152,6 +151,7 @@ interface Props<T> extends CommonProps {
withoutPerPageAsPreference?: boolean;
withoutSortQueryParameter?: boolean;
showRestoreBulk?: (selectedResources: T[]) => boolean;
enableSavingFilterPreference?: boolean;
}

export type ResourceAction<T> = (resource: T) => ReactElement;
Expand All @@ -160,7 +160,6 @@ export type PerPage = '10' | '50' | '100';

export function DataTable<T extends object>(props: Props<T>) {
const [t] = useTranslation();
const location = useLocation();
const options = useDataTableOptions();

const reactSettings = useReactSettings();
Expand All @@ -174,8 +173,6 @@ export function DataTable<T extends object>(props: Props<T>) {
new URL(endpoint(props.endpoint))
);

const tableKey = `${location.pathname}${props.endpoint.replace('.', '')}`;

const setInvalidationQueryAtom = useSetAtom(invalidationQueryAtom);

const {
Expand All @@ -193,6 +190,7 @@ export function DataTable<T extends object>(props: Props<T>) {
withoutPerPageAsPreference = false,
withoutSortQueryParameter = false,
showRestoreBulk,
enableSavingFilterPreference = false,
} = props;

const companyUpdateTimeOut = useRef<NodeJS.Timeout | undefined>(undefined);
Expand Down Expand Up @@ -232,9 +230,10 @@ export function DataTable<T extends object>(props: Props<T>) {
setSort,
setSortedBy,
setStatus,
tableKey,
tableKey: `${props.resource}s`,
customFilters,
withoutStoringPerPage: withoutPerPageAsPreference,
enableSavingFilterPreference,
});

const {
Expand All @@ -244,7 +243,7 @@ export function DataTable<T extends object>(props: Props<T>) {
} = useDataTableUtilities({
apiEndpoint,
isInitialConfiguration,
tableKey,
tableKey: `${props.resource}s`,
customFilter,
customFilters,
});
Expand Down
7 changes: 2 additions & 5 deletions src/pages/clients/index/Clients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ export default function Clients() {
} = useChangeTemplate();

return (
<Default
breadcrumbs={pages}
title={t('clients')}
docsLink="en/clients"
>
<Default breadcrumbs={pages} title={t('clients')} docsLink="en/clients">
<DataTable
resource="client"
endpoint="/api/v1/clients?include=group_settings&sort=id|desc"
Expand Down Expand Up @@ -86,6 +82,7 @@ export default function Clients() {
}
linkToCreateGuards={[permission('create_client')]}
hideEditableOptions={!hasPermission('edit_client')}
enableSavingFilterPreference
/>

<ChangeTemplateModal<Client>
Expand Down
1 change: 1 addition & 0 deletions src/pages/credits/index/Credits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default function Credits() {
dateRangeColumns={dateRangeColumns}
linkToCreateGuards={[permission('create_credit')]}
hideEditableOptions={!hasPermission('edit_credit')}
enableSavingFilterPreference
/>

<ChangeTemplateModal<Credit>
Expand Down
1 change: 1 addition & 0 deletions src/pages/expenses/index/Expenses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default function Expenses() {
}
linkToCreateGuards={[permission('create_expense')]}
hideEditableOptions={!hasPermission('edit_expense')}
enableSavingFilterPreference
/>
</Default>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/invoices/index/Invoices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export default function Invoices() {
setInvoiceSliderVisibility(true);
}}
dateRangeColumns={dateRangeColumns}
enableSavingFilterPreference
/>

{!disableNavigation('invoice', invoiceSlider) && <InvoiceSlider />}
Expand Down
1 change: 1 addition & 0 deletions src/pages/payments/index/Payments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export default function Payments() {
(payment) => getEntityState(payment) === EntityState.Archived
)
}
enableSavingFilterPreference
/>

{!disableNavigation('payment', paymentSlider) && <PaymentSlider />}
Expand Down
1 change: 1 addition & 0 deletions src/pages/products/index/Products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function Products() {
}
linkToCreateGuards={[permission('create_product')]}
hideEditableOptions={!hasPermission('edit_product')}
enableSavingFilterPreference
/>
</Default>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/projects/index/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default function Projects() {
}
linkToCreateGuards={[permission('create_project')]}
hideEditableOptions={!hasPermission('edit_project')}
enableSavingFilterPreference
/>

<ChangeTemplateModal<Project>
Expand Down
1 change: 1 addition & 0 deletions src/pages/purchase-orders/index/PurchaseOrders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default function PurchaseOrders() {
dateRangeColumns={dateRangeColumns}
linkToCreateGuards={[permission('create_purchase_order')]}
hideEditableOptions={!hasPermission('edit_purchase_order')}
enableSavingFilterPreference
/>

<ChangeTemplateModal<PurchaseOrder>
Expand Down
1 change: 1 addition & 0 deletions src/pages/quotes/index/Quotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export default function Quotes() {
dateRangeColumns={dateRangeColumns}
linkToCreateGuards={[permission('create_quote')]}
hideEditableOptions={!hasPermission('edit_quote')}
enableSavingFilterPreference
/>

{!disableNavigation('quote', quoteSlider) && <QuoteSlider />}
Expand Down
1 change: 1 addition & 0 deletions src/pages/recurring-expenses/index/RecurringExpenses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default function RecurringExpenses() {
}
linkToCreateGuards={[permission('create_recurring_expense')]}
hideEditableOptions={!hasPermission('edit_recurring_expense')}
enableSavingFilterPreference
/>
</Default>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/recurring-invoices/index/RecurringInvoices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export default function RecurringInvoices() {
);
setRecurringInvoiceSliderVisibility(true);
}}
enableSavingFilterPreference
/>

{!disableNavigation('recurring_invoice', recurringInvoiceSlider) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function TransactionRules() {
linkToCreate="/settings/bank_accounts/transaction_rules/create"
linkToEdit="/settings/bank_accounts/transaction_rules/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/bank-accounts/index/BankAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function BankAccounts() {
)}
</div>
}
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function ExpenseCategories() {
linkToCreate="/settings/expense_categories/create"
linkToEdit="/settings/expense_categories/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
);
}
1 change: 1 addition & 0 deletions src/pages/settings/group-settings/index/GroupSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function GroupSettings() {
linkToEdit="/settings/group_settings/:id/edit"
customActions={actions}
withResourcefulActions
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/integrations/api-tokens/ApiTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function ApiTokens() {
linkToCreate="/settings/integrations/api_tokens/create"
linkToEdit="/settings/integrations/api_tokens/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function ApiWebhooks() {
linkToCreate="/settings/integrations/api_webhooks/create"
linkToEdit="/settings/integrations/api_webhooks/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function CustomDesigns() {
linkToEdit="/settings/invoice_design/custom_designs/:id/edit"
withResourcefulActions
hideEditableOptions={!proPlan() && !enterprisePlan()}
enableSavingFilterPreference
/>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/payment-terms/PaymentTerms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function PaymentTerms() {
linkToCreate="/settings/payment_terms/create"
linkToEdit="/settings/payment_terms/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/schedules/index/Schedules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function Schedules() {
linkToCreate="/settings/schedules/create"
linkToEdit="/settings/schedules/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/subscriptions/index/Subscriptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function Subscriptions() {
linkToCreate="/settings/subscriptions/create"
linkToEdit="/settings/subscriptions/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
</Settings>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/task-statuses/TaskStatuses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function TaskStatuses() {
linkToCreate="/settings/task_statuses/create"
linkToEdit="/settings/task_statuses/:id/edit"
withResourcefulActions
enableSavingFilterPreference
/>
);
}
1 change: 1 addition & 0 deletions src/pages/settings/tax-rates/TaxRates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function TaxRates() {
linkToEdit="/settings/tax_rates/:id/edit"
withResourcefulActions
onBulkActionSuccess={(_, action) => onBulkActionsSuccess(action)}
enableSavingFilterPreference
/>
);
}
1 change: 1 addition & 0 deletions src/pages/settings/users/index/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function Users() {
setAction(action);
setIsPasswordConfirmModalOpen(true);
}}
enableSavingFilterPreference
/>
</Settings>

Expand Down
1 change: 1 addition & 0 deletions src/pages/tasks/index/Tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default function Tasks() {
setSliderTaskId(quote.id);
setTaskSliderVisibility(true);
}}
enableSavingFilterPreference
/>

{!disableNavigation('task', taskSlider) && <TaskSlider />}
Expand Down
1 change: 1 addition & 0 deletions src/pages/transactions/index/Transactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export default function Transactions() {
withResourcefulActions
linkToCreateGuards={[permission('create_bank_transaction')]}
hideEditableOptions={!hasPermission('edit_bank_transaction')}
enableSavingFilterPreference
/>
</Default>
</>
Expand Down
1 change: 1 addition & 0 deletions src/pages/vendors/index/Vendors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default function Vendors() {
}
linkToCreateGuards={[permission('create_vendor')]}
hideEditableOptions={!hasPermission('edit_vendor')}
enableSavingFilterPreference
/>
</Default>
);
Expand Down
Loading