✨ Dont show inactive users in register view
This commit is contained in:
1 parent
46c52b7618
commit
350d31d8d8
8 files changed
+221
-68
No files matched your search
@@ -1,21 +1,25 @@
|
||||
import React from "react";
|
||||
|
||||
import { Product, useAllProductsQuery } from "../types";
|
||||
import { Product, User } from "../types";
|
||||
import { useRegister } from "./useRegister";
|
||||
|
||||
export const Register: React.FC = () => {
|
||||
const { data, error, loading } = useAllProductsQuery({ variables: { active: true } });
|
||||
if (error) {
|
||||
console.log("🔥", error);
|
||||
return null;
|
||||
}
|
||||
const { users, products, loading, showAll, setShowAll } = useRegister();
|
||||
if (loading) {
|
||||
return <div>...</div>;
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
{data?.allProducts.map((product: Product) => {
|
||||
return <div key={product.id}>{product.name}</div>;
|
||||
})}{" "}
|
||||
<section>
|
||||
{users.map((user: User) => {
|
||||
return <div key={user.id}>{user.firstName}</div>;
|
||||
})}
|
||||
</section>
|
||||
<section>
|
||||
{products.map((product: Product) => {
|
||||
return <div key={product.id}>{product.name}</div>;
|
||||
})}
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
query allUsers {
|
||||
allUsers {
|
||||
id
|
||||
firstName
|
||||
lastName
|
||||
profile {
|
||||
id
|
||||
balance
|
||||
image
|
||||
lastPurchaseDate
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { parseISO, subDays } from "date-fns";
|
||||
import { useState } from "react";
|
||||
|
||||
import { useAllProductsQuery, useAllUsersQuery, User } from "../types";
|
||||
|
||||
const activeLimitDays = 365 / 2;
|
||||
|
||||
export const useRegister = () => {
|
||||
const [showAll, setShowAll] = useState(false);
|
||||
const { data, loading } = useAllProductsQuery({ variables: { active: true } });
|
||||
const { data: usersData, loading: usersLoading } = useAllUsersQuery();
|
||||
|
||||
const allUsers = usersData?.allUsers || [];
|
||||
const users = showAll
|
||||
? allUsers
|
||||
: allUsers.filter((user: User) => {
|
||||
const balance = user.profile?.balance || 0;
|
||||
const active = parseISO(user.profile?.lastPurchaseDate) >= subDays(new Date(), activeLimitDays);
|
||||
return balance >= 15 && active;
|
||||
});
|
||||
|
||||
return { loading: loading || usersLoading, users, products: data?.allProducts || [], showAll, setShowAll };
|
||||
};
|
||||
Reference in new issue
Block a user