1
- import { Icon } from '@wordpress/components' ;
1
+ import { Icon , Button } from '@wordpress/components' ;
2
2
import { closeSmall , check } from '@wordpress/icons' ;
3
+ import { useSelect } from '@wordpress/data' ;
4
+ import { saveSettings } from '../../utils/api' ;
5
+
6
+ const OptimizationStatus = ( { settings, setSettings, setCanSave, setTab } ) => {
7
+
8
+ const { isLoading } = useSelect ( select => {
9
+ const { isLoading } = select ( 'optimole' ) ;
10
+ return {
11
+ isLoading : isLoading ( )
12
+ } ;
13
+ } , [ ] ) ;
3
14
4
- const OptimizationStatus = ( { settings } ) => {
5
15
const userStatus = optimoleDashboardApp . user_status ? optimoleDashboardApp . user_status : 'inactive' ;
6
- const lazyloadEnabled = 'enabled' === settings ?. lazyload && 'active' === userStatus ;
16
+ const lazyloadEnabled = 'enabled' === settings ?. lazyload && 'active' === userStatus ;
7
17
const imageHandlingEnabled = 'enabled' === settings ?. image_replacer && 'active' === userStatus ;
18
+ const directUpdate = ( option , value ) => {
19
+ if ( setCanSave && setSettings ) {
20
+ setCanSave ( true ) ;
21
+ const data = { ...settings } ;
22
+
23
+ data [ option ] = value ? 'enabled' : 'disabled' ;
24
+
25
+ if ( 'scale' === option && data . scale && 'disabled' === data . scale && 'disabled' === data . lazyload ) {
26
+ data . lazyload = 'enabled' ;
27
+ }
28
+
29
+ if ( 'lazyload' === option && data . lazyload && 'disabled' === data . lazyload && 'disabled' === data . scale ) {
30
+ data . scale = 'enabled' ;
31
+ }
32
+
33
+ setSettings ( data ) ;
34
+
35
+ saveSettings (
36
+ data ,
37
+ false ,
38
+ false ,
39
+ ( ) => {
40
+ setCanSave ( false ) ;
41
+ }
42
+ ) ;
43
+ }
44
+ } ;
45
+
46
+ const handleSettingsNavigation = ( ) => {
47
+ if ( setTab ) {
48
+ setTab ( 'settings' ) ;
49
+ }
50
+ } ;
51
+
52
+
8
53
const statuses = [
9
54
{
10
55
active : imageHandlingEnabled ,
11
56
label : optimoleDashboardApp . strings . optimization_status . statusTitle1 ,
12
- description : optimoleDashboardApp . strings . optimization_status . statusSubTitle1
57
+ description : optimoleDashboardApp . strings . optimization_status . statusSubTitle1 ,
58
+ buttonText : imageHandlingEnabled ? optimoleDashboardApp . strings . optimization_status . manage : optimoleDashboardApp . strings . optimization_status . enable ,
59
+ settingType : 'image_replacer'
13
60
} ,
14
61
{
15
62
active : lazyloadEnabled ,
16
63
label : optimoleDashboardApp . strings . optimization_status . statusTitle2 ,
17
- description : optimoleDashboardApp . strings . optimization_status . statusSubTitle2
64
+ description : optimoleDashboardApp . strings . optimization_status . statusSubTitle2 ,
65
+ buttonText : lazyloadEnabled ? optimoleDashboardApp . strings . optimization_status . disable : optimoleDashboardApp . strings . optimization_status . enable ,
66
+ settingType : 'lazyload'
18
67
} ,
19
68
{
20
69
active : lazyloadEnabled && 'disabled' === settings ?. scale ,
21
70
label : optimoleDashboardApp . strings . optimization_status . statusTitle3 ,
22
- description : optimoleDashboardApp . strings . optimization_status . statusSubTitle3
71
+ description : optimoleDashboardApp . strings . optimization_status . statusSubTitle3 ,
72
+ buttonText : ( lazyloadEnabled && 'disabled' === settings ?. scale ) ? optimoleDashboardApp . strings . optimization_status . disable : optimoleDashboardApp . strings . optimization_status . enable ,
73
+ settingType : 'scale'
23
74
}
24
75
] . map ( el => ( {
25
- ...el , active : imageHandlingEnabled && el . active
76
+ ...el , active : imageHandlingEnabled && el . active ,
77
+ buttonText : 'image_replacer' === el . settingType ?
78
+ ( imageHandlingEnabled ? el . buttonText : optimoleDashboardApp . strings . optimization_status . enable ) :
79
+ ( imageHandlingEnabled ? el . buttonText : optimoleDashboardApp . strings . optimization_status . block )
26
80
} ) ) ;
27
81
28
82
return (
@@ -32,20 +86,47 @@ const OptimizationStatus = ({ settings }) => {
32
86
{ statuses . map ( ( status , index ) => (
33
87
< li
34
88
key = { index }
35
- className = "flex items-start gap-2"
89
+ className = "flex items-start gap-2 justify-between "
36
90
>
37
- { status . active ? (
38
- < Icon icon = { check } className = "fill-success bg-success/20 rounded-full" size = { 20 } />
39
- ) : (
40
- < Icon icon = { closeSmall } className = "fill-danger bg-danger/20 rounded-full" size = { 20 } />
41
- ) }
42
- < div >
43
- < span className = 'text-gray-700 font-normal font-semibold' >
44
- { status . label }
45
- </ span >
46
-
47
- < p className = "m-0" > { status . description } </ p >
91
+ < div className = "flex items-start gap-2" >
92
+ { status . active ? (
93
+ < Icon icon = { check } className = "fill-success bg-success/20 rounded-full" size = { 20 } />
94
+ ) : (
95
+ < Icon icon = { closeSmall } className = "fill-danger bg-danger/20 rounded-full" size = { 20 } />
96
+ ) }
97
+ < div >
98
+ < span className = 'text-gray-700 font-normal font-semibold' >
99
+ { status . label }
100
+ </ span >
101
+ < p className = "m-0" > { status . description } </ p >
102
+ </ div >
48
103
</ div >
104
+ < Button
105
+ variant = "link"
106
+ className = "text-info text-sm font-medium"
107
+ style = { { textDecoration : 'none' } }
108
+ onClick = { ( ) => {
109
+ if ( 'image_replacer' === status . settingType && status . active ) {
110
+ handleSettingsNavigation ( ) ;
111
+ return ;
112
+ }
113
+
114
+ if ( 'scale' === status . settingType ) {
115
+ status . active = ! status . active ;
116
+ }
117
+
118
+ if ( 'disabled' === settings ?. image_replacer && 'image_replacer' !== status . settingType ) {
119
+ return ;
120
+ }
121
+
122
+ directUpdate ( status . settingType , ! status . active ) ;
123
+ } }
124
+
125
+ isBusy = { isLoading }
126
+ disabled = { isLoading }
127
+ >
128
+ { status . buttonText }
129
+ </ Button >
49
130
</ li >
50
131
) ) }
51
132
</ ul >
0 commit comments