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

fix SelectUser component when there is no deskId #4657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scripts/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ng from 'core/services/ng';
import {IUser} from 'superdesk-api';
import {OrderedMap} from 'immutable';

function hasPrivilege(privilege: string): boolean {
const privileges = ng.get('privileges');
Expand All @@ -18,8 +20,13 @@ function isLoggedIn() {
return session?.identity?._id != null;
}

function getAll(): OrderedMap<IUser['_id'], IUser> {
return OrderedMap(ng.get('desks').users._items.map((user) => [user._id, user]));
}

export const user = {
hasPrivilege,
isLoggedIn,
getCurrentUserId,
getAll,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you export an OrderedMap<IUser['_id'], IUser]> from the API? So we also have a capability to get a user by ID without having to iterate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works quite well with vocabularies which I did a while ago https://github.com/superdesk/superdesk-client-core/blob/develop/scripts/api/vocabularies.ts#L97

When thinking more about it, I'm not sure whether it's better to expose an ordered map or simply have 2 methods - getAll that would return an array and getById that would return a single item - without involving immutable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to get the map, imo when you get all users it's usually rather for a lookup and not for listing them, this is rather an edge case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally it works great both - lookup and iteration. The only thing I'm wary about is developers being confused that getting all items when they only need one would be expensive in terms of performance when in reality it wouldn't.

};
6 changes: 4 additions & 2 deletions scripts/core/ui/components/SelectUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ export class SelectUser extends SuperdeskReactComponent<IPropsSelectUser, IState
inlineLabel={true}
labelHidden={true}
getItems={(searchString) => {
const deskMembers = sdApi.desks.getDeskMembers(this.props.deskId);
const users = this.props.deskId
? sdApi.desks.getDeskMembers(this.props.deskId)
: sdApi.user.getAll().toArray();

return Promise.resolve(searchUsers(deskMembers, searchString));
return Promise.resolve(searchUsers(users, searchString));
}}
value={this.state.selectedUser}
onChange={(user) => {
Expand Down
Loading