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

redux-rails-resource

v1.0.2

Published

Convenient Higher Order Component for React elements utilizing the Redux-Rails middleware

Downloads

6,369

Readme

redux-rails-resource

Convenient Higher Order Component for React elements utilizing the Redux-Rails middleware

CircleCI npm license

Installation

You can use either yarn or npm to install redux-rails-resource and its dependencies.

with yarn

yarn add 'redux-rails-resource'

with npm

npm install 'redux-rails-resource'

Installing peer dependencies

redux-rails-resource has a few peer dependencies required to use the library.

If you already have these libraries listed in your app's dependencies, there's no need to install them again.

  • prop-types v15 or v16
  • react v15 or v16
  • react-redux 5.0.0+
  • redux 3.7.0+
  • redux-rails: 1.0.0+

Usage

resource(resourceName: string, resourceOptions: object)(CustomComponent: ReactComponent)

resourceName

The key of the corresponding resource in the redux state defined by the redux-rails config.

This will be used as both resource and controller when dispatching railsActions from redux-rails. (NOTE: controller can be explicitly set via resourceOptions)

The argument will also be the name of the key which will wrap everything passed down from the resource hoc to the wrapped component.

resourceOptions

  • autoload
    • Will dispatch an index call on componentDidMount. The sibling queryParams attribute will be passed as an argument if defined
  • queryParams
    • The optional argument to be passed to the index call if autoload is true
  • controller <string | Function(props) => string>
    • Explicitly set the controller for railsActions.
      • If set to a function, it must take the component's props as an argument and return a string
  • propWrapper
    • Explicitly set the name of the key which will wrap resource props
  • onlyActions
    • Do not pass redux state, only the corresponding railsActions. This may be more performant if the component does not need access to state.

Examples

Collection React Component

import { resource, resourceProps } from 'redux-rails-resource'

@resource('comments')
class CommentSection extends Component {
  static propTypes = {
    comments: resourceProps.collection
  }

  componentDidMount() {
    // GET request to /comments?deleted=false
    // Stores the result in redux and updates this component's models
    this.props.comments.index({ queryParams: { deleted: false } })
  }

  handleCreate = (commentAttributes) => {
    // POST request to /comments
    // The body of the post request will be JSON string of commentAttributes
    this.props.comments.create(commentAttributes)
  }

  render() {
    const { models } = this.props.comments

    return (
      <div>
        <NewCommentForm onCreate={this.handleCreate} />
        <CommentList comments={models} />
      </div>
    )
  }
}

Member React Component

import { resource, resourceProps } from 'redux-rails-resource'

@resource('user')
class UserProfile extends Component {
  static propTypes = {
    user: resourceProps.member
  }

  handleChangeName = (name) => {
    const { id } = this.comments.attributes

    // PUT request to /comments/:id
    // Second argument will be body of post request
    this.props.comments.update(id, { name })
  }

  render() {
    const { name, company } = this.props.comments.attributes

    return (
      <div>
        <NameField value={name} onSave={this.handleChangeName} />
        <CompanyDisplay company={company} />
      </div>
    )
  }
}

Paginated React Component

import { resource, resourceProps } from 'redux-rails-resource'

@resource('posts', { autoload: true })
class PaginatedPosts extends Component {
  static propTypes = {
    posts: resourceProps.collection
  }

  handlePageChange = (page) => {
    this.props.comments.updateFilters({ page })
  }

  handleFilterSelect = (filter, value) => {
    this.props.comments.updateFilters({ [filter]: value })
  }

  render() {
    return (
      <div>
        <PaginationControl onPageChange={this.handlePageChange}
        <Filters onFilterSelect={this.handleFilterSelect}
        <PostList posts={this.posts.models} />
      </div>
    )
  }
}

Actions

The resource hoc will pass down 5 functions as top level keys in the prop passed to the wrapped component: index, update, create, destroy, and updateFilters.

index

index(queryParams: object)

update

update(id: number, queryParams: object)

create

create(objectAttributes: object)

destroy

destroy(id: number)

updateFilters

updateFilters(partialQueryParams: object)

updateFilter will merge the existing queryParams of the corresponding resource with the partialQueryParams argument.