Commit 71bb4cc4 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Make sure selected user is live updated on export pannel

parent b06d0d16
...@@ -116,10 +116,17 @@ function ExportAnnotations({ ...@@ -116,10 +116,17 @@ function ExportAnnotations({
}), }),
[exportableAnnotations], [exportableAnnotations],
); );
const [selectedUser, setSelectedUser] = useState(
// Try to preselect current user // Try to preselect current user
userList.find(userInfo => userInfo.userid === currentUser) ?? const [selectedUserId, setSelectedUserId] = useState(currentUser);
// Re-compute selectedUserAnnotations if:
// - `userList` changes: This means annotations where created/deleted/updated
// - `selectedUserId` changes: This means user was manually changed
const selectedUserAnnotations = useMemo(
() =>
userList.find(user => user.userid === selectedUserId) ??
allAnnotationsOption, allAnnotationsOption,
[allAnnotationsOption, selectedUserId, userList],
); );
const exportFormatsEnabled = store.isFeatureEnabled('export_formats'); const exportFormatsEnabled = store.isFeatureEnabled('export_formats');
...@@ -143,7 +150,7 @@ function ExportAnnotations({ ...@@ -143,7 +150,7 @@ function ExportAnnotations({
const buildExportContent = useCallback( const buildExportContent = useCallback(
(format: ExportFormat['value']): string => { (format: ExportFormat['value']): string => {
const annotationsToExport = const annotationsToExport =
selectedUser?.annotations ?? exportableAnnotations; selectedUserAnnotations?.annotations ?? exportableAnnotations;
switch (format) { switch (format) {
case 'json': { case 'json': {
const data = annotationsExporter.buildJSONExportContent( const data = annotationsExporter.buildJSONExportContent(
...@@ -194,7 +201,7 @@ function ExportAnnotations({ ...@@ -194,7 +201,7 @@ function ExportAnnotations({
exportableAnnotations, exportableAnnotations,
group?.name, group?.name,
profile, profile,
selectedUser?.annotations, selectedUserAnnotations?.annotations,
], ],
); );
const exportAnnotations = useCallback( const exportAnnotations = useCallback(
...@@ -270,21 +277,26 @@ function ExportAnnotations({ ...@@ -270,21 +277,26 @@ function ExportAnnotations({
Select which user{"'"}s annotations to export: Select which user{"'"}s annotations to export:
</label> </label>
<SelectNext <SelectNext
value={selectedUser} value={selectedUserId}
onChange={setSelectedUser} onChange={setSelectedUserId}
buttonId={userSelectId} buttonId={userSelectId}
buttonContent={ buttonContent={
<UserAnnotationsListItem userAnnotations={selectedUser} /> <UserAnnotationsListItem
userAnnotations={selectedUserAnnotations}
/>
} }
data-testid="user-select" data-testid="user-select"
> >
<SelectNext.Option value={allAnnotationsOption}> <SelectNext.Option value={undefined}>
<UserAnnotationsListItem <UserAnnotationsListItem
userAnnotations={allAnnotationsOption} userAnnotations={allAnnotationsOption}
/> />
</SelectNext.Option> </SelectNext.Option>
{userList.map(userInfo => ( {userList.map(userInfo => (
<SelectNext.Option key={userInfo.userid} value={userInfo}> <SelectNext.Option
key={userInfo.userid}
value={userInfo.userid}
>
<UserAnnotationsListItem userAnnotations={userInfo} /> <UserAnnotationsListItem userAnnotations={userInfo} />
</SelectNext.Option> </SelectNext.Option>
))} ))}
......
...@@ -210,11 +210,12 @@ describe('ExportAnnotations', () => { ...@@ -210,11 +210,12 @@ describe('ExportAnnotations', () => {
assert.equal(users.length, userEntries.length); assert.equal(users.length, userEntries.length);
for (const [i, entry] of userEntries.entries()) { for (const [i, entry] of userEntries.entries()) {
const value = users.at(i).prop('value'); const user = users.at(i);
const value = user.prop('value');
const text = user.text();
assert.equal(value.userid, entry.userid); assert.equal(value, entry.userid);
assert.equal(value.displayName, entry.displayName); assert.equal(text, `${entry.displayName}${entry.annotationsCount}`);
assert.equal(value.annotations.length, entry.annotationsCount);
} }
}); });
}); });
...@@ -376,14 +377,8 @@ describe('ExportAnnotations', () => { ...@@ -376,14 +377,8 @@ describe('ExportAnnotations', () => {
// Select the user whose annotations we want to export // Select the user whose annotations we want to export
const userList = await waitForTestId(wrapper, 'user-select'); const userList = await waitForTestId(wrapper, 'user-select');
const option = userList
.find(SelectNext.Option)
.filterWhere(
option => option.prop('value').userid === 'acct:john@example.com',
)
.first();
act(() => { act(() => {
userList.prop('onChange')(option.prop('value')); userList.prop('onChange')('acct:john@example.com');
}); });
wrapper.update(); wrapper.update();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment