Skip to content

Commit

Permalink
Prepare data
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-karlsson committed Mar 22, 2024
1 parent 66ab3b5 commit 06e3a84
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
4 changes: 2 additions & 2 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function App() {
width: "100vw",
};

const enterSetup = () => {
function enterSetup() {
setCurrentView("setup");
};
}

return (
<div
Expand Down
17 changes: 12 additions & 5 deletions client/src/components/FoodButton.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React, { useState } from "react";

function FoodButton({ name }) {
function FoodButton({ name, onSelectFood, onDeselectFood }) {
const [isSelected, setIsSelected] = useState(false);

const imageUrl = `/images/foods/${name}.png`;

function selectFood() {
console.log("Food selected!");
setIsSelected(!isSelected); // Toggle the isSelected state
function handleClickFood() {
if (!isSelected) {
// Select food
onSelectFood(name);
} else {
// Deselect food
onDeselectFood(name);
}

setIsSelected(!isSelected);
}

const buttonStyle = {
Expand Down Expand Up @@ -41,7 +48,7 @@ function FoodButton({ name }) {
src={imageUrl}
alt={name}
style={imageStyle}
onClick={selectFood}
onClick={handleClickFood}
/>
</div>
);
Expand Down
9 changes: 6 additions & 3 deletions client/src/components/NameInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ function NameInput(props) {
}
}

function enterCouncil() {
function enterSetup() {
if (name) {
props.onEnterSetup();

// Save name to local storage
localStorage.setItem("name", name);
} else {
console.log("No name...");
setIsNameMissing(true);
Expand All @@ -31,7 +34,7 @@ function NameInput(props) {

function handleKeyDown(e) {
if (e.key === "Enter") {
enterCouncil();
enterSetup();
}
}

Expand All @@ -49,7 +52,7 @@ function NameInput(props) {
<FontAwesomeIcon
icon={faArrowRight}
className="input-arrow"
onClick={enterCouncil}
onClick={enterSetup}
/>
</div>
<h3 className={`sub-sub-header ${!isNameMissing ? "hidden" : ""}`}>
Expand Down
39 changes: 38 additions & 1 deletion client/src/components/Setup.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React from "react";
import React, { useState } from "react";
import FoodButton from "./FoodButton";

function Setup() {
const [topic, setTopic] = useState(""); // Added state for the topic
const [selectedFoods, setSelectedFoods] = useState([]);

const foods = [
"banana",
"bratwurst",
Expand All @@ -25,6 +28,22 @@ function Setup() {

function enterCouncil() {
console.log("Entering council...");

console.log("Name: ", localStorage.getItem("name"));
console.log("Topic: ", topic);
console.log("Selected foods: ", selectedFoods);

// First test call socket

// let promptsAndOptions = {options: , topic: , characters: }

// socket.emit("start_conversation", promptsAndOptions);

// return {
// options: promptsAndOptions.options,
// topic: promptsAndOptions.rooms[currentRoom].topic,
// characters: promptsAndOptions.rooms[currentRoom].characters,
// };
}

function handleKeyDown(e) {
Expand All @@ -33,6 +52,21 @@ function Setup() {
}
}

function selectFood(foodName) {
// Check if the foodName is already selected to prevent duplicates
if (!selectedFoods.includes(foodName)) {
setSelectedFoods([...selectedFoods, foodName]);
console.log(`SELECTED ${foodName}`);
}
}

function deselectFood(foodName) {
// Filter out the foodName from the selectedFoods array to remove it
const filteredFoods = selectedFoods.filter((food) => food !== foodName);
setSelectedFoods(filteredFoods);
console.log(`DESELECTED ${foodName}`);
}

return (
<div style={welcomeStyle}>
<div>
Expand All @@ -52,6 +86,7 @@ function Setup() {
rows="2"
cols="30"
onKeyDown={handleKeyDown}
onChange={(e) => setTopic(e.target.value)} // Update the state of the topic when input changes
></textarea>
</div>
<div>
Expand All @@ -61,6 +96,8 @@ function Setup() {
<FoodButton
key={food}
name={food}
onSelectFood={selectFood}
onDeselectFood={deselectFood}
/>
))}
</div>
Expand Down

0 comments on commit 06e3a84

Please sign in to comment.