Skip to content

Commit

Permalink
Merge pull request #289 from Arquisoft/fix/webapp/dashboard
Browse files Browse the repository at this point in the history
Fixed a bug in the custom game in the dashboard
  • Loading branch information
sergiorodriguezgarcia authored Apr 25, 2024
2 parents 320e15a + 2a2db08 commit c008391
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 44 deletions.
79 changes: 37 additions & 42 deletions webapp/src/components/dashboard/CustomGameMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,68 @@ import { newGame, gameCategories } from 'components/game/Game';

const CustomGameMenu = ({ isOpen, onClose }) => {
const navigate = useNavigate();

const [selectedCategories, setSelectedCategories] = useState([]);
const [allCategories, setAllCategories] = useState([]);
const [rounds, setRounds] = useState(9);
const [time, setTime] = useState(20);
const [categories, setCategories] = useState([]);

const { t, i18n } = useTranslation();

useEffect(() => {
async function fetchCategories() {
try {
let lang = i18n.language;
if (lang.includes("en")) {
lang = "en";
} else if (lang.includes("es")) {
lang = "es"
} else {
lang = "en";
}

const lang = getNormalizedLanguage(i18n.language);
const categoriesData = (await gameCategories(lang)).data;
const formattedCategories = categoriesData.map(category => category.name);
setCategories(formattedCategories);
setAllCategories(categoriesData.map(category => category));
} catch (error) {
console.error("Error fetching game categories:", error);
}
}
fetchCategories();
}, [i18n.language]);

const getNormalizedLanguage = (language) => {
if (language.includes("en"))
return "en";
else if (language.includes("es"))
return "es";
else
return "en";
};

const manageCategory = (category) => {
if (selectedCategories.includes(category)) {
setSelectedCategories(selectedCategories.filter(item => item !== category));
} else {
setSelectedCategories([...selectedCategories, category]);
}
if (selectedCategories.includes(category.internal_representation))
setSelectedCategories(selectedCategories.filter(item => item !== category.internal_representation));
else
setSelectedCategories([...selectedCategories, category.internal_representation]);
};

const initializeCustomGameMode = async () => {
try {
let lang = i18n.language;
if (lang.includes("en")) {
lang = "en";
} else if (lang.includes("es")) {
lang = "es"
} else {
lang = "en";
}
const lang = getNormalizedLanguage(i18n.language);

const gamemode = 'CUSTOM';
let uppercaseCategories = selectedCategories.map(category => category.toUpperCase());
if (uppercaseCategories.length === 0) {
uppercaseCategories = ["GEOGRAPHY", "SPORTS", "MUSIC", "ART", "VIDEOGAMES"];
}


let categoriesCustom = selectedCategories;
if (categoriesCustom.length === 0)
categoriesCustom = allCategories.map(category => category.internal_representation);
const customGameDto = {
rounds: rounds,
categories: uppercaseCategories,
categories: categoriesCustom,
round_duration: time

}

const newGameResponse = await newGame(lang, gamemode, customGameDto);
if (newGameResponse) {
navigate("/dashboard/game");
}
} catch (error) {

if (newGameResponse)
navigate("/dashboard/game");

} catch (error) {
console.error("Error initializing game:", error);
}
};
}
};

return (
<Drawer isOpen={isOpen} placement="right" onClose={onClose}>
Expand Down Expand Up @@ -112,16 +107,16 @@ const CustomGameMenu = ({ isOpen, onClose }) => {
<Box marginTop="2em">
<Text color={"forest_green.500"}>{t("game.categories")}</Text>
<Flex direction="column">
{categories.map(category => (
{allCategories.map(category => (
<Button
key={category}
key={category.name}
className={"custom-button effect2"}
variant={selectedCategories.includes(category) ? "solid" : "outline"}
variant={selectedCategories.includes(category.internal_representation) ? "solid" : "outline"}
colorScheme="green"
margin={"10px"}
onClick={() => manageCategory(category)}
>
{category}
{category.name}
</Button>
))}
</Flex>
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/components/game/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export async function getCurrentGame() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/play");
}

export async function gameCategories() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/question-categories");
export async function gameCategories(lang) {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/question-categories?lang=" + lang);
}

export async function gameModes() {
Expand Down

0 comments on commit c008391

Please sign in to comment.