diff --git a/src/components/Field.js b/src/components/Field.js new file mode 100644 index 00000000..1659c659 --- /dev/null +++ b/src/components/Field.js @@ -0,0 +1,23 @@ +import React from 'react'; +import './Field.css'; +import PropTypes from 'prop-types'; + +const Field = ({id, placeholder, value, onChangeCallback}) => { + return ( + 0 ? "" : "--invalid"}`} + id={id} + placeholder={placeholder} + type="text" + value={value} + onChange={onChangeCallback} /> + ); +} + +Field.propTypes = { + id: PropTypes.string.isRequired, + placeholder: PropTypes.string.isRequired, + value: PropTypes.string, + onChangeCallback: PropTypes.func.isRequired, +}; + +export default Field; \ No newline at end of file diff --git a/src/components/FinalPoem.js b/src/components/FinalPoem.js index d516184e..1ef567be 100644 --- a/src/components/FinalPoem.js +++ b/src/components/FinalPoem.js @@ -1,20 +1,28 @@ import React from 'react'; import './FinalPoem.css'; +import PropTypes from 'prop-types'; -const FinalPoem = (props) => { +const FinalPoem = ({isRevealed, poem, onButtonClickCallback}) => { return (
-
+ { isRevealed ?

Final Poem

- -
- + {poem.map((line, index) =>

{line}

)} +
: null } +
- + { isRevealed ? null : }
); } +FinalPoem.propTypes = { + isRevealed: PropTypes.bool.isRequired, + poem: PropTypes.array.isRequired, + onButtonClickCallback: PropTypes.func.isRequired, +}; + + export default FinalPoem; diff --git a/src/components/Game.js b/src/components/Game.js index ad27105b..d9ab4152 100644 --- a/src/components/Game.js +++ b/src/components/Game.js @@ -3,16 +3,60 @@ import './Game.css'; import PlayerSubmissionForm from './PlayerSubmissionForm'; import FinalPoem from './FinalPoem'; import RecentSubmission from './RecentSubmission'; +import poemFormat from './data/fields.json'; + +const FIELDS = poemFormat; + +//Establish the format of user data that gets stored +//We are only tracking poem fields that take user input +const submissionTemplate = () => { + const submissionKeys = {}; + for (let field of FIELDS) { + if (field.key) submissionKeys[field.key] = ""; + } + return submissionKeys; +} const Game = () => { + const [playerSubmission, setPlayerSubmission] = useState(submissionTemplate()); + const [poem, setPoem] = useState([]); + const [isRevealed, setIsRevealed] = useState(false); + + //pretty-print the format of a poem sentence const exampleFormat = FIELDS.map((field) => { - if (field.key) { - return field.placeholder; - } else { - return field; - } + return field.key ? field.placeholder : field }).join(" "); + //callback function to handle changes on text input fields + const handleChange = (event) => { + let updatedFields = {...playerSubmission}; + updatedFields[event.target.id] = event.target.value; + setPlayerSubmission(updatedFields); + } + + //callback function to store user's submitted poem sentence + const handleSubmit = (event) => { + event.preventDefault(); + + //returns the user's inputs in string format + const submittedLine = () => { + let submissionString = ""; + for (let field of FIELDS) { + field.key ? submissionString += ` ${(playerSubmission[field.key])}` : submissionString += ` ${field}`; + } + return submissionString.trim(); + }; + + let updatedPoem = [...poem]; + updatedPoem.push(submittedLine()); + setPoem(updatedPoem); + setPlayerSubmission(submissionTemplate()); + } + + const handleReveal = (event) => { + setIsRevealed(true); + } + return (

Game

@@ -25,45 +69,13 @@ const Game = () => { { exampleFormat }

- - - - - + {poem.length > 0 && !isRevealed ? : null } + { isRevealed ? null : } +
); } -const FIELDS = [ - "The", - { - key: 'adj1', - placeholder: 'adjective', - }, - { - key: 'noun1', - placeholder: 'noun', - }, - { - key: 'adv', - placeholder: 'adverb', - }, - { - key: 'verb', - placeholder: 'verb', - }, - "the", - { - key: 'adj2', - placeholder: 'adjective', - }, - { - key: 'noun2', - placeholder: 'noun', - }, - ".", -]; - export default Game; diff --git a/src/components/PlayerSubmissionForm.css b/src/components/PlayerSubmissionForm.css index 7cded5d9..155ca1cd 100644 --- a/src/components/PlayerSubmissionForm.css +++ b/src/components/PlayerSubmissionForm.css @@ -31,8 +31,8 @@ background-color: tomato; } -.PlayerSubmissionFormt__input--invalid { - background-color: #FFE9E9; +.PlayerSubmissionForm__input--invalid { + background-color: #f7ada3; } .PlayerSubmissionForm__input--invalid::placeholder { diff --git a/src/components/PlayerSubmissionForm.js b/src/components/PlayerSubmissionForm.js index ba19e6ef..2ef5acec 100644 --- a/src/components/PlayerSubmissionForm.js +++ b/src/components/PlayerSubmissionForm.js @@ -1,22 +1,30 @@ -import React, { useState } from 'react'; +import React from 'react'; import './PlayerSubmissionForm.css'; +import Field from './Field.js'; +import PropTypes from 'prop-types'; + +const generateSubmissionFields = (fields, current, onChangeCallback) => { + const submissionFields = fields.map((field, index) => field.key ? : {field} ); + + return submissionFields; +} + +const PlayerSubmissionForm = ({ fields, current, count, onChangeCallback, onSubmitCallback}) => { + const submissionFields = generateSubmissionFields(fields, current, onChangeCallback); -const PlayerSubmissionForm = () => { return (
-

Player Submission Form for Player #{ }

+

Player Submission Form for Player #{count}

-
+
- - { - // Put your form inputs here... We've put in one below as an example - } - - + {submissionFields}
@@ -27,5 +35,16 @@ const PlayerSubmissionForm = () => { ); } +PlayerSubmissionForm.propTypes = { + fields: PropTypes.array.isRequired, + current: PropTypes.object.isRequired, + count: PropTypes.number.isRequired, + onChangeCallback: PropTypes.func.isRequired, + onSubmitCallback: PropTypes.func.isRequired, +}; + + export default PlayerSubmissionForm; + + diff --git a/src/components/RecentSubmission.js b/src/components/RecentSubmission.js index 663da34b..41bc3aa3 100644 --- a/src/components/RecentSubmission.js +++ b/src/components/RecentSubmission.js @@ -1,13 +1,18 @@ import React from 'react'; import './RecentSubmission.css'; +import PropTypes from 'prop-types'; -const RecentSubmission = (props) => { +const RecentSubmission = ({line}) => { return (

The Most Recent Submission

-

{ }

+

{line}

); } +RecentSubmission.propTypes = { + line: PropTypes.string.isRequired, +}; + export default RecentSubmission; diff --git a/src/components/data/fields.json b/src/components/data/fields.json new file mode 100644 index 00000000..0085843b --- /dev/null +++ b/src/components/data/fields.json @@ -0,0 +1,29 @@ +[ + "The", + { + "key":"adj1", + "placeholder":"adjective" + }, + { + "key":"noun1", + "placeholder":"noun" + }, + { + "key":"adv", + "placeholder":"adverb" + }, + { + "key":"verb", + "placeholder":"verb" + }, + "the", + { + "key":"adj2", + "placeholder":"adjective" + }, + { + "key":"noun2", + "placeholder":"noun" + }, + "." +] \ No newline at end of file