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

@docflow/react-activestorage-provider

v1.0.1

Published

A React component that allows easy file upload using ActiveStorage. Updated for React 18

Downloads

1,355

Readme

@docflow/react-activestorage-provider

NPM

[!NOTE]
This is a fork of react-activestorage-provider, rewritten in TypeScript and modern React (supporting React 18).

There is an important breaking change regarding the uploads array containing finished uploads, see CHANGELOG.md.

Apart from that, this package also extracts the functionality of DirectUploadProvider into a new hook useDirectUpload that you can use to skip the render function. The rest is the same.

ActiveStorage is an amazing addition to Rails 5.2, and as usual the team have made it fantastically simple to use... if you’re generating HTML server-side, that is. This component aims to make it just as easy to use from React.

ActiveStorageProvider handles the direct upload of a file to an ActiveStorage service and the attachment of that file to your model. It uses the render props pattern so you can build your own upload widget.

Install

npm install --save @docflow/react-activestorage-provider

Usage

ActiveStorageProvider makes it easy to add a simple upload button. When you call handleUpload with a FileList or an array of Files, this component creates a Blob record, uploads the file directly to your storage service, and then hits your Rails controller to attach the blob to your model. (If you want to handle the attachment yourself in order to, for example, provide other attributes, see the lower level DirectUploadProvider.)

import ActiveStorageProvider from "@docflow/react-activestorage-provider";

// ...

return (
  <ActiveStorageProvider
    endpoint={{
      path: "/profile",
      model: "User",
      attribute: "avatar",
      method: "PUT",
    }}
    onSubmit={(user) => this.setState({ avatar: user.avatar })}
    render={({ handleUpload, uploads, ready }) => (
      <div>
        <input
          type="file"
          disabled={!ready}
          onChange={(e) => handleUpload(e.currentTarget.files)}
        />

        {uploads.map((upload) => {
          switch (upload.state) {
            case "waiting":
              return (
                <p key={upload.id}>Waiting to upload {upload.file.name}</p>
              );
            case "uploading":
              return (
                <p key={upload.id}>
                  Uploading {upload.file.name}: {upload.progress}%
                </p>
              );
            case "error":
              return (
                <p key={upload.id}>
                  Error uploading {upload.file.name}: {upload.error}
                </p>
              );
            case "finished":
              return (
                <p key={upload.id}>Finished uploading {upload.file.name}</p>
              );
          }
        })}
      </div>
    )}
  />
);

ActiveStorageProvider Props

These are your options for configuring ActiveStorageProvider.

| Prop (*required) | Description | | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | directUploadsPath | stringThe direct uploads path on your Rails app, if you’ve overridden ActiveStorage::DirectUploadsController | | endpoint* | { path: string, model: string, attribute: string, method: string, host?: string, port?: string, protocol?: string }The details for the request to attach the file | | headers | Record<string, string>Optional headers to add to request, can also be used to override default headers | | multiple | boolean (default: false)Whether the component should accept multiple files. If true, the model should use has_many_attached | | onBeforeBlobRequest | (request: { id: string, file: File, xhr: XMLHttpRequest }) => voidA callback that allows you to modify the blob request | | onBeforeStorageRequest | (request: { id: string, file: File, xhr: XMLHttpRequest }) => voidA callback that allows you to modify the storage request | | onError | (error: Response) => voidA callback to handle an error (>= 400) response by the server in saving your model | | onSubmit* | (endpointResponse: unknown) => voidA callback for the server response to successfully saving your model | | render* | (props: RenderProps) => ReactNodeRender function for your uploader |

RenderProps

This is the type of the argument with which your render function will be called.

export type RenderProps = {
  /** false while any file is uploading */
  ready: boolean;
  /** uploads in progress */
  uploads: ActiveStorageFileUpload[];
  /** call to initiate an upload */
  handleUpload: (files: FileList | File[]) => unknown;

  /* or, if you want more granular control... */

  /** call to set list of files to be uploaded */
  handleChooseFiles: (files: FileList | File[]) => unknown;
  /** begin the upload of the files in the list */
  handleBeginUpload: () => unknown;
};

export type CustomHeaders = Record<string, string>;

DirectUploadProvider

ActiveStorageProvider makes it simple to add a quick “upload” button by taking care of both uploading and attaching your file, but it shouldn’t stand in your way if you’re doing something more interesting. If you want to handle the second step, attaching your Blob record to your model, yourself, you can use the lower level DirectUploadProvider. It creates the blob records and uploads the user’s files directly to your storage service, then calls you back with the signed ids of those blobs. Then, you can create or update your model as you need.

function PostForm() {
  function handleAttachment(signedIds) {
    const body = JSON.stringify({ post: { title: ..., images: signedIds }})
    fetch('/posts.json', { method: 'POST', body })
  }

  return (
    <DirectUploadProvider onSuccess={handleAttachment} render={...} />
  )
}

DirectUploadProvider is a named export, so

import { DirectUploadProvider } from "@docflow/react-activestorage-provider";

and use it with the following props:

| Prop (*required) | Description | | ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- | | directUploadsPath | stringThe direct uploads path on your Rails app, if you’ve overridden ActiveStorage::DirectUploadsController | | headers | Record<string, string>Optional headers to add to request | | onBeforeBlobRequest | (request: { id: string, file: File, xhr: XMLHttpRequest }) => voidA callback that allows you to modify the blob request | | onBeforeStorageRequest | (request: { id: string, file: File, xhr: XMLHttpRequest }) => voidA callback that allows you to modify the storage request | | onSuccess* | (signedIds: string[]) => voidThe callback that will be called with the signed ids of the files after the upload is complete | | origin | { host?: string, port?: string, protocol?: string }The origin of your rails server. Defaults to where your React app is running | | render* | (props: RenderProps) => ReactNodeRender function for your uploader |

useDirectUpload

If you want to use the functionality of DirectUploadProvider without the hassle of the render function, use this hook. It takes all the same props as DirectUploadProvider, except for render, and returns the same thing as the render function accepts as an argument.

import { useDirectUpload } from "@docflow/react-activestorage-provider";

function Uploader() {
  const { handleUpload } = useDirectUpload({
    onSuccess: (signedIds) => console.log(signedIds),
  });

  return (
    <input
      type="file"
      onChange={(e) => e.target.files && handleUpload(e.target.files)}
    />
  );
}