Skip to content

Commit

Permalink
Merge pull request #1992 from brave-intl/admin-charts
Browse files Browse the repository at this point in the history
Admin charts
  • Loading branch information
yachtcaptain23 authored Jul 1, 2019
2 parents 0c8b9da + 4713068 commit e939987
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 642 deletions.
8 changes: 8 additions & 0 deletions app/controllers/admin/promo_registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Admin::PromoRegistrationsController < AdminController
def for_referral_code
publisher = Publisher.find(params[:publisher_id])
promo_registration = publisher.promo_registrations.find_by(referral_code: params[:referral_code])
render :unauthorized and return if promo_registration.nil?
render json: promo_registration.stats_by_date.to_json
end
end
5 changes: 5 additions & 0 deletions app/controllers/admin/publishers/referrals_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Admin::Publishers::ReferralsController < Admin::PublishersController
def show
@navigation_view = Views::Admin::NavigationView.new(@publisher).as_json.merge({ navbarSelection: "Referrals" }).to_json
end
end
44 changes: 0 additions & 44 deletions app/controllers/admin/referrals_controller.rb

This file was deleted.

4 changes: 3 additions & 1 deletion app/controllers/publishers/promo_registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Publishers::PromoRegistrationsController < PublishersController
def for_referral_code
promo_registration = current_publisher.promo_registrations.find_by(referral_code: params[:referral_code])
promo_registration = current_publisher.admin? ?
PromoRegistration.find_by(referral_code: params[:referral_code]) :
current_publisher.promo_registrations.find_by(referral_code: params[:referral_code])
render :unauthorized and return if promo_registration.nil?
render json: promo_registration.stats_by_date.to_json
end
Expand Down
21 changes: 5 additions & 16 deletions app/javascript/packs/chart/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import moment from "moment";
import Chart from "chart.js";

class ReactChart extends React.Component {
export default class ReactChart extends React.Component {
constructor(props) {
super(props);
this.chartRef = React.createRef();
Expand Down Expand Up @@ -40,10 +40,7 @@ class ReactChart extends React.Component {
});
} else {
this.chart.data = this.getData(this.props.data);
this.chart.options = this.getOptions(
this.props.title,
this.getSuggestedMax(this.props.data)
);
this.chart.options = this.getOptions(this.props.title, this.getSuggestedMax(this.props.data));
this.chart.update();
}
}
Expand Down Expand Up @@ -128,10 +125,8 @@ class ReactChart extends React.Component {
var currentMax = 0;
Object.keys(data).forEach(function(key) {
var value = data[key];
currentMax =
value.retrievals > currentMax ? value.retrievals : currentMax;
currentMax =
value.first_runs > currentMax ? value.first_runs : currentMax;
currentMax = value.retrievals > currentMax ? value.retrievals : currentMax;
currentMax = value.first_runs > currentMax ? value.first_runs : currentMax;
currentMax = value.finalized > currentMax ? value.finalized : currentMax;
});
return Math.ceil((currentMax / 95) * 100);
Expand All @@ -154,12 +149,6 @@ class ReactChart extends React.Component {
}

render() {
return (
<div>
{this.props.data && <canvas id="chart-canvas" ref={this.chartRef} />}
</div>
);
return <div>{this.props.data && <canvas id="chart-canvas" ref={this.chartRef} />}</div>;
}
}

export default ReactChart;
19 changes: 10 additions & 9 deletions app/javascript/packs/referral_charts.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import "babel-polyfill";
import styled from "brave-ui/theme";
import Select from "brave-ui/components/formControls/select";
import ControlWrapper from "brave-ui/components/formControls/controlWrapper";
import { PrimaryButton } from "../publishers/ReferralChartsStyle";
// import "../publishers/dashboard_chart";
import routes from "../views/routes";
import Chart from "./chart/Chart";
import ReactChart from "./chart/Chart";
import { ThemeProvider } from "brave-ui/theme";
import Theme from "brave-ui/theme/brave-default";

Expand All @@ -27,10 +26,11 @@ export default class ReferralCharts extends React.Component {

async viewReferralCodeStats() {
const node = this.selectMenuRef.current;
var url = routes.publishers.promo_registrations.show.path.replace(
"{id}",
this.props.publisherId
);
var url =
this.props.scope === "admin"
? routes.admin.promo_registrations.show.path.replace("{publisher_id}", this.props.publisherId)
: routes.publishers.promo_registrations.show.path.replace("{id}", this.props.publisherId);

url = url.replace("{referral_code}", node.state.value);
const result = await fetch(url, {
headers: {
Expand Down Expand Up @@ -71,21 +71,22 @@ export default class ReferralCharts extends React.Component {
View stats
</PrimaryButton>
</div>
<Chart data={this.state.data} title={this.state.title} />
<ReactChart data={this.state.data} title={this.state.title} />
</React.Fragment>
);
}
}

export function renderReferralCharts() {
export function renderReferralCharts(scope) {
const { value } = document.getElementById("referrals-hidden-tags");
const publisherId = document.getElementById("publisher_id").value;
if (value === undefined) {
return;
}
let referralCodes = JSON.parse(value);
let props = {
referralCodes: referralCodes
referralCodes: referralCodes,
scope: scope
};
ReactDOM.render(
<ReferralCharts {...props} publisherId={publisherId} />,
Expand Down
11 changes: 2 additions & 9 deletions app/javascript/packs/views/admin/referrals/Referrals.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import Referrals from "../../../../views/admin/referrals/Referrals";
import { renderReferralCharts } from "../../../../packs/referral_charts";

document.addEventListener("DOMContentLoaded", () => {
const node = document.getElementById("referrals_data");
const data = JSON.parse(node.getAttribute("data"));
ReactDOM.render(
<Referrals data={data} />,
document
.getElementById("main-content")
.appendChild(document.createElement("div"))
);
renderReferralCharts("admin");
});
7 changes: 1 addition & 6 deletions app/javascript/packs/views/referrals/Referrals.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@ import * as ReactDOM from "react-dom";
import Referrals from "../../../views/referrals/Referrals";

document.addEventListener("DOMContentLoaded", () => {
ReactDOM.render(
<Referrals />,
document.body.appendChild(
document.getElementsByClassName("main-content")[0]
)
);
ReactDOM.render(<Referrals />, document.body.appendChild(document.getElementsByClassName("main-content")[0]));
});
91 changes: 0 additions & 91 deletions app/javascript/publishers/dashboard_chart.js

This file was deleted.

Loading

0 comments on commit e939987

Please sign in to comment.