Skip to content

Commit

Permalink
feat(graphs): add graphs and make app responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Nov 14, 2024
1 parent 73cc44f commit 3ac1de7
Show file tree
Hide file tree
Showing 27 changed files with 689 additions and 419 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"@tanstack/react-query": "^4.36.1",
"@tanstack/react-query-devtools": "^4.29.6",
"@types/react-select": "^5.0.1",
"highcharts": "^11.4.1",
"highcharts": "^11.4.8",
"highcharts-more": "^0.1.7",
"highcharts-react-official": "^3.2.1",
"prop-types": "^15.8.1",
"react": "^18.2.0",
Expand Down
2 changes: 2 additions & 0 deletions client/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from "react";
import ContactAndContributionPage from "./src/pages/contact-contributionbyobject-page";
import LocalBSO from "./src/pages/bso/index";
import LastMailSent from "./src/pages/last-mails-sent";
import GetStats from "./src/pages/stats";

export default function Router() {
return (
Expand Down Expand Up @@ -54,6 +55,7 @@ export default function Router() {
path="/scanr-namechange"
element={<ContactAndContributionPage />}
/>
<Route path="/statistiques" element={<GetStats />} />
</Route>
</Routes>
);
Expand Down
38 changes: 35 additions & 3 deletions client/src/api/utils/buildURL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ export const buildURL = (
status: string,
query: string,
page: number,
searchInMessages: boolean = false,
fromApplication?: string
searchInMessages: boolean = true,
fromApplication?: string,
max_results: string = "20"
): string => {
const isDevelopment = import.meta.env.VITE_HEADER_TAG === "Development";
const url = import.meta.env.VITE_BASE_API_URL;
Expand Down Expand Up @@ -43,6 +44,7 @@ export const buildURL = (
}
}
}

const fromAppQuery = fromApplication
? `&fromApplication=${fromApplication.toLocaleLowerCase()}`
: "";
Expand All @@ -53,5 +55,35 @@ export const buildURL = (
const whereQuery =
Object.keys(where).length > 0 ? `&where=${JSON.stringify(where)}` : "";

return `${baseApiUrl}/${baseUrl}?${sorted}&page=${page}&max_results=20${whereQuery}${fromAppQuery}`;
return `${baseApiUrl}/${baseUrl}?${sorted}&page=${page}&max_results=${max_results}${whereQuery}${fromAppQuery}`;
};
export const buildStatsURL = (
filter: string,
sort: string = "ASC",
page: number = 1,
maxResults: string = "3000"
): string => {
const isDevelopment = import.meta.env.VITE_HEADER_TAG === "Development";
const url = import.meta.env.VITE_BASE_API_URL;
const baseApiUrl = isDevelopment ? "http://localhost:3000/api" : `${url}/api`;

let baseUrl = "contacts";

switch (filter) {
case "object":
baseUrl = "contribute";
break;
case "removeuser":
baseUrl = "remove-user";
break;
case "namechange":
baseUrl = "update-user-data";
break;
default:
baseUrl = "contacts";
}

const sorted = sort === "ASC" ? "sort=created_at" : "sort=-created_at";

return `${baseApiUrl}/${baseUrl}?${sorted}&page=${page}&max_results=${maxResults}`;
};
8 changes: 4 additions & 4 deletions client/src/components/edit-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const EditModal: React.FC<EditModalProps> = ({
</select>
</Col>
<Row gutters>
<Col md="6">
<Col md="6" xs="12">
<TextArea
label="Ajouter des tags"
hint="Séparez les tags par des virgules"
Expand All @@ -198,7 +198,7 @@ const EditModal: React.FC<EditModalProps> = ({
</Button>
)}
</Col>
<Col md="6">
<Col md="6" xs="12">
{inputs.tags.map((tag, index) => (
<span key={index}>
<DismissibleTag
Expand All @@ -221,15 +221,15 @@ const EditModal: React.FC<EditModalProps> = ({
Sélectionner des tags
</Button>
<Row gutters>
<Col md="6">
<Col md="6" xs="12">
<TextArea
label="Ajouter des extra"
value={inputs.extra}
onChange={(e) => handleInputChange("extra", e.target.value)}
hint="Exemple : clé: valeur"
/>
</Col>
<Col md="6">
<Col md="6" xs="12">
<TextArea
label="Commentaire"
value={inputs.comment}
Expand Down
114 changes: 57 additions & 57 deletions client/src/components/graphs/by-missing-productions.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import { Col } from "@dataesr/dsfr-plus";
import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
import { ClipLoader } from "react-spinners";
// import { Col } from "@dataesr/dsfr-plus";
// import Highcharts from "highcharts";
// import HighchartsReact from "highcharts-react-official";
// import { ClipLoader } from "react-spinners";

const ContributionsGraphByStatus = ({ contributions, isLoading, isError }) => {
if (isLoading) {
return (
<Col className="comment">
<ClipLoader color="#123abc" size={50} />
</Col>
);
}
// const ContributionsGraphByStatus = ({ contributions, isLoading, isError }) => {
// if (isLoading) {
// return (
// <Col className="comment">
// <ClipLoader color="#123abc" size={50} />
// </Col>
// );
// }

if (isError) {
return <div>Une erreur s'est produite</div>;
}
// if (isError) {
// return <div>Une erreur s'est produite</div>;
// }

if (!Array.isArray(contributions)) {
return <div>Les données ne sont pas disponibles</div>;
}
// if (!Array.isArray(contributions)) {
// return <div>Les données ne sont pas disponibles</div>;
// }

const newCount = contributions.filter(
(contribution) => contribution.status === "new"
).length;
const ongoingCount = contributions.filter(
(contribution) => contribution.status === "ongoing"
).length;
const treatedCount = contributions.filter(
(contribution) => contribution.status === "treated"
).length;
// const newCount = contributions.filter(
// (contribution) => contribution.status === "new"
// ).length;
// const ongoingCount = contributions.filter(
// (contribution) => contribution.status === "ongoing"
// ).length;
// const treatedCount = contributions.filter(
// (contribution) => contribution.status === "treated"
// ).length;

const chartData = [
{ name: "New", y: newCount },
{ name: "Ongoing", y: ongoingCount },
{ name: "Treated", y: treatedCount },
];
// const chartData = [
// { name: "New", y: newCount },
// { name: "Ongoing", y: ongoingCount },
// { name: "Treated", y: treatedCount },
// ];

const options = {
chart: {
type: "column",
},
title: {
text: "Nombre de demande d'affiliation par statut",
},
xAxis: {
type: "category",
},
yAxis: {
title: {
text: "Nombre de contributions",
},
},
series: [
{
name: "Statut",
data: chartData,
},
],
};
// const options = {
// chart: {
// type: "pie",
// },
// title: {
// text: "Nombre de demande d'affiliation par statut",
// },
// xAxis: {
// type: "category",
// },
// yAxis: {
// title: {
// text: "Nombre de contributions",
// },
// },
// series: [
// {
// name: "Statut",
// data: chartData,
// },
// ],
// };

return <HighchartsReact highcharts={Highcharts} options={options} />;
};
// return <HighchartsReact highcharts={Highcharts} options={options} />;
// };

export default ContributionsGraphByStatus;
// export default ContributionsGraphByStatus;
116 changes: 70 additions & 46 deletions client/src/components/graphs/by-status.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
import { Col, SegmentedControl, SegmentedElement } from "@dataesr/dsfr-plus";
import { ContributionDataType } from "../../types";
import { ClipLoader } from "react-spinners";
import { ContributionDataType } from "../../types";
import "./styles.scss";

const ContributionsGraphByStatus = ({
contributions,
isLoading,
isError,
filter,
setFilter,
}) => {
const ContributionsGraphByStatus = ({ contributions, isLoading, isError }) => {
if (isLoading) {
return (
<Col className="comment">
<div className="loading-container">
<ClipLoader color="#123abc" size={50} />
</Col>
</div>
);
}

Expand All @@ -30,65 +24,95 @@ const ContributionsGraphByStatus = ({
const contributionsByStatus = contributions.reduce(
(acc: Record<string, number>, contribution: ContributionDataType) => {
const { status } = contribution;
if (!acc[status]) {
acc[status] = 1;
} else {
acc[status] += 1;
if (status) {
const normalizedStatus = status.toLowerCase();
if (!acc[normalizedStatus]) {
acc[normalizedStatus] = 1;
} else {
acc[normalizedStatus] += 1;
}
}

return acc;
},
{}
);
const chartData = Object.entries(contributionsByStatus).map(([name, y]) => ({
name,
y,
}));

const sortedStatuses = Object.entries(contributionsByStatus).sort(
([, a]: [string, number], [, b]: [string, number]) => b - a
);

const chartData = sortedStatuses.map(([name, y]) => {
let color;
switch (name) {
case "new":
color = "#a94645";
break;
case "treated":
color = "#c9fcac";
break;
case "ongoing":
color = "#D1B781";
break;
default:
color = "#808080";
}
return {
name,
y,
color,
};
});
const options = {
chart: {
type: "column",
backgroundColor: "#ffffff",
borderWidth: 0,
plotBackgroundColor: "#f5f5f5",
},
title: {
text: "Les status des contributions",
text: "Status des Contributions",
style: {
fontSize: "24px",
fontWeight: "600",
color: "#333",
},
},
xAxis: {
type: "category",
tooltip: {
pointFormat: "{point.name}: <b>{point.y}</b> contributions",
borderRadius: 5,
backgroundColor: "#f8f8f8",
style: {
color: "#333",
fontSize: "14px",
},
},
yAxis: {
title: {
text: "Nombre de contributions",
plotOptions: {
pie: {
dataLabels: {
enabled: true,
format: "{point.y}",
style: {
color: "#333",
fontSize: "14px",
fontWeight: "bold",
},
},
},
},
series: [
{
name: "Statut",
data: chartData,
},
],
legend: {
enabled: false,
},
};

return (
<>
<SegmentedControl name={""} className="fr-mb-5w">
<SegmentedElement
onClick={() => setFilter("object")}
name="Par Objet"
label={"Par Objet"}
value={""}
icon="fr-fi-eye-line"
checked={filter === "object"}
/>
<SegmentedElement
onClick={() => setFilter("contact")}
name="Par Objet"
label={"Via formulaire contact"}
value={""}
checked={filter === "contact"}
/>
</SegmentedControl>
<div className="graph-container">
<HighchartsReact highcharts={Highcharts} options={options} />
</>
</div>
);
};

Expand Down
Loading

0 comments on commit 3ac1de7

Please sign in to comment.