Skip to content

Commit

Permalink
UIREQ-1146 - Revert the custom "Print Status" filter code implementat…
Browse files Browse the repository at this point in the history
…ion (#1202)

* UIREQ-1146 - revert Custom PrintStatus Filter implementation code

* UIREQ-1146 - revert Custom PrintStatus Filter implementation code

* UIREQ-1146 - revert custom PrintStatus filter code
  • Loading branch information
manvendra-s-rathore authored Sep 18, 2024
1 parent e4d5226 commit bd0ec5e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 199 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Add missing sub-permissions to fetch staff slips records. Refs UIREQ-1129.
* Add missing sub-permissions to fetch "pick-slips" and "search-slips" records in "Requests: View, edit, cancel" permission. Refs UIREQ-1137.
* Optimize performance of the "print-events-entry" API to reduce slowness during the initial call. Refs UIREQ-1130.
* Revert Custom "Print Status" filter code implementation. Refs UIREQ-1146.

## [9.1.2] (https://github.com/folio-org/ui-requests/tree/v9.1.2) (2024-09-13)
[Full Changelog](https://github.com/folio-org/ui-requests/compare/v9.1.1...v9.1.2)
Expand Down
69 changes: 8 additions & 61 deletions src/routes/RequestsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import {
DEFAULT_REQUEST_TYPE_VALUE,
INPUT_REQUEST_SEARCH_SELECTOR,
PRINT_DETAILS_COLUMNS,
requestFilterTypes,
} from '../constants';
import {
buildUrl,
Expand Down Expand Up @@ -101,8 +100,6 @@ import {
getFormattedYears,
getStatusQuery,
getFullNameForCsvRecords,
getPrintStatusFilteredData,
filterRecordsByPrintStatus,
} from './utils';
import SinglePrintButtonForPickSlip from '../components/SinglePrintButtonForPickSlip';

Expand Down Expand Up @@ -291,8 +288,6 @@ export const buildHoldRecords = (records) => {
});
};

export const viewPrintDetailsPath = 'circulationSettings.records[0].value.enablePrintLog';

class RequestsRoute extends React.Component {
static contextType = CalloutContext;

Expand Down Expand Up @@ -650,7 +645,6 @@ class RequestsRoute extends React.Component {
titleLevelRequestsFeatureEnabled,
createTitleLevelRequestsByDefault,
isViewPrintDetailsEnabled: false,
selectedPrintStatusFilters: [],
};

this.pickSlipsPrintContentRef = React.createRef();
Expand All @@ -662,7 +656,7 @@ class RequestsRoute extends React.Component {
static getDerivedStateFromProps(props, state) {
const layer = (props.resources.query || {}).layer;
const newState = {};
const currViewPrintDetailsSettings = get(props.resources, viewPrintDetailsPath) === 'true';
const currViewPrintDetailsSettings = get(props.resources, 'circulationSettings.records[0].value.enablePrintLog') === 'true';

if (!layer) {
newState.dupRequest = null;
Expand All @@ -683,13 +677,13 @@ class RequestsRoute extends React.Component {
componentDidUpdate(prevProps, prevState) {
const patronBlocks = get(this.props.resources, ['patronBlocks', 'records'], []);
const prevBlocks = get(prevProps.resources, ['patronBlocks', 'records'], []);
const { submitting, isViewPrintDetailsEnabled, selectedPrintStatusFilters } = this.state;
const { submitting, isViewPrintDetailsEnabled } = this.state;
const prevExpired = prevBlocks.filter(p => moment(moment(p.expirationDate).format()).isSameOrBefore(moment().format()) && p.expirationDate) || [];
const expired = patronBlocks.filter(p => moment(moment(p.expirationDate).format()).isSameOrBefore(moment().format()) && p.expirationDate) || [];
const { id: currentServicePointId } = this.getCurrentServicePointInfo();
const prevStateServicePointId = get(prevProps.resources.currentServicePoint, 'id');
const { configs: prevConfigs } = prevProps.resources;
const { configs, query: { filters } } = this.props.resources;
const { configs } = this.props.resources;
const instanceId = parse(this.props.location?.search)?.instanceId;

if (prevExpired.length > 0 && expired.length === 0) {
Expand Down Expand Up @@ -734,30 +728,6 @@ class RequestsRoute extends React.Component {
if (isViewPrintDetailsEnabled !== prevState.isViewPrintDetailsEnabled && !isViewPrintDetailsEnabled) {
this.columnHeadersMap = getFilteredColumnHeadersMap(this.columnHeadersMap);
}

if (filters?.includes(requestFilterTypes.PRINT_STATUS)) {
const printStatusFilterInQuery = this.getActiveFilters()[requestFilterTypes.PRINT_STATUS];

this.updateSelectedPrintStatusFilters(isViewPrintDetailsEnabled, selectedPrintStatusFilters, printStatusFilterInQuery);
}
}

updateSelectedPrintStatusFilters(isViewPrintDetailsEnabled, selectedPrintStatusFilters, printStatusFilterInQuery) {
/**
* Updates the `selectedPrintStatusFilters` state based on pre selected filters when user navigates back to Request App.
*
* The function performs the following actions:
* 1. If `isViewPrintDetailsEnabled` is true and if `Print Status` filters contains exactly one filter:
* - it updates state to set `selectedPrintStatusFilters` to this one `Print Status` filter.
*
* 2. If `isViewPrintDetailsEnabled` is false and `filters` in query includes 'PRINT STATUS' filter:
* - it clears the 'PRINT STATUS' filter from query by invoking `handleFilterChange`.
*/
if (isViewPrintDetailsEnabled && selectedPrintStatusFilters.length === 0 && printStatusFilterInQuery?.length === 1) {
this.setState({ selectedPrintStatusFilters: [printStatusFilterInQuery[0]] });
} else if (!isViewPrintDetailsEnabled && printStatusFilterInQuery?.length) {
this.handleFilterChange({ name: requestFilterTypes.PRINT_STATUS, values: [] });
}
}

toggleAllRows = () => {
Expand Down Expand Up @@ -834,33 +804,24 @@ class RequestsRoute extends React.Component {
// Export function for the CSV search report action
async exportData() {
this.setState({ csvReportPending: true });
const { isViewPrintDetailsEnabled, selectedPrintStatusFilters } = this.state;

const activeFilters = this.getActiveFilters();
const activeFilterKeys = Object.keys(activeFilters);
const isOnlyPrintStatusFilterSelected = activeFilterKeys.length === 1 && activeFilterKeys[0] === requestFilterTypes.PRINT_STATUS;

// Build a custom query for the CSV record export, which has to include
// all search and filter parameters
const queryClauses = [];
let queryString;

const queryTerm = this.props.resources?.query?.query;
const filterQuery = filters2cql(RequestsFiltersConfig, deparseFilters(activeFilters));
const filterQuery = filters2cql(RequestsFiltersConfig, deparseFilters(this.getActiveFilters()));

if (queryTerm) {
queryString = `(requesterId=="${queryTerm}" or requester.barcode="${queryTerm}*" or item.title="${queryTerm}*" or item.barcode=="${queryTerm}*" or itemId=="${queryTerm}")`;
queryClauses.push(queryString);
}
if (filterQuery) queryClauses.push(filterQuery);
if (isOnlyPrintStatusFilterSelected) queryClauses.push('cql.allRecords=1');

queryString = queryClauses.join(' and ');
const records = await this.fetchReportData(this.props.mutator.reportRecords, queryString);

const printStatusFilteredRecords = isViewPrintDetailsEnabled && selectedPrintStatusFilters.length === 1 &&
filterRecordsByPrintStatus(records, selectedPrintStatusFilters);
const recordsToCSV = this.buildRecords(printStatusFilteredRecords || records);
const recordsToCSV = this.buildRecords(records);

exportCsv(recordsToCSV, {
onlyFields: this.columnHeadersMap,
Expand Down Expand Up @@ -1232,10 +1193,6 @@ class RequestsRoute extends React.Component {
}

handleFilterChange = ({ name, values }) => {
if (name === requestFilterTypes.PRINT_STATUS) {
this.setState({ selectedPrintStatusFilters: values });
}

const { mutator } = this.props;
const newFilters = {
...this.getActiveFilters(),
Expand Down Expand Up @@ -1381,7 +1338,6 @@ class RequestsRoute extends React.Component {
holdsShelfReportPending,
createTitleLevelRequestsByDefault,
isViewPrintDetailsEnabled,
selectedPrintStatusFilters,
} = this.state;
const isPrintHoldRequestsEnabled = getPrintHoldRequestsEnabled(resources.printHoldRequests);
const { name: servicePointName } = this.getCurrentServicePointInfo();
Expand Down Expand Up @@ -1427,7 +1383,6 @@ class RequestsRoute extends React.Component {

const isPickSlipsArePending = resources?.pickSlips?.isPending;
const isSearchSlipsArePending = resources?.searchSlips?.isPending;
const isRequestsRecordsLoaded = resources.records.hasLoaded;
const requestsEmpty = isEmpty(requests);
const isPickSlipsEmpty = isEmpty(pickSlips);
const isSearchSlipsEmpty = isEmpty(searchSlips);
Expand All @@ -1436,14 +1391,6 @@ class RequestsRoute extends React.Component {
const pickSlipsData = convertToSlipData(pickSlips, intl, timezone, locale, SLIPS_TYPE.PICK_SLIP, user);
const searchSlipsData = convertToSlipData(searchSlips, intl, timezone, locale, SLIPS_TYPE.SEARCH_SLIP_HOLD_REQUESTS);
let multiSelectPickSlipData = getSelectedSlipDataMulti(pickSlipsData, selectedRows);
/**
* For 'displayPrintStatusFilteredData' to be true the length of 'selectedPrintStatusFilters' must be 1.
* This is because we only filter data when exactly one PrintStatus filter is selected ([Printed] or [Not Printed]).
* If the filter array is empty or contains both filters ([] or [Printed, Not Printed]),
* no filtering is needed as the data should be used directly from the query response.
*/
const displayPrintStatusFilteredData = isViewPrintDetailsEnabled &&
isRequestsRecordsLoaded && selectedPrintStatusFilters.length === 1;

const resultsFormatter = getListFormatter(
{
Expand Down Expand Up @@ -1665,8 +1612,7 @@ class RequestsRoute extends React.Component {
customPaneSub={this.renderPaneSub()}
onCreate={this.create}
onCloseNewRecord={this.handleCloseNewRecord}
parentResources={displayPrintStatusFilteredData ?
getPrintStatusFilteredData(resources, selectedPrintStatusFilters) : resources}
parentResources={resources}
parentMutator={mutator}
detailProps={{
onChangePatron: this.onChangePatron,
Expand All @@ -1693,7 +1639,8 @@ class RequestsRoute extends React.Component {
renderFilters={this.renderFilters}
resultIsSelected={this.resultIsSelected}
onFilterChange={this.handleFilterChange}
sortableColumns={['requestDate', 'title', 'year', 'itemBarcode', 'callNumber', 'type', 'requestStatus', 'position', 'servicePoint', 'requester', 'requesterBarcode', 'proxy']}
sortableColumns={['requestDate', 'title', 'year', 'itemBarcode', 'callNumber', 'type', 'requestStatus',
'position', 'servicePoint', 'requester', 'requesterBarcode', 'proxy']}
pageAmount={100}
pagingType={MCLPagingTypes.PREV_NEXT}
/>
Expand Down
42 changes: 0 additions & 42 deletions src/routes/RequestsRoute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ jest.mock('./utils', () => ({
...jest.requireActual('./utils'),
getFormattedYears: jest.fn(),
getStatusQuery: jest.fn(),
filterRecordsByPrintStatus: jest.fn(),
getPrintStatusFilteredData: jest.fn(),
}));
jest.mock('../components', () => ({
ErrorModal: jest.fn(({ onClose }) => (
Expand Down Expand Up @@ -610,26 +608,6 @@ describe('RequestsRoute', () => {
});
});

it('should trigger "exportCsv" when only "Print Status" filter is selected', async () => {
const props = {
...defaultProps,
resources: {
...defaultProps.resources,
query: {
...defaultProps.resources.query,
filters: 'printStatus.Printed',
},
}
};
cleanup();
renderComponent(props);
await userEvent.click(screen.getByRole('button', { name: 'ui-requests.exportSearchResultsToCsv' }));

await waitFor(() => {
expect(exportCsv).toHaveBeenCalled();
});
});

it('should render "ErrorModal"', async () => {
await userEvent.click(screen.getByTestId('exportExpiredHoldShelfToCsvButton'));

Expand All @@ -652,26 +630,6 @@ describe('RequestsRoute', () => {

await userEvent.click(screen.getByRole('button', { name: 'onFilterChange' }));

expect(defaultProps.mutator.query.update).toBeCalledWith(expectFilterValue);
});

it('should trigger "mutator.query.update" when "Print Status" filters are present in query', async () => {
const props = {
...defaultProps,
resources: {
...defaultProps.resources,
query: {
...defaultProps.resources.query,
filters: 'filter1.value1,printStatus.Printed',
},
}
};
cleanup();
renderComponent(props);
const expectFilterValue = { 'filters': 'filter1.value1,printStatus.Printed,filter4.Value4,filter4.Value5' };

await userEvent.click(screen.getByRole('button', { name: 'onFilterChange' }));

expect(defaultProps.mutator.query.update).toHaveBeenCalledWith(expectFilterValue);
});

Expand Down
22 changes: 0 additions & 22 deletions src/routes/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { cloneDeep } from 'lodash';

import {
requestPrintStatusType,
requestStatuses,
requestTypesMap,
} from '../constants';
Expand Down Expand Up @@ -44,22 +41,3 @@ export const getFullNameForCsvRecords = (record) => {
const { firstName = '', middleName = '', lastName = '' } = record;
return [firstName, middleName, lastName].filter(Boolean).join(' ');
};

export const filterRecordsByPrintStatus = (records, printStatusFilters) => {
const isPrintedFilterSelected = printStatusFilters[0] === requestPrintStatusType.PRINTED;
return records.filter(record => {
const hasCopiesCount = record?.printDetails?.count !== undefined;

return isPrintedFilterSelected ? hasCopiesCount : !hasCopiesCount;
});
};

export const getPrintStatusFilteredData = (resources, printStatusFilters) => {
const clonedResources = cloneDeep(resources);
const filteredRecords = filterRecordsByPrintStatus(clonedResources.records.records, printStatusFilters);

clonedResources.records.records = filteredRecords;
clonedResources.records.other.totalRecords = filteredRecords.length;

return clonedResources;
};
74 changes: 0 additions & 74 deletions src/routes/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { cloneDeep } from 'lodash';
import {
requestPrintStatusType,
requestStatuses,
requestTypesMap,
} from '../constants';
Expand All @@ -11,8 +9,6 @@ import {
isReorderableRequest,
getStatusQuery,
getFullNameForCsvRecords,
filterRecordsByPrintStatus,
getPrintStatusFilteredData,
} from './utils';

describe('utils', () => {
Expand Down Expand Up @@ -189,74 +185,4 @@ describe('utils', () => {
expect(getFullNameForCsvRecords(record)).toBe('');
});
});

describe('filterRecordsByPrintStatus', () => {
const records = [
{ id: 'a', printDetails: { count: 5 } },
{ id: 'b', printDetails: null },
{ id: 'c', printDetails: undefined },
];

it('should filter and return only printed records when PRINTED filter is selected', () => {
const printStatusFilters = [requestPrintStatusType.PRINTED];
const result = filterRecordsByPrintStatus(records, printStatusFilters);
expect(result).toEqual([
{ id: 'a', printDetails: { count: 5 } }
]);
});

it('should filter and return only non-printed records when NOT_PRINTED filter is selected', () => {
const printStatusFilters = [requestPrintStatusType.NOT_PRINTED];
const result = filterRecordsByPrintStatus(records, printStatusFilters);
expect(result).toEqual([
{ id: 'b', printDetails: null },
{ id: 'c', printDetails: undefined }
]);
});
});

describe('getPrintStatusFilteredData', () => {
const resources = {
records: {
records: [
{ id: 1, printDetails: { count: 5 } },
{ id: 2, printDetails: null },
{ id: 3, printDetails: undefined },
],
other: {
totalRecords: 3
}
}
};

it('should return resources with only printed records when PRINTED filter is selected', () => {
const printStatusFilters = [requestPrintStatusType.PRINTED];
const result = getPrintStatusFilteredData(resources, printStatusFilters);

expect(result.records.records).toEqual([
{ id: 1, printDetails: { count: 5 } }
]);
expect(result.records.other.totalRecords).toBe(1);
});

it('should return resources with only non-printed records when NOT_PRINTED filter is selected', () => {
const printStatusFilters = [requestPrintStatusType.NOT_PRINTED];
const result = getPrintStatusFilteredData(resources, printStatusFilters);

expect(result.records.records).toEqual([
{ id: 2, printDetails: null },
{ id: 3, printDetails: undefined }
]);
expect(result.records.other.totalRecords).toBe(2);
});

it('should not modify the original resources object', () => {
const printStatusFilters = [requestPrintStatusType.PRINTED];
const clonedResources = cloneDeep(resources);

getPrintStatusFilteredData(resources, printStatusFilters);

expect(resources).toEqual(clonedResources);
});
});
});

0 comments on commit bd0ec5e

Please sign in to comment.