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
User.js
173 lines
| 1 | /** @jsx jsx */ |
| 2 | |
| 3 | const { __ } = wp.i18n; |
| 4 | |
| 5 | const { Flex, FlexBlock, FlexItem, Spinner, Button } = wp.components; |
| 6 | |
| 7 | import { history } from "@/router/context"; |
| 8 | import DatePicker from "../components/DatePicker"; |
| 9 | import TopVideos from "../components/TopVideos"; |
| 10 | import TotalVideoViewsByUser from "../components/TotalVideoViewsByUser"; |
| 11 | import VideoAverageWatchTimeByUser from "../components/VideoAverageWatchTimeByUser"; |
| 12 | import VideoTotalWatchTimeByUser from "../components/VideoTotalWatchTimeByUser"; |
| 13 | |
| 14 | const { useEffect, useState } = wp.element; |
| 15 | const { apiFetch } = wp; |
| 16 | |
| 17 | import { css, jsx } from "@emotion/core"; |
| 18 | |
| 19 | const User = ({ route, startDate, endDate, setStartDate, setEndDate }) => { |
| 20 | const [loading, setLoading] = useState(true); |
| 21 | const [user, setUser] = useState({}); |
| 22 | const [error, setError] = useState(""); |
| 23 | |
| 24 | const back = () => { |
| 25 | const { search } = history.location; |
| 26 | history.push(`${search}#/`); |
| 27 | }; |
| 28 | |
| 29 | const getUser = async () => { |
| 30 | setLoading(true); |
| 31 | try { |
| 32 | let user = await apiFetch({ |
| 33 | url: `/wp-json/wp/v2/users/${route?.params?.id}?context=edit`, |
| 34 | }); |
| 35 | setUser(user); |
| 36 | } catch (e) { |
| 37 | if (e.code === "rest_no_route") { |
| 38 | setError("User Not Found"); |
| 39 | } |
| 40 | } finally { |
| 41 | setLoading(false); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | useEffect(() => { |
| 46 | getUser(); |
| 47 | }, []); |
| 48 | |
| 49 | if (error) { |
| 50 | return ( |
| 51 | <div className="presto-flow"> |
| 52 | <Flex> |
| 53 | <FlexBlock> |
| 54 | <h2>{error}</h2> |
| 55 | </FlexBlock> |
| 56 | </Flex> |
| 57 | </div> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | return ( |
| 62 | <div className="presto-flow"> |
| 63 | <Flex> |
| 64 | <FlexBlock> |
| 65 | <Button isSecondary onClick={back}> |
| 66 | ← {__("Back to Dashboard", "presto-player")} |
| 67 | </Button> |
| 68 | </FlexBlock> |
| 69 | </Flex> |
| 70 | <Flex wrap> |
| 71 | <FlexBlock> |
| 72 | {loading ? ( |
| 73 | <Spinner /> |
| 74 | ) : ( |
| 75 | <div |
| 76 | css={css` |
| 77 | display: flex; |
| 78 | align-items: center; |
| 79 | |
| 80 | .presto__edit-user-button { |
| 81 | opacity: 0; |
| 82 | visibility: hidden; |
| 83 | transition: opacity 0.3s ease; |
| 84 | } |
| 85 | |
| 86 | &:hover { |
| 87 | .presto__edit-user-button { |
| 88 | opacity: 1; |
| 89 | visibility: visible; |
| 90 | } |
| 91 | } |
| 92 | `} |
| 93 | > |
| 94 | <div> |
| 95 | <h1 className="presto-dashboard__title">{user?.name}</h1> |
| 96 | <p |
| 97 | css={css` |
| 98 | margin-top: -10px; |
| 99 | opacity: 0.65; |
| 100 | `} |
| 101 | > |
| 102 | {user?.email} |
| 103 | </p> |
| 104 | </div> |
| 105 | {!!user.id && ( |
| 106 | <div |
| 107 | className="presto__edit-user-button" |
| 108 | css={css` |
| 109 | margin: 0 20px; |
| 110 | `} |
| 111 | > |
| 112 | <Button |
| 113 | href={`/wp-admin/user-edit.php?user_id=${user?.id}`} |
| 114 | isSecondary |
| 115 | isSmall |
| 116 | > |
| 117 | {__("View Profile", "presto-player")} → |
| 118 | </Button> |
| 119 | </div> |
| 120 | )} |
| 121 | </div> |
| 122 | )} |
| 123 | </FlexBlock> |
| 124 | <FlexItem> |
| 125 | <DatePicker |
| 126 | startDate={startDate} |
| 127 | setStartDate={setStartDate} |
| 128 | endDate={endDate} |
| 129 | setEndDate={setEndDate} |
| 130 | /> |
| 131 | </FlexItem> |
| 132 | </Flex> |
| 133 | |
| 134 | <div className="presto-dashboard presto-flow"> |
| 135 | <div className="presto-dashboard__row"> |
| 136 | <div className="presto-dashboard__item"> |
| 137 | <TotalVideoViewsByUser |
| 138 | userId={route?.params?.id} |
| 139 | startDate={startDate} |
| 140 | endDate={endDate} |
| 141 | /> |
| 142 | </div> |
| 143 | <div className="presto-dashboard__item"> |
| 144 | <VideoAverageWatchTimeByUser |
| 145 | userId={route?.params?.id} |
| 146 | startDate={startDate} |
| 147 | endDate={endDate} |
| 148 | /> |
| 149 | </div> |
| 150 | <div className="presto-dashboard__item"> |
| 151 | <VideoTotalWatchTimeByUser |
| 152 | userId={route?.params?.id} |
| 153 | startDate={startDate} |
| 154 | endDate={endDate} |
| 155 | /> |
| 156 | </div> |
| 157 | </div> |
| 158 | <div className="presto-dashboard__row"> |
| 159 | <div className="presto-dashboard__item is-large"> |
| 160 | <TopVideos |
| 161 | startDate={startDate} |
| 162 | endDate={endDate} |
| 163 | userId={route?.params?.id} |
| 164 | /> |
| 165 | </div> |
| 166 | </div> |
| 167 | </div> |
| 168 | </div> |
| 169 | ); |
| 170 | }; |
| 171 | |
| 172 | export default User; |
| 173 |