AnalyticsUpgrade.js
5 years ago
Dashboard.js
4 years ago
User.js
3 years ago
Video.js
3 years ago
illustration.js
5 years ago
Video.js
220 lines
| 1 | const { __ } = wp.i18n; |
| 2 | |
| 3 | const { Flex, FlexBlock, FlexItem, Spinner, Button, TextControl } = |
| 4 | wp.components; |
| 5 | |
| 6 | import { history } from "@/router/context"; |
| 7 | import DatePicker from "../components/DatePicker"; |
| 8 | import VideoAverageWatchTime from "../components/VideoAverageWatchTime"; |
| 9 | import VideoTimeline from "../components/VideoTimeline"; |
| 10 | import VideoViews from "../components/VideoViews"; |
| 11 | import Player from "../../blocks/shared/Player"; |
| 12 | import { getProvider } from "../../blocks/util"; |
| 13 | |
| 14 | const { useEffect, useState } = wp.element; |
| 15 | const { apiFetch } = wp; |
| 16 | |
| 17 | const Video = ({ route, startDate, endDate, setStartDate, setEndDate }) => { |
| 18 | const [loading, setLoading] = useState(true); |
| 19 | const [video, setVideo] = useState({}); |
| 20 | const [error, setError] = useState(""); |
| 21 | const [thisName, setThisName] = useState(null); |
| 22 | const [editing, setEditing] = useState(false); |
| 23 | |
| 24 | const back = () => { |
| 25 | const { search } = history.location; |
| 26 | history.push(`${search}#/`); |
| 27 | }; |
| 28 | |
| 29 | const getVideo = async () => { |
| 30 | setLoading(true); |
| 31 | try { |
| 32 | let video = await apiFetch({ |
| 33 | url: `${prestoPlayer?.root}${prestoPlayer?.prestoVersionString}videos/${route?.params?.id}`, |
| 34 | }); |
| 35 | setVideo(video); |
| 36 | setThisName(video?.title); |
| 37 | } catch (e) { |
| 38 | if (e.code === "rest_no_route") { |
| 39 | setError("Video Not Found"); |
| 40 | } |
| 41 | } finally { |
| 42 | setLoading(false); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | const putVideo = async () => { |
| 47 | console.log(`New Video title ${thisName}`); |
| 48 | setLoading(true); |
| 49 | try { |
| 50 | const data = { |
| 51 | ...video, |
| 52 | ...{ title: thisName }, |
| 53 | }; |
| 54 | let saved = await wp.apiFetch({ |
| 55 | method: "POST", |
| 56 | url: wp.url.addQueryArgs( |
| 57 | `${prestoPlayer.root}${prestoPlayer.prestoVersionString}videos/${video.id}`, |
| 58 | { _method: "PUT" } |
| 59 | ), |
| 60 | data, |
| 61 | }); |
| 62 | |
| 63 | if (!saved) { |
| 64 | throw genericError; |
| 65 | } |
| 66 | setEditing(false); |
| 67 | setVideo(saved); |
| 68 | } catch (e) { |
| 69 | setError(e?.message ? e.message : genericError); |
| 70 | } finally { |
| 71 | setLoading(false); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | const cancelEditing = () => { |
| 76 | setThisName(video?.title); |
| 77 | setEditing(false); |
| 78 | }; |
| 79 | |
| 80 | const renderVideoEditableTitle = () => { |
| 81 | if (loading) { |
| 82 | return <Spinner />; |
| 83 | } else if (editing) { |
| 84 | return ( |
| 85 | <div className="presto-inline-edit presto-inline-edit--editing"> |
| 86 | <TextControl |
| 87 | className="presto-inline-edit__input" |
| 88 | type="text" |
| 89 | value={thisName} |
| 90 | onChange={(title) => setThisName(title)} |
| 91 | /> |
| 92 | <Button |
| 93 | className="presto-inline-edit__button" |
| 94 | isPrimary |
| 95 | onClick={putVideo} |
| 96 | > |
| 97 | {" "} |
| 98 | Save{" "} |
| 99 | </Button> |
| 100 | <Button |
| 101 | className="presto-inline-edit__button" |
| 102 | isSecondary |
| 103 | onClick={cancelEditing} |
| 104 | > |
| 105 | {" "} |
| 106 | Cancel{" "} |
| 107 | </Button> |
| 108 | </div> |
| 109 | ); |
| 110 | } else { |
| 111 | return ( |
| 112 | <div className="presto-inline-edit"> |
| 113 | <h1 className="presto-dashboard__title presto-inline-edit__text"> |
| 114 | {video?.title} |
| 115 | </h1> |
| 116 | |
| 117 | <button |
| 118 | className="presto-inline-edit__edit" |
| 119 | onClick={() => setEditing(true)} |
| 120 | > |
| 121 | <span className="dashicon dashicons dashicons-edit"></span> |
| 122 | </button> |
| 123 | </div> |
| 124 | ); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | useEffect(() => { |
| 129 | getVideo(); |
| 130 | }, []); |
| 131 | |
| 132 | if (error) { |
| 133 | return ( |
| 134 | <div className="presto-flow"> |
| 135 | <Flex> |
| 136 | <FlexBlock> |
| 137 | <h2>{error}</h2> |
| 138 | </FlexBlock> |
| 139 | </Flex> |
| 140 | </div> |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | return ( |
| 145 | <div className="presto-flow"> |
| 146 | <Flex> |
| 147 | <FlexBlock> |
| 148 | <Button isSecondary onClick={back}> |
| 149 | ← {__("Back to Dashboard", "presto-player")} |
| 150 | </Button> |
| 151 | </FlexBlock> |
| 152 | </Flex> |
| 153 | <Flex wrap> |
| 154 | <FlexBlock>{renderVideoEditableTitle()}</FlexBlock> |
| 155 | <FlexItem> |
| 156 | <DatePicker |
| 157 | startDate={startDate} |
| 158 | setStartDate={setStartDate} |
| 159 | endDate={endDate} |
| 160 | setEndDate={setEndDate} |
| 161 | /> |
| 162 | </FlexItem> |
| 163 | </Flex> |
| 164 | |
| 165 | <div className="presto-dashboard presto-flow"> |
| 166 | <div className="presto-dashboard__row"> |
| 167 | <div className="presto-dashboard__item is-large"> |
| 168 | <VideoViews |
| 169 | video_id={route?.params?.id} |
| 170 | startDate={startDate} |
| 171 | endDate={endDate} |
| 172 | /> |
| 173 | </div> |
| 174 | <div className="presto-dashboard__item"> |
| 175 | {!!Object.keys(video || {}).length && ( |
| 176 | <Player |
| 177 | src={video?.src} |
| 178 | attributes={{ |
| 179 | title: video.title, |
| 180 | }} |
| 181 | type={getProvider(video.src)} |
| 182 | preset={{ |
| 183 | "play-large": true, |
| 184 | play: true, |
| 185 | progress: true, |
| 186 | rewind: true, |
| 187 | "fast-forward": true, |
| 188 | "current-time": true, |
| 189 | background_color: "#8421cb", |
| 190 | volume: true, |
| 191 | mute: true, |
| 192 | i18n: window.prestoPlayer.i18n, |
| 193 | }} |
| 194 | /> |
| 195 | )} |
| 196 | </div> |
| 197 | <div className="presto-dashboard__item"> |
| 198 | <VideoAverageWatchTime |
| 199 | video_id={route?.params?.id} |
| 200 | startDate={startDate} |
| 201 | endDate={endDate} |
| 202 | /> |
| 203 | </div> |
| 204 | </div> |
| 205 | <div className="presto-dashboard__row"> |
| 206 | <div className="presto-dashboard__item is-large"> |
| 207 | <VideoTimeline |
| 208 | video_id={route?.params?.id} |
| 209 | startDate={startDate} |
| 210 | endDate={endDate} |
| 211 | /> |
| 212 | </div> |
| 213 | </div> |
| 214 | </div> |
| 215 | </div> |
| 216 | ); |
| 217 | }; |
| 218 | |
| 219 | export default Video; |
| 220 |