npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-apollo-autosave

v0.0.9

Published

React component that allows for editing and autosave behaviour, combining an Apollo Query and Mutation.

Downloads

17

Readme

react-apollo-autosave

A React component that allows for editing and autosave behavior, combining an Apollo Query and Mutation. With the query, you retrieve the data that you want to edit from your backend. The mutation updates it. Internally, the component maintains a local copy of the data so that you can directly couple properties of the data to React props. As a result, you don't need to create variables inside your component state for this.

Installation

npm install react-apollo-autosave --save

Example

import { EditorAutosave } from 'react-apollo-autosave';

// GraphQL query and mutation for reading and updating a user's name
const getUser = gql`
    query User($id: ID!) {
        user(id: $id) {
            id
            name
        }
    }
`;

const updateUser = gql`
    mutation UpdateUser($id: ID!, $input: UserInput!) {
        updateUser(id: $id, input: $input) {
            id
            name
        }
    }
`;

// A simple React component that shows an input where you can edit a user's name
class MyReactComponent {
    render() {
        const id = "someUser";
        return <EditorAutosave
            query={getUser}            // the query to perform to retrieve the data
            queryVariables={{ id }}    // variables passed to the query
            mutation={updateUser}      // the mutation to perform to update the data
            mutateOnUpdate={true}      /* automatically perform the mutate when the update
                                          function is called */
        >
            {({ update, queryResult: { loading, error, data } }) => {
                // deal with loading and error cases
                if (loading) { return null; }
                if (error) { return error.message; }
                
                // retrieve the user information from the query result
                const user = data.user;
                
                // render the input
                return <input type="text" value={user.name} onChange={(event) => {
                    // create the object containing the changed user name
                    const input = {
                        name: event.target.value
                    };
                    
                    // only strings with length > 0 are a valid username
                    const nameIsValid = input.name.length > 0;

                    /* Call the update function. The first parameter contains the new local
                       data (should be the same structure as the query result). The second parameter
                       contains the information needed to perform the mutation. The third parameter
                       overrides the mutateOnUpdate value. In this case, the mutation will not be
                       performed if the user name is not valid, but the local data will be updated
                       so that the contents of the input element changes when the user types
                       something.
                    */
                    update({ user: input }, { variables: { id, input } }, nameIsValid);
                }}>;
            }
        </EditorAutosave>
    }
}

Features

Autosaving changes

Whenever you change the value that the editor component controls, you call the update method, which updates the local data. The component then automatically calls the mutate function (resulting in the autosave behavior). The component uses the lodash throttle function to reduce the number of queries to the backend. You can set the throttle wait time and whether the throttle is leading or trailing by changing the waitTime (default 3000 ms) and throttleType (default "leading") properties.

Explicit saving

If you want control over the saving behavior (for example only save when the user presses a button), you can set the mutateOnUpdate prop to false. Whenever the input changes, call the update function:

update({ user: input });

When the user presses the save button, call the update function again to trigger a save explicitly:

update(undefined, { variables: { id, input } }, true);

Combining with validation

In case of an invalid input, normally you don't want to sync the changes to the backend, but you do want to change the local data, otherwise the user will not be able to see what (s)he is typing/clicking on. To support validation, you can override whether a mutation should happen when you call the update function (see the code example in the previous section).