Skip to content

Commit

Permalink
Fixed out of order bug and improved some ux
Browse files Browse the repository at this point in the history
  • Loading branch information
Rice-Tech committed Jan 28, 2024
1 parent 3d89252 commit a655b76
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 42 deletions.
48 changes: 29 additions & 19 deletions src/components/StoryEngine.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useState, useEffect } from "react";
import { useState } from "react";
import StoryTemplate from "./StoryTemplate";
import WordInputs from "./WordInputs";
import { Button } from "./ui/button";


interface Props {
templateProp: string;
wordsList: WordInput[];
Expand All @@ -13,6 +11,9 @@ export interface WordInput {
word: string;
partOfSpeech: string;
refPath?: string;
user?: string;
index: number;
status?: string;
}

export const parseTemplate = (template: string): WordInput[] => {
Expand All @@ -29,27 +30,29 @@ export const parseTemplate = (template: string): WordInput[] => {

const StoryEngine = ({ templateProp, wordsList }: Props) => {
const [template /*, setTemplate*/] = useState<string>(templateProp);
const [wordInputs, setWordInputs] = useState<WordInput[]>(wordsList);
//const [wordInputs, setWordInputs] = useState<WordInput[]>(wordsList);
const [revealIndex, setRevealIndex] = useState(0);
const [showStory, setShowStory] = useState(false);

const handleInputChange = (
e: React.ChangeEvent<HTMLInputElement>,
index: number
) => {
const newWordInputs = [...wordInputs];
newWordInputs[index].word = e.target.value;
setWordInputs(newWordInputs);
};
// const handleInputChange = (
// e: React.ChangeEvent<HTMLInputElement>,
// index: number
// ) => {
// const newWordInputs = [...wordInputs];
// newWordInputs[index].word = e.target.value;
// setWordInputs(newWordInputs);
// };

useEffect(() => {
setWordInputs(parseTemplate(template));
}, [template]);
// useEffect(() => {
// setWordInputs(parseTemplate(template));
// }, [template]);

return (
<div className=" flex-col">
<WordInputs wordsList={wordsList} onChange={handleInputChange} />

{/* <WordInputs wordsList={wordsList} onChange={handleInputChange} /> */}
<h3>
Recieved {wordsList.filter((item) => item.status == "submitted").length}
</h3>
{showStory && (
<>
<StoryTemplate
Expand All @@ -58,7 +61,10 @@ const StoryEngine = ({ templateProp, wordsList }: Props) => {
revealIndex={revealIndex}
/>{" "}
<br />
<Button className="m-auto" onClick={() => setRevealIndex(revealIndex + 1)}>
<Button
className="m-auto"
onClick={() => setRevealIndex(revealIndex + 1)}
>
Reveal Next Word
</Button>
{/*
Expand All @@ -71,7 +77,11 @@ const StoryEngine = ({ templateProp, wordsList }: Props) => {
></Input> */}
</>
)}
<Button className="m-auto" id="revealStoryButton" onClick={() => setShowStory(!showStory)}>
<Button
className="m-auto"
id="revealStoryButton"
onClick={() => setShowStory(!showStory)}
>
{showStory ? "Hide the Story" : "Reveal the Story"}
</Button>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/components/StoryTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const StoryTemplate = ({ template, words, revealIndex }: Props) => {
: "filledWord hiddenWord"
}
>
{words[wordIndex].word}{" "}
{
words.filter((word) => {
return word.index == wordIndex;
})[0].word
}{" "}
<div className="posTooltip" hidden>
{part}
</div>
Expand Down
30 changes: 15 additions & 15 deletions src/components/WordInputs.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { WordInput } from "./StoryEngine"
import { WordInput } from "./StoryEngine";
import { Input } from "./ui/input";

interface Props{
wordsList:WordInput[];
onChange: (e:React.ChangeEvent<HTMLInputElement>, index:number)=>void;
interface Props {
wordsList: WordInput[];
onChange: (e: React.ChangeEvent<HTMLInputElement>, index: number) => void;
}
const WordInputs = ({wordsList, onChange}:Props) => {
const WordInputs = ({ wordsList, onChange }: Props) => {
return (
<div className="wordInputs">
{wordsList.map((input, index) => (
<div key={index}>
<label>
{wordsList
.sort((a, b) => a.index - b.index)
.map((input, index) => (
<label key={index}>
{input.partOfSpeech}:
<Input
id={"word" + index}
type="text"
value={input.word}
onChange={(e) => onChange(e, index)}
/>
</label>
<br />
</div>
))}
</div>
)
}
))}
</div>
);
};

export default WordInputs
export default WordInputs;
22 changes: 17 additions & 5 deletions src/routes/HostGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import StoryEngine, {
import madlib from "../assets/madlibs.json";
import { useAuth } from "../contexts/AuthContext";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";

const HostGame = () => {
const params = useParams();
Expand Down Expand Up @@ -55,6 +61,8 @@ const HostGame = () => {
word: data.word,
partOfSpeech: data.partOfSpeech,
refPath: doc.ref.path,
index: data.index,
status: data.status,
});
});
setWordsList(wordSnaps);
Expand Down Expand Up @@ -86,7 +94,7 @@ const HostGame = () => {
doc(db, ref),
{
status: newRoomStatus,
storyTemplate: madlib[0].template,
storyTemplate: madlib[1].template,
},
{ merge: true }
);
Expand All @@ -95,20 +103,22 @@ const HostGame = () => {

updateRoomStatus("play");
setGameStatus("play");
const newWordsList = parseTemplate(madlib[0].template);
const newWordsList = parseTemplate(madlib[1].template);
console.table(newWordsList);
if (!newWordsList.length) {
return;
}
const ref = `rooms/${params.gameId}/words`;
newWordsList.forEach((wordInput, index) => {
const assignedUser = users[index % users.length];
console.log(wordInput.word, index)
addDoc(collection(db, ref), {
word: wordInput.word,
partOfSpeech: wordInput.partOfSpeech,
timeAdded: new Date().getTime(),
status: "new",
user: assignedUser,
index: wordInput.index,
});
});
};
Expand Down Expand Up @@ -153,11 +163,13 @@ const HostGame = () => {
<Card>
<CardHeader>
<CardTitle>Story</CardTitle>
<CardDescription>Players Choose Words and then a story is created here!</CardDescription>
<CardDescription>
Players Choose Words and then a story is created here!
</CardDescription>
</CardHeader>
<CardContent>
<StoryEngine
templateProp={madlib[0].template || "Error Loading Template"}
templateProp={madlib[1].template || "Error Loading Template"}
wordsList={wordsList}
/>
</CardContent>
Expand Down
15 changes: 13 additions & 2 deletions src/routes/PlayGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const PlayGame = () => {
const params = useParams();
const { currentUser } = useAuth();
const [wordAssignments, setWordAssignments] = useState<WordInput[]>([]);
const [status, setStatus] = useState("collectwords");
useEffect(() => {
if (!currentUser) {
console.log(
Expand Down Expand Up @@ -53,6 +54,9 @@ const PlayGame = () => {
word: data.word,
partOfSpeech: data.partOfSpeech,
refPath: doc.ref.path,
index: data.index,
status: data.status,
user: data.user,
});
});
setWordAssignments(wordSnaps);
Expand All @@ -70,7 +74,11 @@ const PlayGame = () => {
index: number
) => {
const newWordInputs = [...wordAssignments];
newWordInputs[index].word = e.target.value;
newWordInputs.filter((item) => {
console.table(item);
console.log(index);
return item.index == index;
})[0].word = e.target.value;
setWordAssignments(newWordInputs);
};
const handleSubmitWords = async () => {
Expand All @@ -87,6 +95,7 @@ const PlayGame = () => {
{ merge: true }
);
});
setStatus("submitted");
};
return (
<div>
Expand All @@ -100,7 +109,9 @@ const PlayGame = () => {
wordsList={wordAssignments}
onChange={handleInputChange}
/>
<Button onClick={handleSubmitWords}>Submit words</Button>
<Button onClick={handleSubmitWords}>
{status == "submitted" ? "Resubmit" : "Submit words"}
</Button>
</CardContent>
</Card>
</div>
Expand Down

0 comments on commit a655b76

Please sign in to comment.