ColorPopup.js
2 years ago
EntitySearchDropdown.js
2 years ago
LoadSelect.js
5 years ago
ProBadge.js
5 years ago
SelectMediaDropdown.js
2 years ago
UrlSelect.js
2 years ago
VideoIcon.js
2 years ago
SelectMediaDropdown.js
151 lines
| 1 | import { useState, useEffect } from "@wordpress/element"; |
| 2 | import { __ } from "@wordpress/i18n"; |
| 3 | import { addQueryArgs } from "@wordpress/url"; |
| 4 | import { store as noticesStore } from "@wordpress/notices"; |
| 5 | import { store as coreStore } from "@wordpress/core-data"; |
| 6 | import apiFetch from "@wordpress/api-fetch"; |
| 7 | import { select, useDispatch } from "@wordpress/data"; |
| 8 | import debounce from "debounce-promise"; |
| 9 | import EntitySearchDropdown from "./EntitySearchDropdown"; |
| 10 | import VideoIcon from "./VideoIcon"; |
| 11 | import { Button, MenuItem } from "@wordpress/components"; |
| 12 | import { capitalize } from "../../util"; |
| 13 | |
| 14 | const SelectMediaDropdown = ({ onSelect, value, ...props }) => { |
| 15 | const [search, setSearch] = useState(""); |
| 16 | const [page, setPage] = useState(1); |
| 17 | const [isLoading, setIsLoading] = useState(false); |
| 18 | const [videoList, setVideoList] = useState([]); |
| 19 | const [totalPages, setTotalPages] = useState(0); |
| 20 | const { createErrorNotice } = useDispatch(noticesStore); |
| 21 | const { receiveEntityRecords } = useDispatch(coreStore); |
| 22 | |
| 23 | // handle the selection of a video. |
| 24 | const handleSelection = async (video) => { |
| 25 | if (!video?.id) return; |
| 26 | const item = await select(coreStore).getEntityRecord( |
| 27 | "postType", |
| 28 | "pp_video_block", |
| 29 | video?.id |
| 30 | ); |
| 31 | onSelect(item); |
| 32 | }; |
| 33 | |
| 34 | // debounce the search. |
| 35 | const debounceSearch = debounce( |
| 36 | () => { |
| 37 | setPage(1); // reset the page. |
| 38 | setVideoList(null); // clear the videos. |
| 39 | doFetch(); // fetch the videos. |
| 40 | }, |
| 41 | 500, |
| 42 | { |
| 43 | leading: true, |
| 44 | } |
| 45 | ); |
| 46 | |
| 47 | // when the search term changes, do a debounce search. |
| 48 | useEffect(() => { |
| 49 | debounceSearch(search); |
| 50 | }, [search]); |
| 51 | |
| 52 | // when the page changes, fetch the videos. |
| 53 | useEffect(() => { |
| 54 | doFetch(); |
| 55 | }, [page]); |
| 56 | |
| 57 | // check if there are more pages. |
| 58 | const hasMore = page < totalPages; |
| 59 | |
| 60 | // set the next page. |
| 61 | const nextPage = () => { |
| 62 | let newPage = page + 1; |
| 63 | newPage = newPage > totalPages ? totalPages : newPage; |
| 64 | setPage(newPage); |
| 65 | }; |
| 66 | |
| 67 | // Fetch videos from the server. |
| 68 | const doFetch = async () => { |
| 69 | try { |
| 70 | setIsLoading(true); |
| 71 | |
| 72 | const baseURL = select(coreStore).getEntityConfig( |
| 73 | "postType", |
| 74 | "pp_video_block" |
| 75 | ).baseURL; |
| 76 | |
| 77 | const res = await apiFetch({ |
| 78 | path: addQueryArgs(baseURL, { |
| 79 | search, |
| 80 | page, |
| 81 | per_page: 10, |
| 82 | _embed: 1, |
| 83 | }), |
| 84 | parse: false, |
| 85 | }); |
| 86 | |
| 87 | const videos = await res.json(); |
| 88 | |
| 89 | setTotalPages(parseInt(res.headers.get("X-WP-TotalPages"))); |
| 90 | receiveEntityRecords("postType", "pp_video_block", videos); |
| 91 | |
| 92 | if (!search && page > 1) { |
| 93 | setVideoList([...videoList, ...videos]); |
| 94 | } else { |
| 95 | setVideoList(videos); |
| 96 | } |
| 97 | } catch (error) { |
| 98 | createErrorNotice( |
| 99 | error?.message || __("Something went wrong", "presto-player"), |
| 100 | { type: "snackbar" } |
| 101 | ); |
| 102 | } finally { |
| 103 | setIsLoading(false); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | // convert single value to array. |
| 108 | const disabledItems = !Array.isArray(value) ? [value] : value; |
| 109 | |
| 110 | return ( |
| 111 | <EntitySearchDropdown |
| 112 | popoverProps={{ placement: "bottom-end" }} |
| 113 | isLoading={isLoading} |
| 114 | options={videoList || []} |
| 115 | search={search} |
| 116 | onSearch={setSearch} |
| 117 | onSelect={handleSelection} |
| 118 | hasMore={hasMore && !search} |
| 119 | onNextPage={nextPage} |
| 120 | renderToggle={({ isOpen, onToggle }) => ( |
| 121 | <Button variant="primary" onClick={onToggle} aria-expanded={isOpen}> |
| 122 | {__("Create or select media", "presto-player")} |
| 123 | </Button> |
| 124 | )} |
| 125 | renderItem={({ item, onSelect }) => { |
| 126 | const { id, title, details } = item; |
| 127 | const { type } = details || {}; |
| 128 | const thumbnail = |
| 129 | item?._embedded?.["wp:featuredmedia"]?.[0]?.source_url || ""; |
| 130 | return ( |
| 131 | <MenuItem |
| 132 | icon={<VideoIcon thumbnail={thumbnail} type={type} />} |
| 133 | iconPosition="left" |
| 134 | suffix={ |
| 135 | type ? capitalize(type) : __("Choose media", "presto-player") |
| 136 | } |
| 137 | onClick={() => onSelect(item)} |
| 138 | disabled={(disabledItems || []).includes(id)} |
| 139 | key={id} |
| 140 | > |
| 141 | {title?.raw || __("Untitled", "presto-player")} |
| 142 | </MenuItem> |
| 143 | ); |
| 144 | }} |
| 145 | {...props} |
| 146 | /> |
| 147 | ); |
| 148 | }; |
| 149 | |
| 150 | export default SelectMediaDropdown; |
| 151 |