Skip to content

Reset all stats button #29

@scottrohrig

Description

@scottrohrig

Description

A button on the profile page to handle setting all the users stats back to default.

This will require __ steps

  1. Create the mutation

    identify whether a mutation exists to pass in all the necessary stat fields and if not make one

    1. check server/schemas/typeDefs.js for the mutation
    2. The stats that should be reset are
      • money, appleCount, trees, juicers, mashers, ovens
    3. The mutation should take in an objectId id and return a User

    EX:

    // server/schemas/typeDefs.js
    const typeDefs = gql`
      ...
      type Mutation {
        ...
        resetUserStats(): User
      }
    `;
  2. 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
        }
      }
    
    }
  3. Test the functionality in Apollo Sandbox

    This mutation should be easy to import after testing using the Apollo Sandbox Explorer

    1. navigate to https://studio.apollographql.com/sandbox/explorer
    2. 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 the Response 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 (see Documentation 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 and Fields, making sure to add in the _id of any subdocuments (ie, juicers)

  4. 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
    `;
  5. Create the reset ALL stats button in the profile page.

    1. Make the button

      In the profile page make a new button element in the Stats container

      Style the button with a btn btn-shop className for now

    2. Add a button handler (eg, handleResetAllStats)

      also add an event handler to handle when the button is pressed.

    3. 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>
        )
      }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions