Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 9f957a9

Browse files
committed
Initial commit
0 parents  commit 9f957a9

File tree

20 files changed

+995
-0
lines changed

20 files changed

+995
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
~

LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Frontity Gravity Forms
2+
3+
Gravity forms package for [Frontity](https://frontity.org).
4+
5+
# How does it work?
6+
7+
1. You need to create a page in your WordPress site (if you don't already have one) that contains a Gravity Forms shortcode or Gutenberg block.
8+
1. Install the Frontity Gravity Forms package as shown in the Installation steps below.
9+
1. Visit that page in Frontity and the package will automatically render all the Gravity Forms forms that are present, including other content on the page.
10+
1. On successful form submission, the email goes to the recipient set in the form settings in WP (if Gravity Forms email settings are configured on your WordPress site). Errors are shown as well if the fields are invalid.
11+
12+
# Features
13+
14+
1. All the Gravity Forms forms on the page will work.
15+
1. You can also use it for multiple pages.
16+
1. Built with React and Frontity, so its fast and performant.
17+
18+
# Installation
19+
20+
1. Do `npm install @aamodtgroup/frontity-gravity-forms` in the root of your project.
21+
2. Add the package name in `frontity-settings.js`.
22+
23+
```javascript
24+
"packages": [
25+
"@aamodtgroup/frontity-gravity-forms"
26+
// other packages ...
27+
]
28+
```
29+
30+
3. Add api keys to the state in frontity
31+
```javascript
32+
source: {
33+
gfAuth: {
34+
key: "ck_example",
35+
secret: "cs_example",
36+
}
37+
}
38+
```
39+
40+
4. Run `npx frontity dev` again.
41+
42+
That's it! The package doesn't need any settings. You should be able to see the form in any page or post that contains one.
43+
44+
## Credits
45+
46+
- Build with love :blue_heart:, using [Frontity](https://frontity.org)
47+
48+
## Authors
49+
50+
1. [Kasper Aamodt](https://twitter.com/kasperaamodt) - Owner and developer [@aamodtgroup](https://twitter.com/aamodtgroup)
51+
52+
## License
53+
54+
![License: GPL v2](https://img.shields.io/badge/License-GPL%20v2-blue.svg)
55+
56+
- **[GPLv2](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)**

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@aamodtgroup/frontity-gravity-forms",
3+
"version": "0.1.4",
4+
"description": "Gravity Forms extension for Frontity theme.",
5+
"keywords": [
6+
"Frontity",
7+
"Gravity",
8+
"forms",
9+
"WordPress",
10+
"Reactjs",
11+
"react",
12+
"frontity-gf"
13+
],
14+
"homepage": "https://aamodtgroup.com",
15+
"license": "GPL-2.0-or-later",
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/aamodtgroup/frontity-gravity-forms"
19+
},
20+
"bugs": {
21+
"url": "https://github.com/aamodtgroup/frontity-gravity-forms/issues"
22+
},
23+
"author": "Aamodt Group",
24+
"peerDependencies": {
25+
"frontity": "^1.15.0",
26+
"@frontity/html2react": "^1.6.2"
27+
},
28+
"publishConfig": {
29+
"cache": "~/.npm",
30+
"access": "public"
31+
}
32+
}

src/components/Form.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { connect, styled } from "frontity";
2+
import FormIdContext from "./../context/FormIdContext";
3+
import Message from "./Message";
4+
5+
/**
6+
*
7+
* @param {Object} state Frontity State.
8+
* @param {Object} actions Actions methods.
9+
* @param {int} id Form Id.
10+
* @param {Object} children children.
11+
* @param {String} className Classname.
12+
* @param {Object} method Function.
13+
*
14+
* @return {*}
15+
*/
16+
const Form = ( { state, actions, id, children, className, method } ) => {
17+
18+
actions.gf.initForm( id );
19+
20+
/**
21+
* Form onSubmit event handler.
22+
*
23+
* @param {Object} event Event.
24+
*/
25+
const handleOnSubmit = ( event ) => {
26+
27+
event.preventDefault();
28+
29+
// Set the loading to true first to show processing while the request is ongoing.
30+
state.gf.forms[ id ].loading = true;
31+
32+
// Call the action sendform that will get the form data from state using the form id.
33+
actions.gf.sendForm( id );
34+
35+
};
36+
37+
return (
38+
<FormIdContext.Provider value={ id }>
39+
<FormElement method={ method } onSubmit={ handleOnSubmit } className={ className } id={ id }>
40+
{ children }
41+
</FormElement>
42+
{ state.gf.forms[ id ].loading ? <Processing>Processing...</Processing> : <Message /> }
43+
</FormIdContext.Provider>
44+
)
45+
};
46+
47+
const FormElement = styled.form``;
48+
const Processing = styled.div``;
49+
50+
export default connect( Form );

src/components/HiddenInput.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import FormIdContext from "../context/FormIdContext";
3+
import { connect } from "frontity";
4+
5+
/**
6+
* HiddenInputs component
7+
*
8+
* @param {Object} state Frontity state.
9+
* @param {Object} actions Actions.
10+
* @param {Object} inputProps Input props.
11+
*
12+
* @return {*}
13+
*/
14+
const HiddenInputs = ( { state, actions, inputProps } ) => {
15+
16+
const id = React.useContext( FormIdContext );
17+
const inputName = inputProps.name;
18+
const inputValue = inputProps.value;
19+
20+
actions.gf.addHiddenInputs( { id, inputName, value: inputValue } );
21+
22+
return null;
23+
24+
};
25+
26+
export default connect( HiddenInputs );

src/components/Input.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { useContext } from 'react';
2+
import FormIdContext from "../context/FormIdContext";
3+
import { connect } from "frontity";
4+
5+
/**
6+
* Input Component.
7+
*
8+
* @param {Object} state Frontity State.
9+
* @param {Object} actions Actions methods.
10+
* @param {Object} inputProps Input props.
11+
* @return {*}
12+
*/
13+
const Input = ( { state, actions, inputProps } ) => {
14+
15+
// Context is used so that we can pass the form id to different components.
16+
const id = useContext( FormIdContext );
17+
const inputName = inputProps.name;
18+
const placeholder = inputProps.placeholder;
19+
20+
if ( 'undefined' === typeof ( state.gf.forms[ id ].inputVals[ inputName ] ) ) {
21+
actions.gf.changeInputValue( { id, inputName, value: inputProps.value } );
22+
}
23+
24+
/**
25+
* OnChange handler for input.
26+
*
27+
* @param {Object} event Event.
28+
*
29+
* @return {void}
30+
*/
31+
const onChange = ( event ) => {
32+
33+
actions.gf.changeInputValue( { id, inputName, value: event.target.value } );
34+
35+
};
36+
37+
return (
38+
<input
39+
name={ inputProps.name }
40+
className={ inputProps.className }
41+
id={ inputProps.id }
42+
aria-invalid={ inputProps.ariaInvalid }
43+
aria-required={ inputProps.ariaRequired }
44+
size={ inputProps.size }
45+
type={ inputProps.type }
46+
value={ state.gf.forms[ id ].inputVals[ inputName ] }
47+
onChange={ onChange }
48+
placeholder={ placeholder }
49+
/>
50+
);
51+
};
52+
53+
export default connect( Input );

src/components/Message.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import { connect, styled } from "frontity";
3+
import FormIdContext from "./../context/FormIdContext";
4+
5+
/**
6+
* Message component
7+
*
8+
* Used to display success and error messages.
9+
*
10+
* @param {Object} state Frontity state.
11+
*
12+
* @return {*|string}
13+
*
14+
*/
15+
const Message = ( { state, libraries } ) => {
16+
17+
const id = React.useContext( FormIdContext );
18+
const responseInfo = state.gf.forms[ id ];
19+
20+
// Get the html2react component for the message.
21+
const Html2React = libraries.html2react.Component;
22+
23+
/**
24+
* Get the error or success message
25+
*
26+
* @return {string|*}
27+
*/
28+
const getMessage = () => {
29+
30+
if ( 'sent' === responseInfo.status && typeof responseInfo.message === 'string' ) {
31+
return <SuccessMessage><Html2React html={responseInfo.message} /></SuccessMessage>
32+
} else if ( 'failed' === responseInfo.status ) {
33+
return <ErrorMessage>{responseInfo.message}</ErrorMessage>
34+
} else {
35+
return '';
36+
}
37+
38+
};
39+
40+
return getMessage();
41+
42+
};
43+
44+
const SuccessMessage = styled.div`
45+
border: 2px solid #398f14;
46+
padding: 0.75rem 1.25rem;
47+
`;
48+
49+
const ErrorMessage = styled.div`
50+
border: 2px solid #ff2c18;
51+
padding: 0.75rem 1.25rem;
52+
`;
53+
54+
export default connect( Message );

0 commit comments

Comments
 (0)