| 1 |
/** |
| 2 |
* WordPress Core Dependencies |
| 3 |
*/ |
| 4 |
const { __ } = wp.i18n; |
| 5 |
const { |
| 6 |
PanelBody, |
| 7 |
PanelRow, |
| 8 |
SelectControl, |
| 9 |
TextControl, |
| 10 |
} = wp.components; |
| 11 |
const { useEffect, useState } = wp.element; |
| 12 |
|
| 13 |
/** |
| 14 |
* External Dependencies |
| 15 |
*/ |
| 16 |
import Select2 from 'react-select'; |
| 17 |
import AsyncSelect from 'react-select/async'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Internal Dependencies |
| 21 |
*/ |
| 22 |
import {SOURCES, ORDER_BY, ORDER} from './constants'; |
| 23 |
import { |
| 24 |
getPosts, |
| 25 |
getAuthor, |
| 26 |
getCategory, |
| 27 |
getTag, |
| 28 |
getLatest5Posts, |
| 29 |
getPostsBySearchString |
| 30 |
} from './apiData'; |
| 31 |
|
| 32 |
export default function Inspector(props) { |
| 33 |
const { queryData, queryResults, setAttributes, initialOpen } = props; |
| 34 |
const [authorOptions, setAuthorOptions] = useState([]); //Author Data for Select2 Format |
| 35 |
const [categoryOptions, setCategoryOptions] = useState([]); //Categories Data for Select2 Format |
| 36 |
const [tagOptions, setTagOptions] = useState([]); //Tags Data for Select2 Format |
| 37 |
const [postOptions, setPostOptions] = useState([]); //Include/Exclude post Data for Select2 Format |
| 38 |
const [query, setQuery] = useState({}); |
| 39 |
const [didMount, setDidMount] = useState(false); |
| 40 |
|
| 41 |
useEffect(() => { |
| 42 |
setDidMount(true); |
| 43 |
|
| 44 |
//If Exixsting query data exists in Attributes, set existing data to current query data |
| 45 |
if (typeof(queryData) != 'undefined' && Object.keys(queryData).length > 0) { |
| 46 |
setQuery({...queryData}) |
| 47 |
} |
| 48 |
else { |
| 49 |
setQuery({ |
| 50 |
source: 'posts', |
| 51 |
author: '', |
| 52 |
categories: '', |
| 53 |
tags: '', |
| 54 |
per_page: '6', |
| 55 |
offset: '0', |
| 56 |
orderby: 'date', |
| 57 |
order: 'desc', |
| 58 |
include: '', |
| 59 |
exclude: '' |
| 60 |
}) |
| 61 |
} |
| 62 |
|
| 63 |
//Get Author data and Set Author Options Data |
| 64 |
getAuthor().then((authorData) => { |
| 65 |
let authorDataForOptions = []; |
| 66 |
authorData.length > 0 && authorData.forEach((item) => { |
| 67 |
let filterAuthorData = {}; |
| 68 |
Object.keys(item).forEach(key => { |
| 69 |
if (key === 'name') { |
| 70 |
filterAuthorData.label = item[key]; |
| 71 |
} |
| 72 |
if (key === 'id') { |
| 73 |
filterAuthorData.value = item[key]; |
| 74 |
} |
| 75 |
}); |
| 76 |
authorDataForOptions.push(filterAuthorData); |
| 77 |
}) |
| 78 |
setAuthorOptions(authorDataForOptions) |
| 79 |
}); |
| 80 |
|
| 81 |
//Get Categories data and Set Categories Options Data |
| 82 |
getCategory().then((categoryData) => { |
| 83 |
let categoryDataForOptions = []; |
| 84 |
categoryData.length > 0 && categoryData.forEach((item) => { |
| 85 |
let filterCategoryData = {}; |
| 86 |
Object.keys(item).forEach(key => { |
| 87 |
if (key === 'name') { |
| 88 |
filterCategoryData.label = item[key]; |
| 89 |
} |
| 90 |
if (key === 'id') { |
| 91 |
filterCategoryData.value = item[key]; |
| 92 |
} |
| 93 |
}); |
| 94 |
categoryDataForOptions.push(filterCategoryData); |
| 95 |
}) |
| 96 |
setCategoryOptions(categoryDataForOptions) |
| 97 |
}); |
| 98 |
|
| 99 |
//Get Tags data and Set Tags Options Data |
| 100 |
getTag().then((tagData) => { |
| 101 |
let tagDataForOptions = []; |
| 102 |
tagData.length > 0 && tagData.forEach((item) => { |
| 103 |
let filterTagData = {}; |
| 104 |
Object.keys(item).forEach(key => { |
| 105 |
if (key === 'name') { |
| 106 |
filterTagData.label = item[key]; |
| 107 |
} |
| 108 |
if (key === 'id') { |
| 109 |
filterTagData.value = item[key]; |
| 110 |
} |
| 111 |
}); |
| 112 |
tagDataForOptions.push(filterTagData); |
| 113 |
}) |
| 114 |
setTagOptions(tagDataForOptions) |
| 115 |
}); |
| 116 |
|
| 117 |
//Get Posts data and Set Tags Options Data |
| 118 |
getLatest5Posts().then((postData) => { |
| 119 |
let postDataForOptions = []; |
| 120 |
postData.length > 0 && postData.forEach((item) => { |
| 121 |
let filterPostData = {}; |
| 122 |
Object.keys(item).forEach(key => { |
| 123 |
if (key === 'title') { |
| 124 |
filterPostData.label = item[key].rendered; |
| 125 |
} |
| 126 |
if (key === 'id') { |
| 127 |
filterPostData.value = item[key]; |
| 128 |
} |
| 129 |
}); |
| 130 |
postDataForOptions.push(filterPostData); |
| 131 |
}) |
| 132 |
setPostOptions(postDataForOptions) |
| 133 |
}); |
| 134 |
|
| 135 |
return () => { |
| 136 |
setDidMount(false) |
| 137 |
} |
| 138 |
}, []); |
| 139 |
|
| 140 |
useEffect(() => { |
| 141 |
//Get Post data on query change and set to Attributes |
| 142 |
if (didMount) { |
| 143 |
getPosts(query).then((posts) => { |
| 144 |
setAttributes({queryData: query}) |
| 145 |
setAttributes({queryResults: posts}) |
| 146 |
}); |
| 147 |
} |
| 148 |
|
| 149 |
}, [query]); |
| 150 |
|
| 151 |
//Function for update query data on Source Select |
| 152 |
const setSource = (value) => { |
| 153 |
let updatedQueryData = {...query}; |
| 154 |
updatedQueryData.source = value; |
| 155 |
setQuery(updatedQueryData); |
| 156 |
} |
| 157 |
|
| 158 |
//Function for update query data on Author Select |
| 159 |
const setAuthor = ( author ) => { |
| 160 |
let updatedQueryData = {...query}; |
| 161 |
updatedQueryData.author = JSON.stringify( author ); |
| 162 |
setQuery(updatedQueryData); |
| 163 |
}; |
| 164 |
|
| 165 |
//Function for update query data on Category Select |
| 166 |
const setCategories = ( categories ) => { |
| 167 |
let updatedQueryData = {...query}; |
| 168 |
updatedQueryData.categories = JSON.stringify( categories ); |
| 169 |
setQuery(updatedQueryData); |
| 170 |
}; |
| 171 |
|
| 172 |
//Function for update query data on Tag Select |
| 173 |
const setTags = ( tags ) => { |
| 174 |
let updatedQueryData = {...query}; |
| 175 |
updatedQueryData.tags = JSON.stringify( tags ); |
| 176 |
setQuery(updatedQueryData); |
| 177 |
}; |
| 178 |
|
| 179 |
//Function for update query data on Tag Select |
| 180 |
const setIncludedPost = ( posts ) => { |
| 181 |
let updatedQueryData = {...query}; |
| 182 |
updatedQueryData.include = JSON.stringify( posts ); |
| 183 |
setQuery(updatedQueryData); |
| 184 |
}; |
| 185 |
|
| 186 |
//Function for update query data on Tag Select |
| 187 |
const setExcludePost = ( posts ) => { |
| 188 |
let updatedQueryData = {...query}; |
| 189 |
updatedQueryData.exclude = JSON.stringify( posts ); |
| 190 |
setQuery(updatedQueryData); |
| 191 |
}; |
| 192 |
|
| 193 |
//Function for update query data on Post Per Page Select |
| 194 |
const setPerPage = ( number ) => { |
| 195 |
let updatedQueryData = {...query}; |
| 196 |
updatedQueryData.per_page = number; |
| 197 |
setQuery(updatedQueryData); |
| 198 |
}; |
| 199 |
|
| 200 |
//Function for update query data on Offset Select |
| 201 |
const setOffset = ( number ) => { |
| 202 |
let updatedQueryData = {...query}; |
| 203 |
updatedQueryData.offset = number; |
| 204 |
setQuery(updatedQueryData); |
| 205 |
}; |
| 206 |
|
| 207 |
//Function for update query data on Order By Select |
| 208 |
const setOrderBy = (value) => { |
| 209 |
let updatedQueryData = {...query}; |
| 210 |
updatedQueryData.orderby = value; |
| 211 |
setQuery(updatedQueryData); |
| 212 |
} |
| 213 |
|
| 214 |
//Function for update query data on Order Select |
| 215 |
const setOrder = (value) => { |
| 216 |
let updatedQueryData = {...query}; |
| 217 |
updatedQueryData.order = value; |
| 218 |
setQuery(updatedQueryData); |
| 219 |
} |
| 220 |
|
| 221 |
//Function for update query data on Order Select |
| 222 |
const LoadIncludePosts = (value) => { |
| 223 |
return getPostsBySearchString(value).then((postData) => { |
| 224 |
let postDataForOptions = []; |
| 225 |
postData.length > 0 && postData.forEach((item) => { |
| 226 |
let filterPostData = {}; |
| 227 |
Object.keys(item).forEach(key => { |
| 228 |
if (key === 'title') { |
| 229 |
filterPostData.label = item[key]; |
| 230 |
} |
| 231 |
if (key === 'id') { |
| 232 |
filterPostData.value = item[key]; |
| 233 |
} |
| 234 |
}); |
| 235 |
postDataForOptions.push(filterPostData); |
| 236 |
}) |
| 237 |
return postDataForOptions; |
| 238 |
}); |
| 239 |
} |
| 240 |
|
| 241 |
return ( |
| 242 |
<PanelBody title={__("Query", "betterdocs")} initialOpen={initialOpen}> |
| 243 |
{typeof(query) != 'undefined' && didMount && ( |
| 244 |
<> |
| 245 |
<SelectControl |
| 246 |
label={__("Source", "betterdocs")} |
| 247 |
value={query.source} |
| 248 |
options={SOURCES} |
| 249 |
onChange={(selected) => setSource(selected)} |
| 250 |
/> |
| 251 |
|
| 252 |
<div className="eb-control-item-wrapper"> |
| 253 |
<PanelRow>Author</PanelRow> |
| 254 |
<Select2 |
| 255 |
name='select-author' |
| 256 |
value={ query.author.length > 0 ? JSON.parse( query.author ) : '' } |
| 257 |
onChange={ (selected) => setAuthor(selected) } |
| 258 |
options={ authorOptions } |
| 259 |
isMulti='true' |
| 260 |
/> |
| 261 |
</div> |
| 262 |
|
| 263 |
{query.source === 'posts' && ( |
| 264 |
<> |
| 265 |
<div className="eb-control-item-wrapper"> |
| 266 |
<PanelRow>Categories</PanelRow> |
| 267 |
<Select2 |
| 268 |
name='select-categories' |
| 269 |
value={ query.categories.length > 0 ? JSON.parse( query.categories ) : '' } |
| 270 |
onChange={ (selected) => setCategories(selected) } |
| 271 |
options={ categoryOptions } |
| 272 |
isMulti='true' |
| 273 |
/> |
| 274 |
</div> |
| 275 |
<div className="eb-control-item-wrapper"> |
| 276 |
<PanelRow>Tags</PanelRow> |
| 277 |
<Select2 |
| 278 |
name='select-tags' |
| 279 |
value={ query.tags.length > 0 ? JSON.parse( query.tags ) : '' } |
| 280 |
onChange={ (selected) => setTags(selected) } |
| 281 |
options={ tagOptions } |
| 282 |
isMulti='true' |
| 283 |
/> |
| 284 |
</div> |
| 285 |
</> |
| 286 |
)} |
| 287 |
|
| 288 |
<div className="eb-control-item-wrapper"> |
| 289 |
<PanelRow>Posts Include</PanelRow> |
| 290 |
<AsyncSelect |
| 291 |
cacheOptions |
| 292 |
value={ query.include.length > 0 ? JSON.parse( query.include ) : '' } |
| 293 |
defaultOptions={postOptions} |
| 294 |
loadOptions={LoadIncludePosts} |
| 295 |
onChange={ (selected) => setIncludedPost(selected) } |
| 296 |
isMulti='true' |
| 297 |
/> |
| 298 |
</div> |
| 299 |
|
| 300 |
<div className="eb-control-item-wrapper"> |
| 301 |
<PanelRow>Posts Exclude</PanelRow> |
| 302 |
<AsyncSelect |
| 303 |
cacheOptions |
| 304 |
value={ query.exclude.length > 0 ? JSON.parse( query.exclude ) : '' } |
| 305 |
defaultOptions={postOptions} |
| 306 |
loadOptions={LoadIncludePosts} |
| 307 |
onChange={ (selected) => setExcludePost(selected) } |
| 308 |
isMulti='true' |
| 309 |
/> |
| 310 |
</div> |
| 311 |
|
| 312 |
<TextControl |
| 313 |
label="Posts Per Page" |
| 314 |
type={"number"} |
| 315 |
value={ query.per_page } |
| 316 |
onChange={ (selected) => setPerPage(selected) } |
| 317 |
/> |
| 318 |
|
| 319 |
<TextControl |
| 320 |
label="Offset" |
| 321 |
type={"number"} |
| 322 |
value={ query.offset } |
| 323 |
onChange={ (selected) => setOffset(selected) } |
| 324 |
/> |
| 325 |
|
| 326 |
<SelectControl |
| 327 |
label={__("Order By", "betterdocs")} |
| 328 |
value={query.orderby} |
| 329 |
options={ORDER_BY} |
| 330 |
onChange={(selected) => setOrderBy(selected)} |
| 331 |
/> |
| 332 |
|
| 333 |
<SelectControl |
| 334 |
label={__("Order", "betterdocs")} |
| 335 |
value={query.order} |
| 336 |
options={ORDER} |
| 337 |
onChange={(selected) => setOrder(selected)} |
| 338 |
/> |
| 339 |
</> |
| 340 |
)} |
| 341 |
</PanelBody> |
| 342 |
); |
| 343 |
} |