Skip to content

Commit 31a1ea9

Browse files
Merge pull request #34 from heikkivihersalo/feature/31/refactor-admin-area
Refactor admin area to cleaner code
2 parents 02f41f0 + e8785e4 commit 31a1ea9

32 files changed

Lines changed: 279 additions & 632 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { __ } from '@wordpress/i18n';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import styles from '../index.module.css';
10+
11+
/**
12+
* Header
13+
* @return {JSX.Element} Form Button Component
14+
*/
15+
const Header = (): JSX.Element => {
16+
return (
17+
<header className={styles.header}>
18+
<h1 className={styles.headerHeading}>
19+
{__('Gutenberg Native AI Settings', 'gutenberg-native-ai')}
20+
</h1>
21+
</header>
22+
);
23+
};
24+
25+
export default Header;

src/features/admin/components/containers/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/features/admin/components/containers/lib/Option.module.css

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/features/admin/components/containers/lib/Option.tsx

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/features/admin/components/containers/lib/OptionBody.module.css

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/features/admin/components/containers/lib/OptionBody.tsx

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/features/admin/components/containers/lib/OptionGroup.module.css

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/features/admin/components/containers/lib/OptionGroup.tsx

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { __ } from '@wordpress/i18n';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import { useAdminForm } from '@hooks';
10+
import FormInput from './FormInput';
11+
import FormSelect from './FormSelect';
12+
13+
import {
14+
TONE_OF_VOICE,
15+
OPEN_AI_TEXT_MODEL,
16+
OPEN_AI_IMAGE_MODEL,
17+
} from '@constants/options';
18+
19+
import type { ChatGPTFormData } from 'types/admin';
20+
21+
import styles from '../../index.module.css';
22+
23+
/**
24+
* Header Component
25+
* @return {JSX.Element | null} Header Component
26+
*/
27+
const Form = (): JSX.Element | null => {
28+
const { formData, handleChange, handleSave } = useAdminForm({
29+
path: 'gutenberg-native-ai/v1/settings',
30+
nonce: window.GUTENBERG_NATIVE_AI?.nonce,
31+
});
32+
33+
/**
34+
* Return early if form is not set and loaded
35+
*/
36+
if (!formData) {
37+
return null;
38+
}
39+
40+
return (
41+
<form
42+
className={styles.form}
43+
onSubmit={(e) => {
44+
e.preventDefault();
45+
handleSave(formData);
46+
}}
47+
>
48+
<h3 className={styles.formHeading}>
49+
{__('API Settings', 'gutenberg-native-ai')}
50+
</h3>
51+
<div className={styles.formInputContainer}>
52+
<FormSelect
53+
label={__('Model (Text)', 'gutenberg-native-ai')}
54+
name="model_text"
55+
value={(formData as ChatGPTFormData).model_text}
56+
options={OPEN_AI_TEXT_MODEL}
57+
onChange={
58+
handleChange as React.ChangeEventHandler<HTMLSelectElement>
59+
}
60+
/>
61+
<FormSelect
62+
label={__('Model (Image)', 'gutenberg-native-ai')}
63+
name="model_image"
64+
value={(formData as ChatGPTFormData).model_image}
65+
options={OPEN_AI_IMAGE_MODEL}
66+
onChange={
67+
handleChange as React.ChangeEventHandler<HTMLSelectElement>
68+
}
69+
/>
70+
<FormInput
71+
type="text"
72+
label={__('API Key', 'gutenberg-native-ai')}
73+
name="api_key"
74+
placeholder="sk-proj-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
75+
value={(formData as ChatGPTFormData).api_key}
76+
onChange={
77+
handleChange as React.ChangeEventHandler<HTMLInputElement>
78+
}
79+
/>
80+
<FormSelect
81+
label={__('Tone of Voice', 'gutenberg-native-ai')}
82+
name="tone_of_voice"
83+
value={(formData as ChatGPTFormData).tone_of_voice}
84+
options={TONE_OF_VOICE}
85+
onChange={
86+
handleChange as React.ChangeEventHandler<HTMLSelectElement>
87+
}
88+
/>
89+
</div>
90+
<button type="submit" className={styles.formButton}>
91+
{__('Save Changes', 'gutenberg-native-ai')}
92+
</button>
93+
</form>
94+
);
95+
};
96+
97+
export default Form;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { __, sprintf } from '@wordpress/i18n';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import styles from '../../index.module.css';
10+
11+
/**
12+
* FormContainer Component
13+
* @param {Object} props - Component props
14+
* @param {React.ReactNode} props.children - Child components
15+
* @return {JSX.Element} FormContainer Component
16+
*/
17+
const FormContainer = ({
18+
children,
19+
}: {
20+
children: React.ReactNode;
21+
}): JSX.Element => {
22+
return (
23+
<div className={styles.formContainer}>
24+
<h2 className={styles.formContainerHeading}>
25+
{__('ChatGPT', 'gutenberg-native-ai')}
26+
</h2>
27+
<p
28+
className={styles.formContainerDescription}
29+
dangerouslySetInnerHTML={{
30+
__html: sprintf(
31+
/* translators: %s: ChatGPT dashboard URL */
32+
__(
33+
'Configure the ChatGPT API settings. You can get the API key from the ChatGPT dashboard. Access the dashboard <a href="%s">here</a>.',
34+
'gutenberg-native-ai'
35+
),
36+
'https://platform.openai.com/api-keys'
37+
),
38+
}}
39+
/>
40+
{children}
41+
</div>
42+
);
43+
};
44+
45+
export default FormContainer;

0 commit comments

Comments
 (0)