-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Description
A button on the profile page to handle setting all the users stats back to default.
This will require __ steps
-
Create the mutation
identify whether a mutation exists to pass in all the necessary stat fields and if not make one
- check
server/schemas/typeDefs.js
for the mutation - The stats that should be reset are
money
,appleCount
,trees
,juicers
,mashers
,ovens
- The mutation should take in an objectId
id
and return aUser
EX:
// server/schemas/typeDefs.js const typeDefs = gql` ... type Mutation { ... resetUserStats(): User } `;
- check
-
Create the resolver to resetUserStats
// server/schemas/resolvers.js const resolvers = { ... Mutation: { ... resetUserStats: async function( parent, args, context ) { // this should find a `User` using the `context.user._id` // then `$set` the new stats to the `args` passed in return user } } }
-
Test the functionality in Apollo Sandbox
This mutation should be easy to import after testing using the Apollo Sandbox Explorer
- navigate to
https://studio.apollographql.com/sandbox/explorer
- make sure the endpoint directs to
http://localhost:3001/graphql
Create a new tab at the top, then click on the Documentation button in the left inner sidebar. The icon looks like a piece of paper.
If you have not created an account, signup because it will be quicker. Otherwise you have to type everything manually.
Login first using the following
Operation
with a known user's email and password// Operation `login` mutation login($email: String!, $password: String!) { login(email: $email, password: $password) { token user { _id username } } }
In the
Variables
pane at the bottom, add a user you have created.// Variables { "email": "test@test.com", "password": "pw123" }
Then copy the
token
from theResponse
pane on the right.Add the token to the
Headers
☑ Authorization <token>
✖ If you are logged in, you can import the mutation easily by clicking on the mutation option in the
Root Types
(seeDocumentation
tab in the left sidebar).From the list of mutations you shoul be able to se the resetUserStats mutation you just created and click the plus button to add it to the
Operation
pane.From here you can import all of that mutation's
Arguments
andFields
, making sure to add in the_id
of any subdocuments (ie,juicers
) - navigate to
-
Copy that mutation into the client-side
mutations.js
If the test works, then open the client side mutations file and add a new exported constant to reset the user stats
// client/src/utils/mutations.js ... export const RESET_USER_STATS = gql` // paste the working mutation here from the sandbox `;
-
Create the reset ALL stats button in the profile page.
-
Make the button
In the profile page make a new button element in the
Stats
containerStyle the button with a
btn btn-shop
className
for now -
Add a button handler (eg,
handleResetAllStats
)also add an event handler to handle when the button is pressed.
-
import the mutation from mutations.js
// client/src/pages/Profile.js // import { RESET_USER_STATS } from '../utils/.. // import useMutation function Profile() { ... // define the mutation function and set it equal to useMutation(RESET_USER_STATS, { variables: { pass in the variables here }}) ... // define the event handler return ( <div> ... {/* Add button near 'STATS' */} </div> ) }
-