-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIIN-3175: Use AuditLogPane from streipes/components
- Loading branch information
1 parent
cbf12a8
commit e315f71
Showing
8 changed files
with
198 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Gets translated change type label | ||
* @param {function} formatMessage | ||
* @returns {{ADDED, MODIFIED, REMOVED}} | ||
*/ | ||
export const getActionLabel = formatMessage => { | ||
return { | ||
ADDED: formatMessage({ id: 'stripes-acq-components.audit-log.action.added' }), | ||
MODIFIED: formatMessage({ id: 'stripes-acq-components.audit-log.action.edited' }), | ||
REMOVED: formatMessage({ id: 'stripes-acq-components.audit-log.action.removed' }), | ||
}; | ||
}; |
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,15 @@ | ||
import { getActionLabel } from './getActionLabel'; | ||
|
||
const intl = { formatMessage: ({ id }) => id }; | ||
|
||
describe('getActionLabel', () => { | ||
it('should return correct action labels', () => { | ||
const labels = { | ||
ADDED: 'stripes-acq-components.audit-log.action.added', | ||
MODIFIED: 'stripes-acq-components.audit-log.action.edited', | ||
REMOVED: 'stripes-acq-components.audit-log.action.removed', | ||
}; | ||
|
||
expect(getActionLabel(intl.formatMessage)).toEqual(labels); | ||
}); | ||
}); |
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,28 @@ | ||
import { sortBy } from 'lodash'; | ||
|
||
/** | ||
* Merge fieldChanges and collectionChanges into a list of changed fields and sort by changeType | ||
* @param {Object} diff | ||
* @param {Array} diff.fieldChanges | ||
* @param {Array} diff.collectionChanges | ||
* @returns {Array.<{fieldName: String, changeType: String, newValue: any, oldValue: any}>} | ||
*/ | ||
export const getChangedFieldsList = diff => { | ||
const fieldChanges = diff.fieldChanges ? diff.fieldChanges.map(field => ({ | ||
fieldName: field.fieldName, | ||
changeType: field.changeType, | ||
newValue: field.newValue, | ||
oldValue: field.oldValue, | ||
})) : []; | ||
|
||
const collectionChanges = diff.collectionChanges ? diff.collectionChanges.flatMap(collection => { | ||
return collection.itemChanges.map(field => ({ | ||
fieldName: collection.collectionName, | ||
changeType: field.changeType, | ||
newValue: field.newValue, | ||
oldValue: field.oldValue, | ||
})); | ||
}) : []; | ||
|
||
return sortBy([...fieldChanges, ...collectionChanges], data => data.changeType); | ||
}; |
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 { default } from './useVersionHistory'; |
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,104 @@ | ||
import { | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
import { Link } from 'react-router-dom'; | ||
import { | ||
keyBy, | ||
uniq, | ||
} from 'lodash'; | ||
|
||
import { | ||
formatDateTime, | ||
useUsersBatch, | ||
} from '@folio/stripes-acq-components'; | ||
|
||
import { getChangedFieldsList } from './getChangedFieldsList'; | ||
import { getActionLabel } from './getActionLabel'; | ||
|
||
const useVersionHistory = (data, totalRecords) => { | ||
const intl = useIntl(); | ||
const anonymousUserLabel = intl.formatMessage({ id: 'stripes-components.versionHistory.anonymousUser' }); | ||
|
||
const [versions, setVersions] = useState([]); | ||
const [usersId, setUsersId] = useState([]); | ||
const [usersMap, setUsersMap] = useState({}); | ||
const [isLoadedMoreVisible, setIsLoadedMoreVisible] = useState(true); | ||
|
||
const { users } = useUsersBatch(usersId); | ||
|
||
// cleanup when component unmounts | ||
useEffect(() => () => { | ||
setVersions([]); | ||
setUsersMap({}); | ||
}, []); | ||
|
||
// update usersId when data changes | ||
useEffect(() => { | ||
if (!data?.length) return; | ||
|
||
const newUsersId = uniq(data.map(version => version.userId)); | ||
|
||
setUsersId(newUsersId); | ||
}, [data]); | ||
|
||
// update usersMap when new users are fetched | ||
useEffect(() => { | ||
if (!users?.length) return; | ||
|
||
setUsersMap(prevState => ({ | ||
...prevState, | ||
...keyBy(users, 'id'), | ||
})); | ||
}, [users]); | ||
|
||
useEffect(() => { | ||
if (!data?.length) return; | ||
|
||
setVersions(prevState => [...prevState, ...data]); | ||
}, [data]); | ||
|
||
useEffect(() => { | ||
setIsLoadedMoreVisible(versions.length < totalRecords); | ||
}, [versions]); | ||
|
||
const versionsToDisplay = useMemo( | ||
() => { | ||
const getUserName = userId => { | ||
const user = usersMap[userId]; | ||
|
||
return user ? `${user.personal.lastName}, ${user.personal.firstName}` : null; | ||
}; | ||
const getSourceLink = userId => { | ||
return userId ? <Link to={`/users/preview/${userId}`}>{getUserName(userId)}</Link> : anonymousUserLabel; | ||
}; | ||
|
||
const transformDiffToVersions = diffArray => { | ||
return diffArray | ||
.filter(({ action }) => action !== 'CREATE') | ||
.map(({ eventDate, eventTs, userId, eventId, diff }) => ({ | ||
eventDate: formatDateTime(eventDate, intl), | ||
source: getSourceLink(userId), | ||
userName: getUserName(userId) || anonymousUserLabel, | ||
fieldChanges: diff ? getChangedFieldsList(diff) : [], | ||
eventId, | ||
eventTs, | ||
})); | ||
}; | ||
|
||
return transformDiffToVersions(versions); | ||
}, [versions, usersMap], | ||
); | ||
|
||
const actionsMap = { ...getActionLabel(intl.formatMessage) }; | ||
|
||
return { | ||
actionsMap, | ||
isLoadedMoreVisible, | ||
versionsToDisplay, | ||
}; | ||
}; | ||
|
||
export default useVersionHistory; |