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

task choice improvement #1178

Merged
merged 9 commits into from Nov 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions services/app/assets/css/style.scss
Expand Up @@ -616,3 +616,13 @@ td {
font-size: 16px;
}
}

.tag-btn-outline-orange {
border-color: $orange;
color: $orange;
}

.tag-btn-outline-orange:hover {
border-color: $orange;
color: $orange;
}
32 changes: 32 additions & 0 deletions services/app/assets/js/__mocks__/react-select.jsx
@@ -0,0 +1,32 @@
import React, { useState } from 'react';

const Select = ({ options, onChange, filterOption }) => {
const [selectInput, setSelectInput] = useState('task');

return (
<div>
{options
.filter(option => (
filterOption({ value: { name: option.value.name } }, selectInput)
))
.map(option => (
<button
type="button"
onClick={() => onChange({ value: option.value })}
key={option.value.name}
>
{option.value.name}
</button>
))}
<button
type="button"
onClick={() => setSelectInput('nAme')}
key="filterOption"
>
filter tasks by name
</button>
</div>
);
};

export default Select;
25 changes: 21 additions & 4 deletions services/app/assets/js/__tests__/LobbyWidget.test.jsx
Expand Up @@ -42,6 +42,7 @@ jest.mock(
const gonParams = {
local: 'en',
current_user: { id: 1, sound_settings: {} },
task_tags: ['math', 'string', 'rest'],
};
return { getAsset: type => gonParams[type] };
},
Expand All @@ -50,15 +51,17 @@ jest.mock(

jest.mock('axios');
const tasks = [
{ name: 'task1 name', id: 1 },
{ name: 'task2 name', id: 2 },
{ name: 'task1 name', id: 1, tags: [''] },
{ name: 'task2 name', id: 2, tags: [''] },
{ name: 'task3 filtered', id: 3, tags: [''] },
];
const users = [{ name: 'user1', id: -4 }, { name: 'user2', id: -2 }];
axios.get.mockResolvedValue({ data: { tasks, users } });

jest.mock('react-select');
jest.mock('react-select/async');
/*
AsyncSelect component mock is made by means of the series of buttons.
AsyncSelect and Select component mock is made by means of the series of buttons.
Each button represents one option.
Clicking the buttons you simulate a choice of the options in the AsyncSelect component.
*/
Expand Down Expand Up @@ -199,6 +202,7 @@ test('test task choice', async () => {
opponent_type: 'other_user',
timeout_seconds: 480,
task_id: null,
tags: [],
};

expect(lobbyMiddlewares.createGame).toHaveBeenCalledWith(params);
Expand Down Expand Up @@ -235,4 +239,17 @@ test('test task choice', async () => {
task_id: 1,
};
expect(mainMiddlewares.createInvite).toHaveBeenCalledWith(paramsWithOpponentAndChosenTask);
});

fireEvent.click(await findByRole('button', { name: 'Create a Game' }));

const firstTaskOpt = await findByRole('button', { name: 'task1 name' });
const secondTaskOpt = getByRole('button', { name: 'task2 name' });
const thirdTaskOpt = getByRole('button', { name: 'task3 filtered' });

fireEvent.click(getByRole('button', { name: 'filter tasks by name' }));

expect(firstTaskOpt).toBeInTheDocument();
expect(secondTaskOpt).toBeInTheDocument();
expect(thirdTaskOpt).not.toBeInTheDocument();
},
8000);
56 changes: 12 additions & 44 deletions services/app/assets/js/widgets/components/Game/CreateGameDialog.jsx
Expand Up @@ -14,6 +14,7 @@ import * as lobbyMiddlewares from '../../middlewares/Lobby';
import * as mainMiddlewares from '../../middlewares/Main';
import i18n from '../../../i18n';
import levelRatio from '../../config/levelRatio';
import TaskChoice from './TaskChoice';

const TIMEOUTS = [3300, 2040, 1260, 780, 480, 300, 180, 120, 60];

Expand Down Expand Up @@ -81,46 +82,6 @@ const OpponentSelect = ({ setOpponent, opponent }) => {
);
};

const TaskLabel = ({ task }) => (
<span className="text-truncate">
<span>{task.name}</span>
</span>
);

const TaskSelect = ({ chosenTask, setChosenTask, randomTask }) => {
const dispatch = useDispatch();

const loadOptions = (inputValue, callback) => {
axios
.get('/api/v1/tasks')
.then(({ data }) => {
const { tasks } = camelizeKeys(data);
const options = [randomTask, ...tasks]
.map(task => ({ label: <TaskLabel task={task} />, value: task }));

callback(options);
})
.catch(error => {
dispatch(actions.setError(error));
});
};

return (
<AsyncSelect
className="w-100"
value={
{
label: <TaskLabel task={chosenTask} />,
value: chosenTask,
}
}
defaultOptions
onChange={({ value }) => setChosenTask(value)}
loadOptions={loadOptions}
/>
);
};

const CreateGameDialog = ({ hideModal }) => {
const dispatch = useDispatch();

Expand All @@ -137,6 +98,7 @@ const CreateGameDialog = ({ hideModal }) => {

const randomTask = { name: i18n.t('random task'), value: {} };
const [chosenTask, setChosenTask] = useState(randomTask);
const [chosenTags, setChosenTags] = useState([]);

const [game, setGame] = useState({
level: gameLevels[0],
Expand Down Expand Up @@ -165,6 +127,7 @@ const CreateGameDialog = ({ hideModal }) => {
timeout_seconds: game.timeoutSeconds,
recipient_id: opponent.id,
task_id: _.get(chosenTask, 'id', null),
task_tags: chosenTags,
}),
);
} else if (!isInvite) {
Expand All @@ -173,6 +136,7 @@ const CreateGameDialog = ({ hideModal }) => {
opponent_type: game.type,
timeout_seconds: game.timeoutSeconds,
task_id: _.get(chosenTask, 'id', null),
task_tags: chosenTags,
});
}
hideModal();
Expand Down Expand Up @@ -244,10 +208,14 @@ const CreateGameDialog = ({ hideModal }) => {
</div>
</>
)}
<h5>{i18n.t('Choose task')}</h5>
<div className="d-flex justify-content-around px-5 mt-3 mb-2">
<TaskSelect setChosenTask={setChosenTask} chosenTask={chosenTask} randomTask={randomTask} />
</div>
<TaskChoice
chosenTask={chosenTask}
setChosenTask={setChosenTask}
chosenTags={chosenTags}
setChosenTags={setChosenTags}
level={game.level}
randomTask={randomTask}
/>
<button type="button" className={createBtnClassname} onClick={createGame}>
{createBtnTitle}
</button>
Expand Down