From 5880abd7ae2c837ef03fac2c0aa7261ffc4bbca1 Mon Sep 17 00:00:00 2001 From: Marquis Kurt Date: Wed, 27 Mar 2019 19:24:52 -0400 Subject: [PATCH] Get some data from Mastodon --- src/App.tsx | 3 +- src/components/AppLayout/AppLayout.styles.tsx | 2 +- src/components/AppLayout/AppLayout.tsx | 12 +++++--- src/pages/About.tsx | 30 ++++++++++++++++--- src/pages/Settings.tsx | 4 +-- src/types/Account.tsx | 6 ++++ src/types/Instance.tsx | 15 ++++++++++ src/utilities/accounts.tsx | 16 ++++++++++ src/utilities/instances.tsx | 9 ++++++ src/utilities/settings.tsx | 1 - 10 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 src/types/Instance.tsx create mode 100644 src/utilities/accounts.tsx create mode 100644 src/utilities/instances.tsx diff --git a/src/App.tsx b/src/App.tsx index cd804aa..50c4282 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,6 @@ import React, { Component } from 'react'; import {MuiThemeProvider, CssBaseline, withStyles, Typography } from '@material-ui/core'; import { setHyperspaceTheme, darkMode } from './utilities/themes'; -import { defaultTheme } from './types/HyperspaceTheme'; import AppLayout from './components/AppLayout'; import {styles} from './App.styles'; import {Route} from 'react-router-dom'; @@ -10,7 +9,7 @@ import Settings from './pages/Settings'; import { getUserDefaultBool, getUserDefaultTheme } from './utilities/settings'; let theme = setHyperspaceTheme(getUserDefaultTheme()); -console.log(theme); + class App extends Component { constructor(props: any) { diff --git a/src/components/AppLayout/AppLayout.styles.tsx b/src/components/AppLayout/AppLayout.styles.tsx index 90b0e70..9714671 100644 --- a/src/components/AppLayout/AppLayout.styles.tsx +++ b/src/components/AppLayout/AppLayout.styles.tsx @@ -93,7 +93,7 @@ export const styles = (theme: Theme) => createStyles({ }, }, appBarAcctMenuIcon: { - backgroundColor: theme.palette.secondary.main + backgroundColor: theme.palette.primary.dark }, drawer: { [theme.breakpoints.up('sm')]: { diff --git a/src/components/AppLayout/AppLayout.tsx b/src/components/AppLayout/AppLayout.tsx index 0ade544..adf6a1f 100644 --- a/src/components/AppLayout/AppLayout.tsx +++ b/src/components/AppLayout/AppLayout.tsx @@ -15,9 +15,12 @@ import ExitToAppIcon from '@material-ui/icons/ExitToApp'; import {styles} from './AppLayout.styles'; import { Link } from 'react-router-dom'; import { ListItemProps } from '@material-ui/core/ListItem'; +import { getCurrentUserData } from '../../utilities/accounts'; +import { UAccount } from '../../types/Account'; interface IAppLayoutState { drawerOpenOnMobile: boolean; + currentUser: UAccount; } interface ILinkableListItemProps extends ListItemProps { @@ -34,7 +37,8 @@ export class AppLayout extends Component { super(props); this.state = { - drawerOpenOnMobile: false + drawerOpenOnMobile: false, + currentUser: getCurrentUserData() } this.toggleDrawerOnMobile = this.toggleDrawerOnMobile.bind(this); @@ -71,9 +75,9 @@ export class AppLayout extends Component {
- + - + @@ -166,7 +170,7 @@ export class AppLayout extends Component { - +
diff --git a/src/pages/About.tsx b/src/pages/About.tsx index 5be5c9e..4c01005 100644 --- a/src/pages/About.tsx +++ b/src/pages/About.tsx @@ -18,8 +18,27 @@ import DomainIcon from '@material-ui/icons/Domain'; import ChatIcon from '@material-ui/icons/Chat'; import PersonIcon from '@material-ui/icons/Person'; import {styles} from './PageLayout.styles'; +import {getCurrentInstanceData} from '../utilities/instances'; +import {Instance} from '../types/Instance'; -class AboutPage extends Component { +interface IAboutPageState { + instance: Instance; +} + +class AboutPage extends Component { + + constructor(props: any) { + super(props); + + let instance = getCurrentInstanceData(); + instance.then((resp: any) => { + let data: Instance = resp.data; + console.log("From response: " + data); + this.setState({ + instance: data + }); + }) + } render() { const { classes } = this.props; @@ -34,7 +53,7 @@ class AboutPage extends Component { - + @@ -43,9 +62,12 @@ class AboutPage extends Component { - + - + diff --git a/src/pages/Settings.tsx b/src/pages/Settings.tsx index 16aa5c3..09cd594 100644 --- a/src/pages/Settings.tsx +++ b/src/pages/Settings.tsx @@ -106,10 +106,10 @@ class SettingsPage extends Component { - - diff --git a/src/types/Account.tsx b/src/types/Account.tsx index c1c2d88..5f31bc2 100644 --- a/src/types/Account.tsx +++ b/src/types/Account.tsx @@ -24,4 +24,10 @@ export type Account = { moved: Account | null; fields: [Field]; bot: boolean | null; +} + +export type UAccount = { + acct: string; + display_name: string; + avatar_static: string; } \ No newline at end of file diff --git a/src/types/Instance.tsx b/src/types/Instance.tsx new file mode 100644 index 0000000..a956b93 --- /dev/null +++ b/src/types/Instance.tsx @@ -0,0 +1,15 @@ +import { Field } from "./Field"; +import { Account } from "./Account"; + +export type Instance = { + uri: string; + title: string; + description: string; + email: string; + version: string; + thumbnail: string | null; + urls: Field; + stats: Field; + languages: [string]; + contact_account: Account; +} \ No newline at end of file diff --git a/src/utilities/accounts.tsx b/src/utilities/accounts.tsx new file mode 100644 index 0000000..a009dd0 --- /dev/null +++ b/src/utilities/accounts.tsx @@ -0,0 +1,16 @@ +import Mastodon from 'megalodon'; +import { Account, UAccount } from '../types/Account'; + +export function getCurrentUserData() { + let currentData: Account = JSON.parse(localStorage.getItem('account') as string); + if (currentData === null) { + let client = new Mastodon(localStorage.getItem('access_token') as string, localStorage.getItem('baseurl') as string + "/api/v1/"); + client.get('/account/verify_credentials').then((resp: any) => { let acct: Account = resp.data; currentData = acct; return acct; }); + } + let account: UAccount = { + acct: "@" + currentData.acct, + display_name: currentData.display_name || "@" + currentData.acct, + avatar_static: currentData.avatar_static + } + return account; +} \ No newline at end of file diff --git a/src/utilities/instances.tsx b/src/utilities/instances.tsx new file mode 100644 index 0000000..c4e48f5 --- /dev/null +++ b/src/utilities/instances.tsx @@ -0,0 +1,9 @@ +import Mastodon from 'megalodon'; +import { Instance } from '../types/Instance'; + +export function getCurrentInstanceData() { + let instance; + let client = new Mastodon(localStorage.getItem('access_token') as string, localStorage.getItem('baseurl') as string + "/api/v1/"); + instance = client.get('/instance'); + return instance; +} \ No newline at end of file diff --git a/src/utilities/settings.tsx b/src/utilities/settings.tsx index 3acc90c..44211f0 100644 --- a/src/utilities/settings.tsx +++ b/src/utilities/settings.tsx @@ -37,7 +37,6 @@ export function setUserDefaultBool(key: string, value: boolean) { * Gets the user's default theme or the default theme */ export function getUserDefaultTheme() { - console.log(localStorage.getItem('theme')); let returnTheme = defaultTheme; themes.forEach((theme) => { if(theme.key === localStorage.getItem('theme')) {