-  Display stats on homepage
This commit is contained in:
Daniel Mason 2021-03-30 16:45:31 +13:00
parent 038823055a
commit 7ae9a0cd66
Signed by: idanoo
GPG Key ID: 387387CDBC02F132
6 changed files with 55 additions and 43 deletions

View File

@ -3,7 +3,7 @@ stages:
- bundle - bundle
variables: variables:
VERSION: 0.0.3 VERSION: 0.0.4
build-go: build-go:
image: golang:1.16.2 image: golang:1.16.2

View File

@ -10,7 +10,7 @@ import (
type StatsRequest struct { type StatsRequest struct {
Users int `json:"users"` Users int `json:"users"`
Scrobbles int `json:"scrobbles"` Scrobbles int `json:"scrobbles"`
Tracks int `json:"songs"` Tracks int `json:"tracks"`
Artists int `json:"artists"` Artists int `json:"artists"`
LastUpdated time.Time `json:"last_updated"` LastUpdated time.Time `json:"last_updated"`
} }

View File

@ -1,28 +1,9 @@
import { toast } from 'react-toastify';
import ApiService from "../Services/api.service"; import ApiService from "../Services/api.service";
export const getStats = () => () => { export const getStats = () => {
return ApiService.getStats().then( return ApiService.getStats().then(
(data) => { (data) => {
console.log(data); return data.data;
if (data.error) {
toast.error(data.error)
return Promise.reject();
}
return Promise.resolve();
},
(error) => {
const message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
console.log(message);
toast.error(message);
return Promise.reject();
} }
); );
}; };

View File

@ -0,0 +1,14 @@
.homeBanner {
margin-top: 30px;
width: 100%;
}
.homeBannerItem {
float: left;
text-align: center;
width: 25%;
}
.homeBannerItemCount {
font-size: 22pt;
}

View File

@ -2,6 +2,7 @@ import React from 'react';
import '../App.css'; import '../App.css';
import './HomeBanner.css'; import './HomeBanner.css';
import { getStats } from '../Actions/api'; import { getStats } from '../Actions/api';
import ClipLoader from 'react-spinners/ClipLoader'
class HomeBanner extends React.Component { class HomeBanner extends React.Component {
constructor(props) { constructor(props) {
@ -17,25 +18,45 @@ class HomeBanner extends React.Component {
componentDidMount() { componentDidMount() {
getStats() getStats()
// .then((data) => { .then((data) => {
// this.setState({ this.setState({
// loading: false, isLoading: false,
// userCount: data.users, userCount: data.users,
// scrobbleCount: data.scrobbles, scrobbleCount: data.scrobbles,
// trackCount: data.tracks, trackCount: data.tracks,
// artistCount: data.artists, artistCount: data.artists,
// }); });
// }) })
// .catch(() => { .catch(() => {
// this.setState({ this.setState({
// loading: false isLoading: false
// }); });
// }); });
} }
render() { render() {
return ( return (
<div className="container"> <div className="homeBanner">
<div className="homeBannerItem">
{this.state.isLoading
? <ClipLoader color="#6AD7E5" size="36" />
: <span className="homeBannerItemCount">{this.state.scrobbleCount}</span>}<br/>Scrobbles
</div>
<div className="homeBannerItem">
{this.state.isLoading
? <ClipLoader color="#6AD7E5" size="36" />
: <span className="homeBannerItemCount">{this.state.userCount}</span>}<br/>Users
</div>
<div className="homeBannerItem">
{this.state.isLoading
? <ClipLoader color="#6AD7E5" size="36" />
: <span className="homeBannerItemCount">{this.state.trackCount}</span>}<br/>Tracks
</div>
<div className="homeBannerItem">
{this.state.isLoading
? <ClipLoader color="#6AD7E5" size="36" />
: <span className="homeBannerItemCount">{this.state.artistCount}</span>}<br/>Artists
</div>
</div> </div>
); );
} }

View File

@ -2,11 +2,7 @@ import axios from "axios";
class ApiService { class ApiService {
async getStats() { async getStats() {
return axios.get(process.env.REACT_APP_API_URL + "stats") return axios.get(process.env.REACT_APP_API_URL + "stats");
.then((response) => {
return response.data;
}
);
} }
} }