-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIORGS-390 Implement logic for banking information management
- Loading branch information
1 parent
92d7a99
commit 924a07e
Showing
14 changed files
with
195 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import chunk from 'lodash/chunk'; | ||
import { useCallback } from 'react'; | ||
|
||
import { useShowCallout } from '@folio/stripes-acq-components'; | ||
|
||
import { useBankingInformationMutation } from '../common/hooks'; | ||
import { getArrayItemsChanges } from '../common/utils'; | ||
|
||
const execute = (fn, arr) => chunk(arr, 5).reduce((acc, chunked) => { | ||
return acc.then(() => Promise.all(chunked.map((bankingInformation) => fn({ bankingInformation })))); | ||
}, Promise.resolve()); | ||
|
||
export const useBankingInformationManager = () => { | ||
const showCallout = useShowCallout(); | ||
|
||
const { | ||
createBankingInformation, | ||
updateBankingInformation, | ||
deleteBankingInformation, | ||
isLoading, | ||
} = useBankingInformationMutation(); | ||
|
||
const manageBankingInformation = useCallback(({ | ||
initBankingInformation, | ||
bankingInformation, | ||
}) => { | ||
const { | ||
created, | ||
updated, | ||
deleted, | ||
} = getArrayItemsChanges(initBankingInformation, bankingInformation); | ||
|
||
return Promise.all([ | ||
execute(createBankingInformation, created), | ||
execute(updateBankingInformation, updated), | ||
execute(deleteBankingInformation, deleted), | ||
]).catch(() => { | ||
showCallout({ | ||
type: 'error', | ||
messageId: 'ui-organizations.bankingInformation.save.error', | ||
}); | ||
}); | ||
}, [showCallout]); | ||
Check warning on line 43 in src/Organizations/useBankingInformationManager.js GitHub Actions / github-actions-ci
|
||
|
||
return { | ||
manageBankingInformation, | ||
isLoading, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useBankingInformationMutation } from './useBankingInformationMutation'; |
52 changes: 52 additions & 0 deletions
52
src/common/hooks/useBankingInformationMutation/useBankingInformationMutation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useMutation } from 'react-query'; | ||
|
||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { BANKING_INFORMATION_API } from '../../constants'; | ||
|
||
export const useBankingInformationMutation = () => { | ||
const ky = useOkapiKy(); | ||
|
||
const { | ||
mutateAsync: createBankingInformation, | ||
isLoading: isBankingInformationCreateLoading, | ||
} = useMutation({ | ||
mutationFn: ({ bankingInformation }) => { | ||
return ky.post(BANKING_INFORMATION_API, { json: bankingInformation }).json(); | ||
}, | ||
}); | ||
|
||
const { | ||
mutateAsync: updateBankingInformation, | ||
isLoading: isBankingInformationUpdateLoading, | ||
} = useMutation({ | ||
mutationFn: ({ bankingInformation }) => { | ||
return ky.put( | ||
`${BANKING_INFORMATION_API}/${bankingInformation.id}`, | ||
{ json: bankingInformation }, | ||
).json(); | ||
}, | ||
}); | ||
|
||
const { | ||
mutateAsync: deleteBankingInformation, | ||
isLoading: isBankingInformationDeleteLoading, | ||
} = useMutation({ | ||
mutationFn: ({ bankingInformation }) => { | ||
return ky.delete(`${BANKING_INFORMATION_API}/${bankingInformation.id}`).json(); | ||
}, | ||
}); | ||
|
||
const isLoading = ( | ||
isBankingInformationCreateLoading | ||
|| isBankingInformationDeleteLoading | ||
|| isBankingInformationUpdateLoading | ||
); | ||
|
||
return { | ||
createBankingInformation, | ||
updateBankingInformation, | ||
deleteBankingInformation, | ||
isLoading, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/common/utils/getArrayItemsChanges/getArrayItemsChanges.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import isEqual from 'lodash/isEqual'; | ||
import keyBy from 'lodash/keyBy'; | ||
|
||
/** | ||
* Detects added, modified and deleted items in an array. | ||
* The function assumes that each element of the array is an object structure. | ||
*/ | ||
export const getArrayItemsChanges = (initialValues = [], values = []) => { | ||
const initialValuesMap = keyBy(initialValues, 'id'); | ||
const valuesMap = keyBy(values, 'id'); | ||
|
||
const { created, updated } = values.reduce((acc, item) => { | ||
const initItem = initialValuesMap[item.id]; | ||
|
||
if (!initItem) { | ||
acc.created.push(item); | ||
} else if (!isEqual(initItem, item)) { | ||
acc.updated.push(item); | ||
} | ||
|
||
return acc; | ||
}, { created: [], updated: [] }); | ||
|
||
const deleted = initialValues.reduce((acc, initItem) => { | ||
const item = valuesMap[initItem.id]; | ||
|
||
if (!item) { | ||
acc.push(initItem); | ||
} | ||
|
||
return acc; | ||
}, []); | ||
|
||
return { | ||
created, | ||
updated, | ||
deleted, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { getArrayItemsChanges } from './getArrayItemsChanges'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters