Using React-smartmultiparts in a Full Stack Application

by chrismojekwu in Design > Websites

44 Views, 0 Favorites, 0 Comments

Using React-smartmultiparts in a Full Stack Application

This instruction guide will walk you through how to use react-smartmultiparts (to be referred to as RSM) in your full-stack application. It consists of a file input component that displays a form based on the file extension. You create the form by using JavaScript objects and style it with CSS class selectors. This means that instead of wasting a day writing and reading HTML input elements, you can skip directly to writing a callback to send data to your backend. Whether you’re working with small files like text documents or very large ones like audio and video files, RSM can speed up development time for you and/or your team.

Supplies

Code Editor

Browser

Internet Connection

Prerequisites

Before you start, you need to understand your use case. If you’re uploading files to a database or storage bucket, you typically need to be able to reference them somehow. The reason RSM generates the form is to allow you to collect the data you need for your database. It gives non-technical users instant feedback, which indicates that they need to submit more information with their media.

Other potential uses include sending a preformatted post to a social media bot or connecting it to a generative AI service. Before starting, you should take inventory and plan your project to use it effectively.

You will need to create a React project and install the package from NPM.

You can learn how to create a React project here:

https://react.dev/learn/creating-a-react-app

You can install the package from here:

https://www.npmjs.com/package/react-smartmultiparts

Choosing a Form

First, choose which form you want to use.

To display the same form for one or more file types, use form one.

To display a different form for a set of file types, use form two.

To handle a batch upload of multiple files, use form three.

For the sake of this instructional guide, I will be using form two. This will allow me to create different forms for different file types.

Writing a Schema

After choosing the form, plan the schema for your file types before writing any code. This will allow you to choose from the available inputs to find the best fit. Then, decide what information you want to persist on the backend. It should match the schema used for your API/DB; if working on a team, ask the backend engineer what they need.

For the demo application, I want to allow users to upload images, audio, and video files. Specifically, JPG files, MP3 files, and MP4 files. For all 3 file types, I will need a title and author, and I would like to allow them to provide a description. I also want to confirm that the user has rights over the media, so I want to include a checkbox.

Additionally, for images, I would like to know whether they were published in any articles, websites, or media galleries. For MP3 files, I would like to know if they were released on any labels or streaming services. Finally, for MP4 files, I would like to know the best resolution to display them in.

Building the Form

Most form inputs will be plain text inputs. Because I’ll be using some of the same fields, I can use an array to share fields between the 3 file options.

Notice I'm using a checkbox input to determine if the user has permission to use the media they’re uploading. The form will use the label to the format object. You can leave it as is, but most endpoints will require you to transform the data in the callback.

This holds true for the extra input fields that I want to add for all three filetypes. To make the code cleaner, I can use another array to hold the string values for the checkbox and the extra inputs for the 3 file types.

The final form object will use the spread operator to add the default fields, including the checkbox, and the extra inputs by referencing their array position in the string array.

Styling the Form

*Note* To style the form you will need to test it by selecting one of the 3 different file types. This will display the form and allow you to adjust the styling as needed. You can also use IDs by inspecting the code in DevTools.

I’m going to keep the form styling simple. I’m using the logo prop on form two to include a black-and-white company logo above the form. To match the black-and-white logo, I want the form input labels to be white text on a black background.

The project setup I’m using includes Tailwind, which overwrites the default CSS for text inputs and makes them invisible on a white background. Because of this, I want to give them a gray border. I’m also adjusting the margin and the width on the inputs. I’m only using the provided class and ID names from the package and CSS pseudo-classes.

Now that we have a completed and styled form, we can move on to writing the callback.

Writing the Callback Part 1: Transform the Data

Now that the form is complete, you’ll need to write a callback to pass into the cb prop on either of the three forms. The form adds all of the input values to a form data object and passes it into the callback. First, print the object so you can see how it's formatted.

Most input types are represented by a key that tells you the input type and its index position in the form object arrays. However, using a plain text input will always use the label name in all lowercase letters as the key. So if your label is “Title,” the key will be “title,” and if your label is “What's your last name?” your key will be “what's your last name?”.

If you have control over your DB schema, you can be intentional about using single words with no spaces or underscores to make this process a bit easier.

In this case, I have two values that need to be transformed for each file type. Each one uses a custom label string and the permission checkbox. Naming a column “checkbox_3” would be a terrible practice, and you shouldn’t use a sentence either. Earlier, we used an array to hold our custom label strings, so we can reuse it for the transformation. I wrote a simple function that takes the form data object and updates the keys to match our DB schema.

I pass in the form data, the key I want to use, and the custom label string. All three files also include the checkbox. In this case, I want to change the key and update the value to a boolean indicating that they have permission. I handle both by using the function to reassign the needed values and delete the old keys. In the example, the checkbox is required by using the exclamation point, so the form won't submit unless checked; it could be hardcoded to true, but it's good to have a fallback in case it changes later.

The function I wrote returns the final object formatted for my backend API and can be reused for all 3 file types in our callback.

Writing the Callback Part 2: Handling the File Types

The most logical way to handle your file types is to use a switch function. You can assign the form data to a variable and access the file type with dot or bracket notation. This gives you the MIME type string for the switch statement to handle logic for each file type. Inside our switch statement, I use the function from the previous step to pass in properties that match the schema. The function returns the corrected object, which is assigned to a variable. I then use that variable to send the data to my backend API.

For the demo, I wrote two mock functions that instantly resolve a promise. I send the formatted object to the mock file endpoint, which would likely be a database or S3 bucket; then, in the callback of the file endpoint, I call the mock data endpoint to send the data to my own DB endpoint. I typically just return false in the default case to avoid unwanted behavior.

Typically, uploading media to a storage bucket will generate a URL that will be sent in the response. Inside of the callback for the file upload, you can attach the url to your form data object and send it to your own db.

Conclusion

That’s it! If you have followed the steps correctly, you now have a fully styled and functional file input that displays a form, collects the necessary data, and passes it into a callback that you wrote. You have transformed the data to match your schema and prepared it for a storage bucket and your own database.

The rest of the work is done on your backend API. You may have to authenticate with your API before sending to a bucket or take other preliminary steps, but the user-facing portion of your work is done.