-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useEffect } from 'react'; | ||
import { useForm } from 'react-final-form'; | ||
|
||
import { RepeatableFieldWithValidation } from '@folio/stripes-acq-components'; | ||
|
||
import { EVENT_EMITTER_EVENTS } from '../../../../common/constants'; | ||
import { useEventEmitter } from '../../../../common/hooks'; | ||
import { getAddressCategoryIdsSet } from '../../getAddressCategoryIdsSet'; | ||
|
||
export const BankingInformationFieldArray = (props) => { | ||
const { fields } = props; | ||
Check failure on line 11 in src/Organizations/OrganizationForm/OrganizationBankingInfoForm/BankingInformationFieldArray/BankingInformationFieldArray.js GitHub Actions / github-actions-ci
|
||
const eventEmitter = useEventEmitter(); | ||
const { change, getFieldState } = useForm(); | ||
|
||
/* | ||
Handles organization addresses categories change. | ||
Resets banking information address category fields without initial value. | ||
*/ | ||
useEffect(() => { | ||
const eventType = EVENT_EMITTER_EVENTS.ADDRESS_CATEGORY_CHANGED; | ||
const callback = () => { | ||
const addressesCategoriesIdsMap = getAddressCategoryIdsSet(getFieldState('addresses').value); | ||
|
||
fields.forEach(field => { | ||
Check failure on line 24 in src/Organizations/OrganizationForm/OrganizationBankingInfoForm/BankingInformationFieldArray/BankingInformationFieldArray.js GitHub Actions / github-actions-ci
|
||
// TODO: change addres to categoty (id) | ||
const fieldName = `${field}.addressId`; | ||
const { initial, value } = getFieldState(fieldName); | ||
|
||
if (!addressesCategoriesIdsMap.has(value) && value !== initial) { | ||
change(fieldName, undefined); | ||
} | ||
}); | ||
}; | ||
|
||
eventEmitter.on(eventType, callback); | ||
|
||
return () => { | ||
eventEmitter.off(eventType, callback); | ||
}; | ||
}, [change, eventEmitter, fields, getFieldState]); | ||
|
||
return <RepeatableFieldWithValidation {...props} />; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { BankingInformationFieldArray } from './BankingInformationFieldArray' | ||
Check failure on line 1 in src/Organizations/OrganizationForm/OrganizationBankingInfoForm/BankingInformationFieldArray/index.js GitHub Actions / github-actions-ci
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import memoize from 'lodash/memoize'; | ||
|
||
export const getAddressCategoryIdsSet = memoize((addresses = []) => { | ||
return addresses.reduce((acc, address) => { | ||
address.categories?.forEach(categoryId => acc.add(categoryId)); | ||
|
||
return acc; | ||
}, new Set()); | ||
}) | ||
Check failure on line 9 in src/Organizations/OrganizationForm/getAddressCategoryIdsSet.js GitHub Actions / github-actions-ci
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const EVENT_EMITTER_EVENTS = { | ||
ADDRESS_CATEGORY_CHANGED: 'ADDRESS_CATEGORY_CHANGED', | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useEventEmitter } from './useEventEmitter'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { EventEmitter } from '../../utils'; | ||
|
||
const eventEmitter = new EventEmitter(); | ||
|
||
export const useEventEmitter = () => { | ||
return eventEmitter; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { renderHook } from '@folio/jest-config-stripes/testing-library/react'; | ||
|
||
import { EventEmitter } from '../../utils'; | ||
import { useEventEmitter } from './useEventEmitter'; | ||
|
||
describe('useEventEmitter', () => { | ||
it('should return event emitter instance', async () => { | ||
const { result } = renderHook(() => useEventEmitter()); | ||
|
||
expect(result.current).toBeInstanceOf(EventEmitter); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export class EventEmitter { | ||
constructor() { | ||
this.eventTarget = new EventTarget(); | ||
} | ||
|
||
on(eventName, callback) { | ||
this.eventTarget.addEventListener(eventName, callback); | ||
} | ||
|
||
off(eventName, callback) { | ||
this.eventTarget.removeEventListener(eventName, callback); | ||
} | ||
|
||
emit(eventName, data) { | ||
const event = new CustomEvent(eventName, { detail: data }); | ||
|
||
this.eventTarget.dispatchEvent(event); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { EventEmitter } from './EventEmitter'; | ||
|
||
const EVENT_TYPE = 'test-event-type'; | ||
const callback = jest.fn(); | ||
const payload = 'Test payload'; | ||
|
||
describe('EventEmitter', () => { | ||
let emitter; | ||
|
||
beforeEach(() => { | ||
emitter = new EventEmitter(); | ||
callback.mockClear(); | ||
}); | ||
|
||
it('should add and invoke event listeners', () => { | ||
emitter.on(EVENT_TYPE, callback); | ||
emitter.emit(EVENT_TYPE, payload); | ||
|
||
expect(callback).toHaveBeenCalledWith(expect.objectContaining({ detail: payload })); | ||
}); | ||
|
||
it('should remove event listeners', () => { | ||
emitter.on(EVENT_TYPE, callback); | ||
emitter.off(EVENT_TYPE, callback); | ||
emitter.emit(EVENT_TYPE, payload); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should emit events with the correct data', () => { | ||
emitter.on(EVENT_TYPE, callback); | ||
emitter.emit(EVENT_TYPE, payload); | ||
|
||
expect(callback).toHaveBeenCalledWith(expect.objectContaining({ detail: payload })); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EventEmitter } from './EventEmitter'; |