1.0.36 • Published 1 year ago

@apexxcloud/react v1.0.36

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

ApexxCloud React Components

A React library for integrating ApexxCloud file upload functionality into your applications.

Installation

npm install @apexxcloud/react

Components

FileUploader

A React component that provides file upload functionality with progress tracking. The component renders a file input and a progress bar, handling all the upload logic internally.

Props

PropTypeDescriptionRequired
getSignedUrl(file: File) => Promise<string>Function to get a signed URL for file upload. This function should implement your backend integration logic.Yes
onUploadComplete(response: any) => voidCallback function that fires when the upload successfully completes. The response contains upload details including timestamp and server response.No
onUploadError(error: any) => voidCallback function that fires if the upload fails. Receives an error object with type, error details, and timestamp.No
onUploadProgress(progress: any) => voidCallback function that fires during upload progress. Receives an object with loaded bytes, total bytes, and progress percentage.No

Features

  • Built-in progress tracking with visual progress bar
  • Error handling with optional error callback
  • Upload progress updates
  • Automatic file selection handling
  • TypeScript support

Example Usage

import { FileUploader } from '@apexxcloud/react';

function MyComponent() {
  // Function to get signed URL from your backend
  const getSignedUrl = async (file: File) => {
    const response = await fetch('/api/get-signed-url', {
      method: 'POST',
      body: JSON.stringify({ fileName: file.name, fileType: file.type }),
    });
    const { signedUrl } = await response.json();
    return signedUrl;
  };

  // Optional callback handlers
  const handleUploadComplete = (response) => {
    console.log('Upload successful:', response);
    // Handle successful upload (e.g., show success message)
  };

  const handleUploadError = (error) => {
    console.error('Upload failed:', error);
    // Handle error (e.g., show error message)
  };

  const handleProgress = (progress) => {
    console.log(`Upload progress: ${progress}%`);
    // Update UI with progress if needed
  };

  return (
    <FileUploader
      getSignedUrl={getSignedUrl}
      onUploadComplete={handleUploadComplete}
      onUploadError={handleUploadError}
      onUploadProgress={handleProgress}
    />
  );
}

Implementation Details

The FileUploader component:

  • Uses the useApexxCloud hook internally for upload functionality
  • Manages its own progress state
  • Renders a basic file input and progress bar
  • Handles file selection changes automatically
  • Cleans up event listeners properly using useCallback

Hooks

useApexxCloud

A custom hook that provides access to ApexxCloud SDK functionality.

Returns

PropertyTypeDescription
upload(signedUrl: string, file: File, options?: UploadOptions) => Promise<any>Function to upload a file

UploadOptions

OptionTypeDescription
onProgress(event: { loaded: number, total: number, progress: number, type: string }) => voidCallback for upload progress updates
onComplete(event: { type: string, response: any, timestamp: Date }) => voidCallback when upload completes
onError(event: { type: string, error: Error, timestamp: Date }) => voidCallback when upload fails
onStart(event: { type: string, timestamp: Date, file: { name: string, size: number, type: string } }) => voidCallback when upload starts
signalAbortSignalOptional AbortSignal to cancel the upload

Example Usage

import { useApexxCloud } from '@apexxcloud/react';

function MyCustomUploader() {
  const { upload } = useApexxCloud();
  const abortController = new AbortController();

  const handleUpload = async (file: File) => {
    const signedUrl = await getSignedUrl(file);
    
    try {
      await upload(signedUrl, file, {
        onStart: (event) => console.log(`Upload starting for ${event.file.name}`),
        onProgress: (event) => console.log(`Upload progress: ${event.progress}%`),
        onComplete: (event) => console.log('Upload complete:', event.response),
        onError: (event) => console.error('Upload failed:', event.error),
        signal: abortController.signal
      });
    } catch (error) {
      console.error('Upload failed:', error);
    }
  };

  const cancelUpload = () => {
    abortController.abort();
  };

  return (
    // Your custom upload UI
  );
}

Requirements

  • React 16.8+ (Hooks support)
  • @apexxcloud/sdk-js

License

Add your license information here

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago