React & Redux Project — Udacity React Developer Nanodegree
This is the second project of the React Developer Nanodegree.
- MyReads — React Fundamentals Project
- Would You Rather — React & Redux Project
- TBD — React Native Project
The project uses Node.js and the Create-React-App starter. If you do not have Node >= 6.x installed, you can download it here: Node.js
Once Node is installed, navigate to the directory where you want to store the app
git clone https://github.com/dxit/react-redux-would-you-rather.git
npm install
or
yarn install
Once all of the dependencies have been installed you can launch the app with
npm start
or
yarn start
A new browser window should automatically open displaying the app. If it doesn't, navigate to http://localhost:3000/ in your browser
The person using your application have a way of impersonating/logging in as an existing user.
Information about the logged in user appears on the top right of the page. If someone tries to navigate anywhere by entering the address in the address bar, the user is asked to sign in and then the requested page is shown. The application allows the user to log out and log back in.
Once the user logs in, the user is able to toggle between his/her answered and unanswered polls on the home page, which is located at the root. The polls in both categories are arranged from the most recently created (top) to the least recently created (bottom). The unanswered questions are shown by default, and the name of the logged in user is visible on the page in the top right corner.
Each polling question has a link to the details of that poll. The details of each poll is available at questions/:question_id
.
When a poll is clicked on the home page, the following is shown:
- Text “Would You Rather”;
- Avatar of the user who posted the polling question; and
- Two options.
For answered polls, each of the two options contains the following:
- Text of the option;
- Number of people who voted for that option; and
- Percentage of people who voted for that option.
The option selected by the logged-in user is clearly marked.
Since we want to make sure our application creates a good user experience, the application shows a 404 page if the user is trying to access a poll that does not exist. (Please keep in mind that newly created polls will not be accessible at their url because of the way the backend is set up in this application.) It also display a navigation bar so that the user can easily navigate anywhere in the application.
So what happens when someone votes in a poll? Upon voting in a poll, all of the information of an answered poll are displayed. The user’s response are recorded and clearly visible on the poll details page. Users can only vote once per poll; they aren’t allowed to change their answer after they’ve voted -- no cheating allowed 😁! When the user comes back to the home page, the polling question appears in the “Answered” column.
It would be no fun to vote in polls if we couldn’t post our own questions! The form for posting new polling questions is available at the /add
route. The application shows the text “Would You Rather” and have a form for creating two options. Upon submitting the form, a new poll is created, the user it taken to the home page, and the new polling question appears in the correct category on the home page.
But how can we know how many questions each user has asked and answered? Let’s get some healthy competition going here! The application have a leaderboard that’s available at the /leaderboard
route. Each entry on the leaderboard contains the following:
- User’s name;
- User’s picture;
- Number of questions the user asked; and
- Number of questions the user answered
Users are ordered in descending order based on the sum of the number of questions they’ve asked and the number of questions they’ve answered. The more questions you ask and answer, the higher up you move.
The user are able to navigate to the leaderboard, to a specific question, and to the form that allows the user to create a new poll both from within the app and by typing in the address into the address bar. To make sure we’re showing the data that is relevant to the user, the application requires the user to be signed in order to access those pages.
Enjoy!
There are two types of objects stored in our database:
- Users
- Questions
Users include:
Attribute | Type | Description |
---|---|---|
id | String | The user’s unique identifier |
name | String | The user’s first name and last name |
avatarURL | String | The path to the image file |
questions | Array | A list of ids of the polling questions this user created |
answers | Object | The object's keys are the ids of each question this user answered. The value of each key is the answer the user selected. It can be either 'optionOne' or 'optionTwo' since each question has two options. |
Questions include:
Attribute | Type | Description |
---|---|---|
id | String | The question’s unique identifier |
author | String | The author’s unique identifier |
timestamp | String | The time when the question was created |
optionOne | Object | The first voting option |
optionTwo | Object | The second voting option |
Voting options are attached to questions. They include:
Attribute | Type | Description |
---|---|---|
votes | Array | A list that contains the id of each user who voted for that option |
text | String | The text of the option |
Your code will talk to the database via 5 methods:
_getUsers()
_getQuestions()
_saveQuestion(question)
_saveQuestionAnswer(object)
_saveUser(object)
_getUsers()
Method
Description: Get all of the existing users from the database.
Return Value: Object where the key is the user’s id and the value is the user object.
_getQuestions()
Method
Description: Get all of the existing questions from the database.
Return Value: Object where the key is the question’s id and the value is the question object.
_saveQuestion(question)
Method
Description: Save the polling question in the database.
Parameters: Object that includes the following properties: author
, optionOneText
, and optionTwoText
. More details about these properties:
Attribute | Type | Description |
---|---|---|
author | String | The id of the user who posted the question |
optionOneText | String | The text of the first option |
optionTwoText | String | The text of the second option |
Return Value: An object that has the following properties: id
, author
, optionOne
, optionTwo
, timestamp
. More details about these properties:
Attribute | Type | Description |
---|---|---|
id | String | The id of the question that was posted |
author | String | The id of the user who posted the question |
optionOne | Object | The object has a text property and a votes property, which stores an array of the ids of the users who voted for that option |
optionTwo | Object | The object has a text property and a votes property, which stores an array of the ids of the users who voted for that option |
timestamp | String | The time when the question was created |
_saveQuestionAnswer(object)
Method
Description: Save the answer to a particular polling question in the database.
Parameters: Object that contains the following properties: authedUser
, qid
, and answer
. More details about these properties:
Attribute | Type | Description |
---|---|---|
authedUser | String | The id of the user who answered the question |
qid | String | The id of the question that was answered |
answer | String | The option the user selected. The value should be either "optionOne" or "optionTwo" |
_saveUser(object)
Method
Description: Save the new user in the database
Parameters: Object that contains the following properties: avatarURL
and name
. More details about these properties:
Attribute | Type | Description |
---|---|---|
avatarURL | String | The URL of the avatar image |
name | String | The name (or username) of the new user |