Skip to content

Commit

Permalink
544 - Polish persisted form example (#545)
Browse files Browse the repository at this point in the history
* Add success message when indexedDB submitted

* Bold code

* Added h3 to step 4

---------

Co-authored-by: theangchen <[email protected]>
  • Loading branch information
jaygiang and theangchen authored Dec 9, 2024
1 parent 1d358c9 commit 3bdfc88
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
17 changes: 11 additions & 6 deletions examples/persisted-form/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This example shows you how to configure a persisted form that saves the data loc

The `FormWrapper` component is a context provider for form data. It provides a context that contains the current form data, a function to update the form data, and a function to handle form input changes.

Learn more about RADFish examples at the official [documentation](https://nmfs-radfish.github.io/radfish/developer-documentation/examples-and-templates#examples)
Learn more about RADFish examples at the official [documentation](https://nmfs-radfish.github.io/radfish/developer-documentation/examples-and-templates#examples).

### Imports

Expand Down Expand Up @@ -53,7 +53,8 @@ The `useFormState` hook is a custom hook for accessing the `FormContext`.

## Steps

1. In the `index.jsx` file, import the `OfflineStorageWrapper`, then create a configuration object:
### 1. Configure Offline Storage
In the `index.jsx` file, import the `OfflineStorageWrapper`, then create a configuration object:

```jsx
import { OfflineStorageWrapper } from "@nmfs-radfish/react-radfish";
Expand All @@ -72,7 +73,8 @@ const offlineStorageConfig = {
};
```

2. In the `index.jsx` file, wrap the `App` component with `OfflineStorageWrapper` and pass the config object:
### 2. Wrap the App Component with OfflineStorageWrapper
In the `index.jsx` file, wrap the `App` component with `OfflineStorageWrapper` and pass the config object:

```jsx
const root = ReactDOM.createRoot(document.getElementById("root"));
Expand All @@ -86,8 +88,10 @@ root.render(
);
```

3. Use the provided `FormWrapper` context provider in this example, located in the `/src/contexts/FormWrapper.jsx` directory, to wrap child form components in a parent component. In this example `App.jsx` is the parent component.
1. Create a `handleOnSubmit` handler and pass it to the wrapper:
### 3. Add FormWrapper Context Provider
Use the provided `FormWrapper` context provider in this example, located in the `/src/contexts/FormWrapper.jsx` directory, to wrap child form components in a parent component. In this example `App.jsx` is the parent component.

Create a `handleOnSubmit` handler and pass it to the wrapper:

```jsx
const { createOfflineData } = useOfflineStorage();
Expand All @@ -114,7 +118,8 @@ return (
);
```

4. Construct your form using the `react-radfish` components. See the `/src/pages/Form.jsx` file to see how to construct the form and use the methods available from `FormWrapper`.
### 4. Build the Form
Construct your form using the `react-radfish` components. See the `/src/pages/Form.jsx` file to see how to construct the form and use the methods available from `FormWrapper`.

## Preview
This example will render as shown in this screenshot:
Expand Down
2 changes: 1 addition & 1 deletion examples/persisted-form/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const FormInfoAnnotation = () => {
return (
<Alert type="info" heading="Information" headingLevel="h2">
This is an example of a live form with data populated from IndexDB. The form data is stored in
the browser's IndexedDB using methods from the <code>useOfflineStorage</code> hook, which uses
the browser's IndexedDB using methods from the <strong>useOfflineStorage</strong> hook, which uses
Dexie.js behind the scenes.
<br />
<br />
Expand Down
8 changes: 5 additions & 3 deletions examples/persisted-form/src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "../index.css";
import React, { useEffect, useState } from "react";
import { useParams, useNavigate } from "react-router-dom";
import { FormGroup, TextInput, Label, Button, Form } from "@trussworks/react-uswds";
import { useOfflineStorage } from "@nmfs-radfish/react-radfish";
import { useOfflineStorage, dispatchToast } from "@nmfs-radfish/react-radfish";

const HomePage = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -35,8 +35,10 @@ const HomePage = () => {
if (!id) {
const formId = await createOfflineData("formData", values);
navigate(`${formId}`);
dispatchToast({ status: "success", message: "Your form has been successfully saved offline! You can now revisit it anytime." });
} else {
await saveOfflineData("formData", [formData]);
await saveOfflineData("formData", [values]);
dispatchToast({ status: "success", message: "Your changes have been saved! The form has been updated successfully." });
// after updating the data in IndexedDB, we can execute any other logic here
// eg. execute a POST request to an API
}
Expand Down Expand Up @@ -123,4 +125,4 @@ const HomePage = () => {
);
};

export default HomePage
export default HomePage;

0 comments on commit 3bdfc88

Please sign in to comment.