Skip to content

Commit 3bdfc88

Browse files
jaygiangtheangchen
andauthored
544 - Polish persisted form example (#545)
* Add success message when indexedDB submitted * Bold code * Added h3 to step 4 --------- Co-authored-by: theangchen <theangchen@yahoo.com>
1 parent 1d358c9 commit 3bdfc88

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

examples/persisted-form/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This example shows you how to configure a persisted form that saves the data loc
66

77
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.
88

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

1111
### Imports
1212

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

5454
## Steps
5555

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

5859
```jsx
5960
import { OfflineStorageWrapper } from "@nmfs-radfish/react-radfish";
@@ -72,7 +73,8 @@ const offlineStorageConfig = {
7273
};
7374
```
7475

75-
2. In the `index.jsx` file, wrap the `App` component with `OfflineStorageWrapper` and pass the config object:
76+
### 2. Wrap the App Component with OfflineStorageWrapper
77+
In the `index.jsx` file, wrap the `App` component with `OfflineStorageWrapper` and pass the config object:
7678

7779
```jsx
7880
const root = ReactDOM.createRoot(document.getElementById("root"));
@@ -86,8 +88,10 @@ root.render(
8688
);
8789
```
8890

89-
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.
90-
1. Create a `handleOnSubmit` handler and pass it to the wrapper:
91+
### 3. Add FormWrapper Context Provider
92+
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.
93+
94+
Create a `handleOnSubmit` handler and pass it to the wrapper:
9195

9296
```jsx
9397
const { createOfflineData } = useOfflineStorage();
@@ -114,7 +118,8 @@ return (
114118
);
115119
```
116120

117-
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`.
121+
### 4. Build the Form
122+
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`.
118123

119124
## Preview
120125
This example will render as shown in this screenshot:

examples/persisted-form/src/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const FormInfoAnnotation = () => {
2525
return (
2626
<Alert type="info" heading="Information" headingLevel="h2">
2727
This is an example of a live form with data populated from IndexDB. The form data is stored in
28-
the browser's IndexedDB using methods from the <code>useOfflineStorage</code> hook, which uses
28+
the browser's IndexedDB using methods from the <strong>useOfflineStorage</strong> hook, which uses
2929
Dexie.js behind the scenes.
3030
<br />
3131
<br />

examples/persisted-form/src/pages/Home.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "../index.css";
22
import React, { useEffect, useState } from "react";
33
import { useParams, useNavigate } from "react-router-dom";
44
import { FormGroup, TextInput, Label, Button, Form } from "@trussworks/react-uswds";
5-
import { useOfflineStorage } from "@nmfs-radfish/react-radfish";
5+
import { useOfflineStorage, dispatchToast } from "@nmfs-radfish/react-radfish";
66

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

126-
export default HomePage
128+
export default HomePage;

0 commit comments

Comments
 (0)