Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
8541100
implemented PlayerSubmissionForm
Galaxylaughing Dec 11, 2019
f4e90af
swapped order of imports at the top to keep local included files last
Galaxylaughing Dec 11, 2019
9b7dabd
implemented displaying of all poem lines in FinalPoem component
Galaxylaughing Dec 11, 2019
cac3b80
added periods to final poem lines
Galaxylaughing Dec 11, 2019
2087bfc
added validation to submission form
Galaxylaughing Dec 11, 2019
9bac1f1
implemented most recent submission component and hid it if no recent …
Galaxylaughing Dec 11, 2019
934c2df
hide poem unless 'show poem' is clicked
Galaxylaughing Dec 11, 2019
b17e76f
hides last submission and submission form when you click 'reveal poem'
Galaxylaughing Dec 11, 2019
d57e42b
fixed final poem to only show header and border after clicking 'revea…
Galaxylaughing Dec 11, 2019
9927f86
fixed final poem to hide 'reveal poem' button after showing final poem
Galaxylaughing Dec 11, 2019
5148ab6
fixed final poem periods to have a space before them
Galaxylaughing Dec 11, 2019
6fabb29
added message if user clicks 'reveal poem' when no lines were entered
Galaxylaughing Dec 11, 2019
29cbc1f
changed ternary line breaks for clarity
Galaxylaughing Dec 11, 2019
c031b78
got dynamic form working, no validations yet
Galaxylaughing Dec 11, 2019
028f57b
implemented validators
Galaxylaughing Dec 11, 2019
4d14730
added correct placeholders
Galaxylaughing Dec 12, 2019
a758e9a
dried up placeholder code
Galaxylaughing Dec 12, 2019
bf710ad
fixed bug
Galaxylaughing Dec 13, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/FinalPoem.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
.FinalPoem__poem {
border-bottom: 2px gray dashed;
}

.FinalPoem__poem--empty {
font-style: italic;
}
37 changes: 28 additions & 9 deletions src/components/FinalPoem.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import React from 'react';
import PropType from 'prop-types';
import './FinalPoem.css';

const FinalPoem = (props) => {
const FinalPoem = ({ poemLines, revealPoem, onRevealPoem }) => {

return (
<div className="FinalPoem">
<section className="FinalPoem__poem">
<h3>Final Poem</h3>
const displayPoem = (poemLines) => {
const collectedPoem = poemLines.map((line, i) => {
return <p key={i}>{line}</p>
});

</section>
return collectedPoem;
}

<div className="FinalPoem__reveal-btn-container">
<input type="button" value="We are finished: Reveal the Poem" className="FinalPoem__reveal-btn" />
</div>
return (
<div className="FinalPoem">
{ revealPoem
? <section className="FinalPoem__poem">
<h3>Final Poem</h3>
{ poemLines.length > 0
? displayPoem(poemLines)
: <p className="FinalPoem__poem--empty">You didn't enter any lines of poetry.</p>
}
</section>
: <div className="FinalPoem__reveal-btn-container">
<input type="button" value="We are finished: Reveal the Poem" className="FinalPoem__reveal-btn" onClick={ onRevealPoem } />
</div>
}
</div>
);
}

FinalPoem.propTypes = {
poemLines: PropType.arrayOf(PropType.string).isRequired,
onRevealPoem: PropType.func.isRequired,
revealPoem: PropType.bool.isRequired,
}

export default FinalPoem;
65 changes: 55 additions & 10 deletions src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,51 @@ class Game extends Component {

constructor(props) {
super(props);
}

render() {
this.state = {
submissions: [],
currentPlayer: 1,
lastsubmission: undefined,
poemCompleted: false,
}
}

const exampleFormat = FIELDS.map((field) => {
if (field.key) {
return field.placeholder;
mapToString = (arrayOfStrings) => {
const newString = arrayOfStrings.map((string) => {
if (string.key) {
return string.placeholder;
} else {
return field;
return string;
}
}).join(" ");
return newString;
}

onFormSubmit = (submissionComponents) => {
const newSubmission = this.mapToString(submissionComponents);

let { submissions, currentPlayer, lastsubmission } = this.state;

// add submissionComponents to submissions array
submissions.push(newSubmission);
// add one to currentPlayer
currentPlayer += 1;
// replace current last submission with this submission
lastsubmission = newSubmission;

this.setState( {submissions, currentPlayer, lastsubmission} );
}

onRevealPoem = () => {
// change poemCompleted to true
let { poemCompleted } = this.state;
poemCompleted = true;
this.setState({ poemCompleted });
}

render() {
const exampleFormat = this.mapToString(FIELDS);
const { submissions, lastsubmission, currentPlayer, poemCompleted } = this.state;

return (
<div className="Game">
Expand All @@ -32,11 +66,18 @@ class Game extends Component {
{ exampleFormat }
</p>

<RecentSubmission />
{ poemCompleted
? ''
: <div>
{ lastsubmission
? <RecentSubmission submission={lastsubmission} />
: ''
}
<PlayerSubmissionForm formFields={FIELDS} playerNumber={currentPlayer} onFormSubmit={this.onFormSubmit} />
</div>
}

<PlayerSubmissionForm />

<FinalPoem />
<FinalPoem poemLines={submissions} revealPoem={poemCompleted} onRevealPoem={this.onRevealPoem} />

</div>
);
Expand All @@ -61,6 +102,10 @@ const FIELDS = [
key: 'verb',
placeholder: 'verb',
},
{
key: 'preposition',
placeholder: 'preposition',
},
"the",
{
key: 'adj2',
Expand Down
2 changes: 1 addition & 1 deletion src/components/PlayerSubmissionForm.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
background-color: tomato;
}

.PlayerSubmissionFormt__input--invalid {
.PlayerSubmissionForm__input--invalid {
background-color: #FFE9E9;
}

Expand Down
133 changes: 122 additions & 11 deletions src/components/PlayerSubmissionForm.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,134 @@
import React, { Component } from 'react';
import PropType from 'prop-types';
import './PlayerSubmissionForm.css';

class PlayerSubmissionForm extends Component {

constructor(props) {
super(props);

this.state = {
fields: {},
placeholders: {},
}
}

render() {
componentDidMount = () => {
this.initializeFieldsAndPlaceholders();
this.initializeValidators();
}

validate = (fieldName) => {
const value = this.state.fields[fieldName];
const validation = this.validators[fieldName];

return (validation.test(value));
}

initializeValidators = () => {
this.validators = {};
const formFields = this.props.formFields;

for (const field of formFields) {
if (field.key) {
const {key} = field;
this.validators[key] = /.+/;
} else {
this.validators[field] = new RegExp(field);
}
}
}

initializeFieldsAndPlaceholders = () => {
let fieldState = {};
let placeholderState = {};
const formFields = this.props.formFields;

for (const field of formFields) {
if (field.key) {
const {key, placeholder} = field;
fieldState[key] = '';
placeholderState[key] = placeholder;
} else {
fieldState[field] = field;
}
}

this.setState({ fields: fieldState, placeholders: placeholderState });
}

onFieldChange = (event) => {
const { name, value } = event.target;

const { fields } = this.state;
const updatedFields = fields;
updatedFields[name] = value;

this.setState({fields: updatedFields});
}

clearState = () => {
let { fields } = this.state;
fields = {};

this.setState({ fields });
this.initializeFieldsAndPlaceholders();
}

onFormSubmit = (event) => {
event.preventDefault();

// returns if there's any invalid inputs
const keys = Object.keys(this.validators);
for (const key of keys) {
if (!this.validators[key].test(this.state.fields[key])) {
return;
}
}

// get the values from state
const submissionComponentsArray = Object.values({ ...this.state.fields });
// pass up array to Game
this.props.onFormSubmit(submissionComponentsArray);
// clear all changeable values from state
this.clearState();
}

makeInputCollection = (stateFields, placeholders) => {
// const { placeholders } = this.state;
const inputCollection = Object.keys(stateFields).map((field, i) => {
if ( !/the/i.test(field)
&& !/[.,?!]/.test(field)
&& !/^a$/.test(field) ) {
const fieldName = field;
const fieldValid = this.validate(String( fieldName ));
return <input
key={ i }
name={ fieldName }
value={ this.state.fields[fieldName] }
placeholder={ placeholders[field] }
onChange={ this.onFieldChange }
className={ fieldValid ? 'valid' : 'PlayerSubmissionForm__input--invalid' }
type="text"
/>;
} else {
return field;
}
});
return inputCollection;
}

render() {
const inputs = this.makeInputCollection(this.state.fields, this.state.placeholders);

return (
<div className="PlayerSubmissionForm">
<h3>Player Submission Form for Player #{ }</h3>
<div className="PlayerSubmissionForm" onSubmit={this.onFormSubmit}>
<h3>Player Submission Form for Player #{ this.props.playerNumber }</h3>

<form className="PlayerSubmissionForm__form" >

<div className="PlayerSubmissionForm__poem-inputs">

{
// Put your form inputs here... We've put in one below as an example
}
<input
placeholder="hm..."
type="text" />

{ inputs }
</div>

<div className="PlayerSubmissionForm__submit">
Expand All @@ -35,4 +140,10 @@ class PlayerSubmissionForm extends Component {
}
}

PlayerSubmissionForm.propTypes = {
formFields: PropType.array.isRequired,
playerNumber: PropType.number.isRequired,
onFormSubmit: PropType.func.isRequired,
};

export default PlayerSubmissionForm;
9 changes: 7 additions & 2 deletions src/components/RecentSubmission.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import React from 'react';
import PropType from 'prop-types';
import './RecentSubmission.css';

const RecentSubmission = (props) => {
const RecentSubmission = ({ submission }) => {
return (
<div className="RecentSubmission">
<h3>The Most Recent Submission</h3>
<p className="RecentSubmission__submission">{ }</p>
<p className="RecentSubmission__submission">{ submission }</p>
</div>
);
}

RecentSubmission.propTypes = {
submission: PropType.string.isRequired,
}

export default RecentSubmission;