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

Move reusable claiming code from ui-receiving to the shared library #841

Merged
merged 13 commits into from
Dec 19, 2024
Merged
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @folio-org/acquisitions-ui
13 changes: 13 additions & 0 deletions PULL_REQUEST_TEMPLATE.md → .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@
[ ] Use GitHub checklists. When solved, check the box and explain the answer.
-->

## Screenshots
<!-- OPTIONAL
One picture is literally worth a thousand words. When the feature is
an interaction, an animated GIF or video is best. Most of the time it helps to
include "before" and "after" screenshots to quickly demonstrate the
value of the feature.

Here are some great tools to help you record gifs:

Windows: https://getsharex.com/
Mac: https://gifox.io/
-->

<!-- OPTIONAL
## Learning
Help out not only your reviewer, but also your fellow developer!
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Move reusable version history components to the ACQ lib. Refs UISACQCOMP-230.
* Move reusable helper function to support version history functionality. Refs UISACQCOMP-232.
* Move reusable version history hook useVersionHistoryValueResolvers to the ACQ lib. Refs UISACQCOMP-235.
* Move reusable claiming code from `ui-receiving` to the shared library. Refs UISACQCOMP-236.

## [6.0.2](https://github.com/folio-org/stripes-acq-components/tree/v6.0.2) (2024-12-04)
[Full Changelog](https://github.com/folio-org/stripes-acq-components/compare/v6.0.1...v6.0.2)
Expand Down
56 changes: 50 additions & 6 deletions lib/FindRecords/hooks/useRecordsSelect/useRecordsSelect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { useState, useCallback, useEffect } from 'react';
import { differenceBy, keyBy } from 'lodash';
import differenceBy from 'lodash/differenceBy';
import keyBy from 'lodash/keyBy';
import omit from 'lodash/omit';
import pick from 'lodash/pick';
import {
useState,
useCallback,
useEffect,
} from 'react';

import { EVENT_EMITTER_EVENTS } from '../../../constants';
import { useEventEmitter } from '../../../hooks';
Expand Down Expand Up @@ -55,7 +62,7 @@ export const useRecordsSelect = ({ records }) => {
}, [emitChangeEvent, eventEmitter]);

useEffect(() => {
setAllRecordsSelected(records.every(r => Boolean(selectedRecordsMap[r.id])));
setAllRecordsSelected(Boolean(records?.length) && records.every(r => Boolean(selectedRecordsMap[r.id])));
}, [records, selectedRecordsMap]);

const toggleSelectAll = useCallback(() => {
Expand Down Expand Up @@ -95,12 +102,49 @@ export const useRecordsSelect = ({ records }) => {
[selectedRecordsMap],
);

/*
* Clear all selected records.
*/
const resetAllSelectedRecords = useCallback(() => {
setSelectedRecordsMap({});
emitChangeEvent({});
}, [emitChangeEvent]);

/*
* Clear selected records with provided ids.
*/
const resetSelectedRecordsByIds = useCallback((ids) => {
setSelectedRecordsMap(prevMap => {
const newMap = omit(prevMap, ids);

emitChangeEvent(newMap);

return newMap;
});
}, [emitChangeEvent]);

/*
* Clear selected records except the ones with provided ids.
*/
const resetOtherSelectedRecordsByIds = useCallback((ids) => {
setSelectedRecordsMap(prevMap => {
const newMap = pick(prevMap, ids);

emitChangeEvent(newMap);

return newMap;
});
}, [emitChangeEvent]);

return {
allRecordsSelected,
selectedRecordsMap,
isRecordSelected,
resetAllSelectedRecords,
resetOtherSelectedRecordsByIds,
resetSelectedRecordsByIds,
selectedRecordsLength,
toggleSelectAll,
selectedRecordsMap,
selectRecord,
isRecordSelected,
toggleSelectAll,
};
};
1 change: 1 addition & 0 deletions lib/FindRecords/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './FindRecords';
export { useRecordsSelect } from './hooks';
42 changes: 42 additions & 0 deletions lib/claiming/components/FieldClaimingDate/FieldClaimingDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import PropTypes from 'prop-types';

import { FieldDatepickerFinal } from '../../../FieldDatepicker';
import { validateRequired } from '../../../utils';
import {
excludePreviousDays,
validateClaimingDate,
} from './utils';

export const FieldClaimingDate = ({
name,
required,
...props
}) => {
const validate = (value) => {
return (
(required && validateRequired(value))
|| validateClaimingDate(value)
);
};

return (
<FieldDatepickerFinal
usePortal
name={name}
required={required}
validate={validate}
exclude={excludePreviousDays}
{...props}
/>
);
};

FieldClaimingDate.propTypes = {
name: PropTypes.string,
required: PropTypes.bool,
};

FieldClaimingDate.defaultProps = {
name: 'claimingDate',
required: true,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';

import stripesFinalForm from '@folio/stripes/final-form';

import { FieldClaimingDate } from './FieldClaimingDate';

const defaultProps = {
label: 'Label',
};

const FormComponent = stripesFinalForm({})(({ children }) => <form>{children}</form>);

const renderFieldClaimingDate = (props = {}, formProps = {}) => render(
<FormComponent
onSubmit={jest.fn}
{...formProps}
>
<FieldClaimingDate
{...defaultProps}
{...props}
/>
</FormComponent>,
{ wrapper: MemoryRouter },
);

describe('FieldClaimingDate', () => {
it('should render field', () => {
renderFieldClaimingDate();

expect(screen.getByText(defaultProps.label)).toBeInTheDocument();
});
});
1 change: 1 addition & 0 deletions lib/claiming/components/FieldClaimingDate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { FieldClaimingDate } from './FieldClaimingDate';
19 changes: 19 additions & 0 deletions lib/claiming/components/FieldClaimingDate/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FormattedMessage } from 'react-intl';

import { dayjs } from '@folio/stripes/components';

const isSameOrBeforeDay = (day) => {
const today = dayjs().startOf('day');

return day.isSameOrBefore(today, 'day');
};

export const validateClaimingDate = (value) => {
return isSameOrBeforeDay(dayjs(value))
? <FormattedMessage id="stripes-acq-components.validation.dateAfter" />
: undefined;
};

export const excludePreviousDays = (day) => {
return isSameOrBeforeDay(day);
};
2 changes: 2 additions & 0 deletions lib/claiming/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './menu-items';
export * from './modals';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';

import {
Button,
Icon,
} from '@folio/stripes/components';

export const DelayClaimActionMenuItem = ({
disabled,
onClick,
}) => {
return (
<Button
data-testid="delay-claim-button"
disabled={disabled}
buttonStyle="dropdownItem"
onClick={onClick}
>
<Icon icon="calendar">
<FormattedMessage id="stripes-acq-components.claiming.action.delayClaim" />
</Icon>
</Button>
);
};

DelayClaimActionMenuItem.propTypes = {
disabled: PropTypes.bool,
onClick: PropTypes.func.isRequired,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { DelayClaimActionMenuItem } from './DelayClaimActionMenuItem';

const defaultProps = {
onClick: jest.fn(),
};

const renderComponent = (props = {}) => render(
<DelayClaimActionMenuItem
{...defaultProps}
{...props}
/>,
);

describe('DelayClaimActionMenuItem', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should call onClick when button is clicked', async () => {
renderComponent();

await userEvent.click(screen.getByTestId('delay-claim-button'));

expect(defaultProps.onClick).toHaveBeenCalledTimes(1);
});

it('should be disabled when disabled prop is true', () => {
renderComponent({ disabled: true });

expect(screen.getByTestId('delay-claim-button')).toBeDisabled();
});

it('should not call onClick when button is disabled and clicked', async () => {
renderComponent({ disabled: true });

await userEvent.click(screen.getByTestId('delay-claim-button'));

expect(defaultProps.onClick).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DelayClaimActionMenuItem } from './DelayClaimActionMenuItem';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';

import {
Button,
Icon,
} from '@folio/stripes/components';

export const MarkUnreceivableActionMenuItem = ({
disabled,
onClick,
}) => {
return (
<Button
data-testid="unreceivable-button"
disabled={disabled}
buttonStyle="dropdownItem"
onClick={onClick}
>
<Icon icon="cancel">
<FormattedMessage id="stripes-acq-components.claiming.action.unreceivable" />
</Icon>
</Button>
);
};

MarkUnreceivableActionMenuItem.propTypes = {
disabled: PropTypes.bool,
onClick: PropTypes.func.isRequired,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { MarkUnreceivableActionMenuItem } from './MarkUnreceivableActionMenuItem';

const defaultProps = {
onClick: jest.fn(),
disabled: false,
};

const renderComponent = (props = {}) => render(
<MarkUnreceivableActionMenuItem
{...defaultProps}
{...props}
/>,
);

describe('MarkUnreceivableActionMenuItem', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should render the button', () => {
renderComponent();

expect(screen.getByTestId('unreceivable-button')).toBeInTheDocument();
});

it('should call onClick when button is clicked', async () => {
renderComponent();

await userEvent.click(screen.getByTestId('unreceivable-button'));

expect(defaultProps.onClick).toHaveBeenCalledTimes(1);
});

it('should disable the button when disabled prop is true', () => {
renderComponent({ disabled: true });

expect(screen.getByTestId('unreceivable-button')).toBeDisabled();
});

it('should enable the button when disabled prop is false', () => {
renderComponent({ disabled: false });

expect(screen.getByTestId('unreceivable-button')).toBeEnabled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MarkUnreceivableActionMenuItem } from './MarkUnreceivableActionMenuItem';
Loading
Loading