import { useQuery } from "@apollo/client";
import { Line } from "@nivo/line";
import React from "react";
import { useParams } from "react-router-dom";
import { User, UserDetailDocument, UserProfile } from "../../types";
import { formatMonth, formatTime } from "../../utils/time";
import styles from "./UserDetail.module.css";
export const UserDetail = () => {
const { userId } = useParams();
const { data, loading } = useQuery(UserDetailDocument, { variables: { userId: userId || "" }, skip: !userId });
if (loading) return
Loading...
;
const user = data?.user as User;
const profile = user?.profile as UserProfile;
// FIXME: types
const series: any[] = [];
(profile?.productOrderStats || [])?.forEach(({ productName: id, data }) => {
const lineData: any[] = [];
data.forEach((point, index) => {
lineData.push({ x: point.x, y: point.y + (lineData?.[index - 1]?.y || 0) });
});
series.push({ id, data: lineData });
});
return (
{profile.image &&
} {user.firstName}
Stats
-
Saldo: {profile.balance.toLocaleString("no")} kr
-
Siste kjøp: {formatTime(profile.lastPurchaseDate)}
-
Kjøp totalt: {profile.orderSumTotal.toLocaleString("no")} kr
Top 5 måneder
{profile.topMonths.map((topMonth) => (
-
{formatMonth(topMonth.period)}: {topMonth.count} kjøp på totalt{" "}
{topMonth.sum} kr
))}
Kjøp over tid
(
{point.data.xFormatted}
{" "}
{point.data.yFormatted} {point.serieId}
)}
/>
);
};