components
3 years ago
pages
3 years ago
scss
5 years ago
store
5 years ago
app.js
3 years ago
entities.js
3 years ago
index.js
3 years ago
routes.js
3 years ago
settings.js
4 years ago
settings.scss
5 years ago
util.js
5 years ago
app.js
141 lines
| 1 | import { __ } from "@wordpress/i18n"; |
| 2 | import { |
| 3 | Card, |
| 4 | Flex, |
| 5 | FlexBlock, |
| 6 | FlexItem, |
| 7 | Spinner, |
| 8 | } from "@wordpress/components"; |
| 9 | import { store as noticesStore } from "@wordpress/notices"; |
| 10 | import { useDispatch, useSelect } from "@wordpress/data"; |
| 11 | import { store as coreStore } from "@wordpress/core-data"; |
| 12 | import { useState, useEffect } from "@wordpress/element"; |
| 13 | |
| 14 | import { Router, Link, Route } from "@/router"; |
| 15 | import { routes } from "./routes"; |
| 16 | |
| 17 | import SaveButton from "./components/SaveButton"; |
| 18 | import Notices from "./components/Notices"; |
| 19 | import useSave from "../../hooks/useSave"; |
| 20 | import General from "./pages/General"; |
| 21 | import Performance from "./pages/Performance"; |
| 22 | import Integrations from "./pages/Integrations"; |
| 23 | |
| 24 | function App() { |
| 25 | const { createSuccessNotice, createErrorNotice } = useDispatch(noticesStore); |
| 26 | const [loaded, setLoaded] = useState(false); |
| 27 | |
| 28 | // scroll top on history change |
| 29 | window.onhashchange = () => { |
| 30 | window.scrollTo(0, 0); |
| 31 | }; |
| 32 | |
| 33 | const { save } = useSave(); |
| 34 | |
| 35 | /** |
| 36 | * Form is submitted. |
| 37 | */ |
| 38 | const onSubmit = async () => { |
| 39 | try { |
| 40 | await save(); |
| 41 | createSuccessNotice(__("Settings Updated", "presto-player"), { |
| 42 | type: "snackbar", |
| 43 | }); |
| 44 | } catch (e) { |
| 45 | console.error(e); |
| 46 | createErrorNotice( |
| 47 | e?.message || __("Something went wrong", "presto-player"), |
| 48 | { type: "snackbar" } |
| 49 | ); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | const loading = useSelect((select) => { |
| 54 | const queryArgs = ["root", "site"]; |
| 55 | select(coreStore).getEntityRecords(...queryArgs); |
| 56 | return !select(coreStore)?.hasFinishedResolution?.( |
| 57 | "getEntityRecords", |
| 58 | queryArgs |
| 59 | ); |
| 60 | }); |
| 61 | |
| 62 | useEffect(() => { |
| 63 | if (!loading) { |
| 64 | setLoaded(true); |
| 65 | } |
| 66 | }, [loading]); |
| 67 | |
| 68 | if (!loaded) { |
| 69 | return ( |
| 70 | <div className="presto-settings__loading"> |
| 71 | <Spinner /> |
| 72 | </div> |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | return ( |
| 77 | <div className="presto-settings"> |
| 78 | <Router routes={routes} defaultRoute={routes?.general?.path}> |
| 79 | <Card className="presto-settings__navigation"> |
| 80 | <Flex> |
| 81 | <FlexBlock> |
| 82 | <div |
| 83 | role="tablist" |
| 84 | aria-orientation="horizontal" |
| 85 | className="components-tab-panel__tabs" |
| 86 | > |
| 87 | <Link |
| 88 | to={routes?.general?.path} |
| 89 | type="button" |
| 90 | role="tab" |
| 91 | activeClassName="is-active" |
| 92 | className="components-button components-tab-panel__tabs-item presto-player__nav-general" |
| 93 | > |
| 94 | {__("General", "presto-player")} |
| 95 | </Link> |
| 96 | <Link |
| 97 | to={routes?.integrations?.path} |
| 98 | type="button" |
| 99 | role="tab" |
| 100 | activeClassName="is-active" |
| 101 | className="components-button components-tab-panel__tabs-item presto-player__nav-integrations" |
| 102 | > |
| 103 | {__("Integrations", "presto-player")} |
| 104 | </Link> |
| 105 | <Link |
| 106 | to={routes?.performance?.path} |
| 107 | type="button" |
| 108 | role="tab" |
| 109 | activeClassName="is-active" |
| 110 | className="components-button components-tab-panel__tabs-item presto-player__nav-performance" |
| 111 | > |
| 112 | {__("Performance", "presto-player")} |
| 113 | </Link> |
| 114 | </div> |
| 115 | </FlexBlock> |
| 116 | <FlexItem> |
| 117 | <SaveButton onSave={onSubmit} style={{ marginRight: "8px" }}> |
| 118 | {__("Update Settings", "presto-player")} |
| 119 | </SaveButton> |
| 120 | </FlexItem> |
| 121 | </Flex> |
| 122 | </Card> |
| 123 | |
| 124 | <Route path={routes?.general?.path}> |
| 125 | <General /> |
| 126 | </Route> |
| 127 | <Route path={routes?.integrations?.path}> |
| 128 | <Integrations /> |
| 129 | </Route> |
| 130 | <Route path={routes?.performance?.path}> |
| 131 | <Performance /> |
| 132 | </Route> |
| 133 | </Router> |
| 134 | |
| 135 | <Notices className="presto-settings-page-notices" /> |
| 136 | </div> |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | export default App; |
| 141 |