mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-04 22:08:29 +00:00
* add wip ui components for csv upload * add backend support for csv bulk upload * add missing type information * add wip ui setup for bulk upload * update bulk upload drawer to handle errors * update error message * move export table from navbar to popover * revise navbar buttons * remove unrelated changes * Fixed UI issues * remove redundant counter increment * remove console.log * explicitly add express import * fix undefined toast error * Fixed button UI issues * rename button * remove unused solid icon * fix row count check * handle null values * add max row count at lib options * check NaN for parsed numbers * skip columns for null values to insert default * reuse declared set * only validate datatype conversion but pass original string * fix content type on requests * remove filter/sort applied by text * reset query and sort filters after upload * add padding for filter/sort buttons * make error on max rows explicit * handle stream errors through events * fix bulk upload to be disabled after drawer close * fix lint * update bulk upload success toast * scroll lock bulk upload drawer * update icon for export * order default by id desc * Fixed hover state issue for download button in bulk upload section * added focusTrapoption's initial focus as false in FocusTrap * Updated data-cy for export option (#7545) * Updated export data-cy * Updated cypess database spec (#7547) --------- Co-authored-by: Abd-Rahman-1999 <s.rahmanabd1999@gmail.com> Co-authored-by: Mekhla Asopa <59684099+Mekhla-Asopa@users.noreply.github.com>
109 lines
3.5 KiB
JavaScript
109 lines
3.5 KiB
JavaScript
import { useRef } from 'react';
|
|
import PostgrestQueryBuilder from '@/_helpers/postgrestQueryBuilder';
|
|
import { tooljetDatabaseService } from '@/_services';
|
|
import { isEmpty } from 'lodash';
|
|
import { toast } from 'react-hot-toast';
|
|
|
|
export const usePostgrestQueryBuilder = ({ organizationId, selectedTable, setSelectedTableData, setTotalRecords }) => {
|
|
const postgrestQueryBuilder = useRef({
|
|
filterQuery: new PostgrestQueryBuilder(),
|
|
sortQuery: new PostgrestQueryBuilder(),
|
|
paginationQuery: new PostgrestQueryBuilder(),
|
|
});
|
|
|
|
const handleBuildSortQuery = (filters) => {
|
|
postgrestQueryBuilder.current.sortQuery = new PostgrestQueryBuilder();
|
|
Object.keys(filters).map((key) => {
|
|
if (!isEmpty(filters[key])) {
|
|
const { column, order } = filters[key];
|
|
if (!isEmpty(column) && !isEmpty(order)) {
|
|
postgrestQueryBuilder.current.sortQuery.order(column, order);
|
|
}
|
|
}
|
|
});
|
|
updateSelectedTableData();
|
|
};
|
|
|
|
const updateSelectedTableData = async () => {
|
|
const sortQuery = isEmpty(postgrestQueryBuilder.current.sortQuery.url.toString())
|
|
? 'order=id.desc'
|
|
: postgrestQueryBuilder.current.sortQuery.url.toString();
|
|
|
|
const query =
|
|
postgrestQueryBuilder.current.filterQuery.url.toString() +
|
|
'&' +
|
|
sortQuery +
|
|
'&' +
|
|
postgrestQueryBuilder.current.paginationQuery.url.toString();
|
|
|
|
const { headers, data, error } = await tooljetDatabaseService.findOne(organizationId, selectedTable.id, query);
|
|
|
|
if (error) {
|
|
toast.error(error?.message ?? 'Something went wrong');
|
|
return;
|
|
}
|
|
|
|
const totalRecords = headers['content-range'].split('/')[1] || 0;
|
|
|
|
if (Array.isArray(data)) {
|
|
setTotalRecords(totalRecords);
|
|
setSelectedTableData(data);
|
|
}
|
|
};
|
|
|
|
const handleBuildFilterQuery = (filters) => {
|
|
postgrestQueryBuilder.current.filterQuery = new PostgrestQueryBuilder();
|
|
Object.keys(filters).map((key) => {
|
|
if (!isEmpty(filters[key])) {
|
|
const { column, operator, value } = filters[key];
|
|
if (!isEmpty(column) && !isEmpty(operator) && !isEmpty(value)) {
|
|
postgrestQueryBuilder.current.filterQuery.filter(column, operator, value);
|
|
}
|
|
}
|
|
});
|
|
|
|
updateSelectedTableData();
|
|
};
|
|
|
|
const buildPaginationQuery = (limit, offset) => {
|
|
postgrestQueryBuilder.current.paginationQuery.limit(limit);
|
|
postgrestQueryBuilder.current.paginationQuery.offset(offset);
|
|
|
|
updateSelectedTableData();
|
|
};
|
|
|
|
const resetSortQuery = () => {
|
|
postgrestQueryBuilder.current.sortQuery = new PostgrestQueryBuilder();
|
|
postgrestQueryBuilder.current.paginationQuery.limit(50);
|
|
postgrestQueryBuilder.current.paginationQuery.offset(0);
|
|
handleBuildSortQuery({});
|
|
};
|
|
|
|
const resetFilterQuery = () => {
|
|
postgrestQueryBuilder.current.filterQuery = new PostgrestQueryBuilder();
|
|
postgrestQueryBuilder.current.paginationQuery.limit(50);
|
|
postgrestQueryBuilder.current.paginationQuery.offset(0);
|
|
handleBuildFilterQuery({});
|
|
};
|
|
|
|
const resetAll = () => {
|
|
console.log('resetAll');
|
|
postgrestQueryBuilder.current.sortQuery = new PostgrestQueryBuilder();
|
|
|
|
postgrestQueryBuilder.current.paginationQuery.limit(50);
|
|
postgrestQueryBuilder.current.paginationQuery.offset(0);
|
|
|
|
postgrestQueryBuilder.current.filterQuery = new PostgrestQueryBuilder();
|
|
|
|
handleBuildSortQuery({});
|
|
};
|
|
|
|
return {
|
|
handleBuildFilterQuery,
|
|
handleBuildSortQuery,
|
|
buildPaginationQuery,
|
|
resetSortQuery,
|
|
resetFilterQuery,
|
|
resetAll,
|
|
};
|
|
};
|