Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix order by date in project metrics #81

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React, { useEffect, useState } from "react";
import { ProjectTotalsGraph } from "../projectTotalsGraph/projectTotalsGraph";
import { IMetric } from "~~/services/mongodb/models/metric";
import { IProjectScore } from "~~/services/mongodb/models/projectScore";
import { MetricService } from "~~/services/onchainImpactDashboardApi/metricsService";
import { ProjectScoreService } from "~~/services/onchainImpactDashboardApi/projectScoreService";

Expand All @@ -13,13 +14,36 @@ export const ProjectTotalsComponent = ({ id }: { id: string }) => {
const [startDate, setStartDate] = useState("");
const [endDate, setEndDate] = useState("");
const { getProjectScoreById } = ProjectScoreService();
const [totalsRecord, setTotalsRecord] = useState<any[]>([]);
const [totalsRecord, setTotalsRecord] = useState<IProjectScore[]>([]);
const [baseData, setBaseData] = useState<IProjectScore[]>([]);

const { getMetrics: getAllMetrics } = MetricService();
const [selectedMetric, setSelectedMetric] = useState<number>(0);
const getTotals = async (currentFil?: string, sDate?: string, eDate?: string) => {
if (baseData.length > 0) {
// Defaults to 90 days
let start = new Date();
start.setDate(start.getDate() - parseInt(currentFil || "7"));
let end = new Date();
if (!!sDate) {
start = new Date(sDate);
start.setDate(start.getDate() - 1);
}

const getTotals = async () => {
const data = await getProjectScoreById(id, filter);
setTotalsRecord(data as any);
if (!!eDate) {
end = new Date(eDate);
end.setDate(end.getDate() + 1);
}
setTotalsRecord(
baseData.filter(
it => new Date(it.date).getTime() > start.getTime() && new Date(it.date).getTime() < end.getTime(),
),
);
return;
}
const data = await getProjectScoreById(id);
setBaseData(data);
setTotalsRecord(data);
};
const getMetrics = async () => {
const data = await getAllMetrics();
Expand All @@ -34,10 +58,26 @@ export const ProjectTotalsComponent = ({ id }: { id: string }) => {

const onFilter = (value: string) => {
setFilter(value);
setStartDate("");
setEndDate("");
if (value == "range") {
return;
}
getTotals();
getTotals(value);
};
const onStartDateChange = (value: string) => {
setStartDate(value);
if (!!endDate && new Date(value).getTime() >= new Date(endDate).getTime()) {
return;
}
getTotals(filter, value, endDate);
};
const onEndDateChange = (value: string) => {
setEndDate(value);
if (!!startDate && new Date(startDate).getTime() >= new Date(value).getTime()) {
return;
}
getTotals(filter, startDate, value);
};
return (
<main className="flex flex-col lg:flex-row w-full mt-14">
Expand All @@ -50,8 +90,8 @@ export const ProjectTotalsComponent = ({ id }: { id: string }) => {
onFilter={val => onFilter(val)}
endDate={endDate}
startDate={startDate}
updateStartDate={val => setStartDate(val)}
updateEndDate={val => setEndDate(val)}
updateStartDate={val => onStartDateChange(val)}
updateEndDate={val => onEndDateChange(val)}
/>
</main>
);
Expand Down
3 changes: 1 addition & 2 deletions packages/nextjs/pages/api/projectScore/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (!entries || entries.length === 0) {
return res.status(404).json({ message: "No scores located with that project id" });
}

try {
res.status(200).json({ data: entries });
res.status(200).json({ data: entries.sort((a, b) => a.date.getTime() - b.date.getTime()) });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal Server Error" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { IProjectScore } from "../mongodb/models/projectScore";

export const ProjectScoreService = () => {
const baseURL = `${process.env.NEXT_PUBLIC_API_URL}/projectScore`;

const getProjectScoreById = async (id: string, filter?: string) => {
filter; // TODO: Implement filter
const getProjectScoreById = async (id: string) => {
const response = await fetch(`${baseURL}/${id}`);
const { data }: { data: IProjectScore } = await response.json();
const { data }: { data: IProjectScore[] } = await response.json();
return data;
};

Expand Down
Loading