Skip to content

Commit 05ea558

Browse files
Merge pull request #36 from heikkivihersalo/feature/35/move-redux-store-to-upper-level
Move redux store to upper level and various bug fixes
2 parents 31a1ea9 + 1b81a4c commit 05ea558

19 files changed

Lines changed: 68 additions & 64 deletions

File tree

includes/api/class-utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function update_chatgpt_settings( \WP_REST_Request $request ): arr
7474
}
7575

7676
$encryptor = new Encryption();
77-
$encrypted_api_key = $encryptor->encrypt( $body['api_key'] ?? $current['api_key'] ?? '' );
77+
$encrypted_api_key = $encryptor->encrypt( $body['api_key'] ?? '' );
7878

7979
$update = update_option(
8080
'gutenberg_native_ai',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const DATA_STORE = 'gutenberg-native-ai';
2+
13
export const ACTIONS = {
24
GET_TEXT: 'GET_TEXT',
35
SET_TEXT: 'SET_TEXT',

src/features/admin/components/form/Form.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import { __ } from '@wordpress/i18n';
77
* Internal dependencies
88
*/
99
import { useAdminForm } from '@hooks';
10-
import FormInput from './FormInput';
11-
import FormSelect from './FormSelect';
12-
10+
import { API_PATH } from '@constants/api';
1311
import {
1412
TONE_OF_VOICE,
1513
OPEN_AI_TEXT_MODEL,
1614
OPEN_AI_IMAGE_MODEL,
1715
} from '@constants/options';
1816

17+
import FormInput from './FormInput';
18+
import FormSelect from './FormSelect';
19+
1920
import type { ChatGPTFormData } from 'types/admin';
2021

2122
import styles from '../../index.module.css';
@@ -26,7 +27,7 @@ import styles from '../../index.module.css';
2627
*/
2728
const Form = (): JSX.Element | null => {
2829
const { formData, handleChange, handleSave } = useAdminForm({
29-
path: 'gutenberg-native-ai/v1/settings',
30+
path: API_PATH.GET_SETTINGS,
3031
nonce: window.GUTENBERG_NATIVE_AI?.nonce,
3132
});
3233

@@ -42,7 +43,7 @@ const Form = (): JSX.Element | null => {
4243
className={styles.form}
4344
onSubmit={(e) => {
4445
e.preventDefault();
45-
handleSave(formData);
46+
handleSave({ data: formData });
4647
}}
4748
>
4849
<h3 className={styles.formHeading}>

src/features/ai-modal/features/BlockEditControl.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
1010
* Internal dependencies
1111
*/
1212
import { MODAL_STATUS, ALLOWED_TEXT_BLOCKS } from '@constants/modal';
13+
import { DATA_STORE } from '@constants/stores';
1314

1415
import Modal from './modal';
1516

@@ -42,11 +43,11 @@ const BlockEditControl = createHigherOrderComponent(function (
4243
*/
4344
const { mode } = useSelect((select: WPAny) => {
4445
return {
45-
mode: select('theme/ai').getMode() as ModalMode,
46+
mode: select(DATA_STORE).getMode() as ModalMode,
4647
};
4748
}, []);
4849

49-
const { setStatus, setSelection } = useDispatch('theme/ai');
50+
const { setStatus, setSelection } = useDispatch(DATA_STORE);
5051

5152
/**
5253
* Handle keyboard shortcut to open the popover

src/features/ai-modal/features/modal/components/form/Form.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useDispatch } from '@wordpress/data';
77
* Internal dependencies
88
*/
99
import { MODAL_STATUS } from '@constants/modal';
10+
import { DATA_STORE } from '@constants/stores';
1011
import CloseButton from '../CloseButton';
1112

1213
import styles from '../../index.module.css';
@@ -28,7 +29,7 @@ const Form = ({
2829
onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
2930
hasApiKey: boolean;
3031
}): JSX.Element => {
31-
const { setStatus, setSelection } = useDispatch('theme/ai');
32+
const { setStatus, setSelection } = useDispatch(DATA_STORE);
3233

3334
const handleClose = () => {
3435
setStatus(MODAL_STATUS.INITIAL);

src/features/ai-modal/features/modal/components/modal-image/ModalControlsImage.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import Settings from '../../../settings';
1919
import ImagePreview from './ImagePreview';
2020

2121
import { MODAL_STATUS } from '@constants/modal';
22+
import { DATA_STORE } from '@constants/stores';
2223

23-
import type { ModalStatus, ModalSelection } from 'types/modal';
24+
import type { ModalSettings, ModalStatus } from 'types/modal';
2425

2526
import styles from '../../index.module.css';
2627

@@ -30,14 +31,14 @@ import styles from '../../index.module.css';
3031
*/
3132
const ModalImage = (): JSX.Element | null => {
3233
const [preview, setPreview] = useState<ChatGPTImage[] | null>(null);
33-
const { status } = useSelect((select: WPAny) => {
34+
const { status, settings } = useSelect((select: WPAny) => {
3435
return {
35-
status: select('theme/ai').getStatus() as ModalStatus,
36-
selection: select('theme/ai').getSelection() as ModalSelection,
36+
status: select(DATA_STORE).getStatus() as ModalStatus,
37+
settings: select(DATA_STORE).getSettings() as ModalSettings,
3738
};
3839
}, []);
3940

40-
const { setStatus, setSelection } = useDispatch('theme/ai');
41+
const { setStatus, setSelection } = useDispatch(DATA_STORE);
4142
const { getImage } = useChatGPT();
4243

4344
const handleCLose = () => {
@@ -85,7 +86,7 @@ const ModalImage = (): JSX.Element | null => {
8586
};
8687

8788
return (
88-
<Form onSubmit={generateContent}>
89+
<Form onSubmit={generateContent} hasApiKey={settings.has_api_key}>
8990
<div className={styles.controlContainer}>
9091
<FormInput
9192
placeholder={__(

src/features/ai-modal/features/modal/components/modal-text/ModalControlsText.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '../../../../utils';
2424

2525
import { MODAL_STATUS } from '@constants/modal';
26+
import { DATA_STORE } from '@constants/stores';
2627

2728
import type { ModalStatus, ModalSelection, ModalSettings } from 'types/modal';
2829

@@ -35,13 +36,13 @@ import styles from '../../index.module.css';
3536
const ModalControlsText = (): JSX.Element | null => {
3637
const { status, selection, settings } = useSelect((select: WPAny) => {
3738
return {
38-
status: select('theme/ai').getStatus() as ModalStatus,
39-
selection: select('theme/ai').getSelection() as ModalSelection,
40-
settings: select('theme/ai').getSettings() as ModalSettings,
39+
status: select(DATA_STORE).getStatus() as ModalStatus,
40+
selection: select(DATA_STORE).getSelection() as ModalSelection,
41+
settings: select(DATA_STORE).getSettings() as ModalSettings,
4142
};
4243
}, []);
4344

44-
const { setStatus, setSelection } = useDispatch('theme/ai');
45+
const { setStatus, setSelection } = useDispatch(DATA_STORE);
4546
const { getText } = useChatGPT();
4647

4748
const handleCLose = () => {

src/features/ai-modal/features/modal/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useSelect } from '@wordpress/data';
88
* Internal dependencies
99
*/
1010
import { MODAL_STATUS } from '@constants/modal';
11+
import { DATA_STORE } from '@constants/stores';
1112

1213
import ModalControlsText from './components/modal-text/ModalControlsText';
1314
import ModalControlsImage from './components/modal-image/ModalControlsImage';
@@ -30,7 +31,7 @@ const Modal = ({
3031
}): JSX.Element | null => {
3132
const { status } = useSelect((select: WPAny) => {
3233
return {
33-
status: select('theme/ai').getStatus() as ModalStatus,
34+
status: select(DATA_STORE).getStatus() as ModalStatus,
3435
};
3536
}, []);
3637

src/features/ai-modal/features/settings/components/ModelNav.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { useSelect, useDispatch } from '@wordpress/data';
77
/**
88
* Internal dependencies
99
*/
10+
import { DATA_STORE } from '@constants/stores';
1011
import ModelNavItem from './ModelNavItem';
12+
1113
import type { ModalMode } from 'types/modal';
1214

1315
import style from '../index.module.css';
@@ -19,11 +21,11 @@ import style from '../index.module.css';
1921
const ModelNav = () => {
2022
const { mode } = useSelect((select: WPAny) => {
2123
return {
22-
mode: select('theme/ai').getMode() as ModalMode,
24+
mode: select(DATA_STORE).getMode() as ModalMode,
2325
};
2426
}, []);
2527

26-
const { setMode } = useDispatch('theme/ai');
28+
const { setMode } = useDispatch(DATA_STORE);
2729

2830
return (
2931
<div className={style.navContainer}>

src/features/ai-modal/features/settings/components/QuickCommandsNav.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import style from '../index.module.css';
1414
* Quick commands navigation component
1515
* @return {JSX.Element} Quick commands navigation component
1616
*/
17-
const QuickCommandsNav = () => {
17+
const QuickCommandsNav = (): JSX.Element => {
1818
return (
1919
<div className={style.navContainer}>
2020
<div className={style.navLabel}>

0 commit comments

Comments
 (0)