Skip to content

Added sorting the list of opponents in the "Choose opponent": online users appear before offline users. #2145

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,68 @@ const unchosenTask = { id: null };
const OpponentSelect = memo(({ setOpponent, opponent }) => {
const dispatch = useDispatch();
const currentUserId = useSelector(selectors.currentUserIdSelector);
const { presenceList } = useSelector(selectors.lobbyDataSelector);

const loadOptions = useCallback(
(inputValue, callback) => {
const queryParamsString = qs.stringify({
q: {
name_ilike: inputValue,
},
});

const loadOptions = useCallback((inputValue, callback) => {
const queryParamsString = qs.stringify({
q: {
name_ilike: inputValue,
},
});
axios
.get(`/api/v1/users?${queryParamsString}`)
.then(({ data }) => {
const { users: apiUsers } = camelizeKeys(data);
const filteredApiUsers = apiUsers.filter(
({ id }) => id !== currentUserId,
);
const onlineUsersFromPresence = presenceList
.map(p => p.user)
.filter(user => user.id !== currentUserId);
const combinedUsersMap = new Map();

axios
.get(`/api/v1/users?${queryParamsString}`)
.then(({ data }) => {
const { users } = camelizeKeys(data);
filteredApiUsers.forEach(user => {
const isOnline = presenceList.some(
presence => String(presence.id) === String(user.id),
);
combinedUsersMap.set(user.id, { ...user, online: isOnline });
});

const options = users
.filter(({ id }) => currentUserId !== id)
.map(user => ({ label: <UserLabel user={user} />, value: user }));
onlineUsersFromPresence.forEach(onlineUser => {
if (!combinedUsersMap.has(onlineUser.id)) {
combinedUsersMap.set(onlineUser.id, {
...onlineUser,
online: true,
});
}
});

callback(options);
})
.catch(error => {
dispatch(actions.setError(error));
});
}, [currentUserId, dispatch]);
const combinedUsers = Array.from(combinedUsersMap.values());

const sortedUsers = combinedUsers.sort((a, b) => {
const aOnline = a.online;
const bOnline = b.online;
if (aOnline === bOnline) {
return 0;
}
return aOnline ? -1 : 1;
});

const options = sortedUsers.map(user => ({
label: <UserLabel user={user} />,
value: user,
}));

callback(options);
})
.catch(error => {
dispatch(actions.setError(error));
});
},
[currentUserId, dispatch, presenceList],
);

return (
<AsyncSelect
Expand Down Expand Up @@ -135,7 +174,9 @@ const GameTypeButtonGroup = memo(({ value, onChange }) => {

function CreateGameDialog({ hideModal }) {
const dispatch = useDispatch();
const { gameOptions: givenGameOptions, opponentInfo } = useSelector(selectors.modalSelector);
const { gameOptions: givenGameOptions, opponentInfo } = useSelector(
selectors.modalSelector,
);
const [opponent, setOpponent] = useState(opponentInfo);
const [chosenTask, setChosenTask] = useState(unchosenTask);
const [chosenTags, setChosenTags] = useState([]);
Expand All @@ -148,13 +189,19 @@ function CreateGameDialog({ hideModal }) {
const isInvite = gameType === 'invite';
const isTaskChosen = chosenTask.id !== null;

const handleTimeoutChange = useCallback(e => setGameTimeout(e.target.value * 60), [setGameTimeout]);
const handleTimeoutChange = useCallback(
e => setGameTimeout(e.target.value * 60),
[setGameTimeout],
);

const switchGameLevel = useCallback(level => {
setGameLevel(level);
setChosenTask(unchosenTask);
setChosenTags([]);
}, [setGameLevel, setChosenTask, setChosenTags]);
const switchGameLevel = useCallback(
level => {
setGameLevel(level);
setChosenTask(unchosenTask);
setChosenTags([]);
},
[setGameLevel, setChosenTask, setChosenTags],
);

const createGame = () => {
if (isInvite && opponent) {
Expand Down
Loading