✨ Dont show inactive users in register view
This commit is contained in:
1 parent
46c52b7618
commit
350d31d8d8
8 files changed
+206
-53
No files matched your search
Generated
+26
-8
@@ -9,6 +9,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.4.16",
|
"@apollo/client": "^3.4.16",
|
||||||
|
"date-fns": "^2.25.0",
|
||||||
"graphql": "^15.7.2",
|
"graphql": "^15.7.2",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2"
|
"react-dom": "^17.0.2"
|
||||||
@@ -4123,10 +4124,16 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/date-fns": {
|
"node_modules/date-fns": {
|
||||||
"version": "1.30.1",
|
"version": "2.25.0",
|
||||||
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.30.1.tgz",
|
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.25.0.tgz",
|
||||||
"integrity": "sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==",
|
"integrity": "sha512-ovYRFnTrbGPD4nqaEqescPEv1mNwvt+UTqI3Ay9SzNtey9NZnYu6E2qCcBBgJ6/2VF1zGGygpyTDITqpQQ5e+w==",
|
||||||
"dev": true
|
"engines": {
|
||||||
|
"node": ">=0.11"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/date-fns"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"node_modules/debounce": {
|
"node_modules/debounce": {
|
||||||
"version": "1.2.1",
|
"version": "1.2.1",
|
||||||
@@ -8041,6 +8048,12 @@
|
|||||||
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
|
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node_modules/listr-verbose-renderer/node_modules/date-fns": {
|
||||||
|
"version": "1.30.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.30.1.tgz",
|
||||||
|
"integrity": "sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"node_modules/listr-verbose-renderer/node_modules/escape-string-regexp": {
|
"node_modules/listr-verbose-renderer/node_modules/escape-string-regexp": {
|
||||||
"version": "1.0.5",
|
"version": "1.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
|
||||||
@@ -14637,10 +14650,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"date-fns": {
|
"date-fns": {
|
||||||
"version": "1.30.1",
|
"version": "2.25.0",
|
||||||
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.30.1.tgz",
|
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.25.0.tgz",
|
||||||
"integrity": "sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==",
|
"integrity": "sha512-ovYRFnTrbGPD4nqaEqescPEv1mNwvt+UTqI3Ay9SzNtey9NZnYu6E2qCcBBgJ6/2VF1zGGygpyTDITqpQQ5e+w=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"debounce": {
|
"debounce": {
|
||||||
"version": "1.2.1",
|
"version": "1.2.1",
|
||||||
@@ -17578,6 +17590,12 @@
|
|||||||
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
|
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"date-fns": {
|
||||||
|
"version": "1.30.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.30.1.tgz",
|
||||||
|
"integrity": "sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"escape-string-regexp": {
|
"escape-string-regexp": {
|
||||||
"version": "1.0.5",
|
"version": "1.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.4.16",
|
"@apollo/client": "^3.4.16",
|
||||||
|
"date-fns": "^2.25.0",
|
||||||
"graphql": "^15.7.2",
|
"graphql": "^15.7.2",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2"
|
"react-dom": "^17.0.2"
|
||||||
|
|||||||
@@ -1,21 +1,25 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { Product, useAllProductsQuery } from "../types";
|
import { Product, User } from "../types";
|
||||||
|
import { useRegister } from "./useRegister";
|
||||||
|
|
||||||
export const Register: React.FC = () => {
|
export const Register: React.FC = () => {
|
||||||
const { data, error, loading } = useAllProductsQuery({ variables: { active: true } });
|
const { users, products, loading, showAll, setShowAll } = useRegister();
|
||||||
if (error) {
|
|
||||||
console.log("🔥", error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return <div>...</div>;
|
return <div>...</div>;
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{data?.allProducts.map((product: Product) => {
|
<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>;
|
return <div key={product.id}>{product.name}</div>;
|
||||||
})}{" "}
|
})}
|
||||||
|
</section>
|
||||||
</div>
|
</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 };
|
||||||
|
};
|
||||||
+85
-21
@@ -1,10 +1,10 @@
|
|||||||
import { gql } from "@apollo/client";
|
import { gql } from '@apollo/client';
|
||||||
import * as Apollo from "@apollo/client";
|
import * as Apollo from '@apollo/client';
|
||||||
export type Maybe<T> = T | null;
|
export type Maybe<T> = T | null;
|
||||||
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
|
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
|
||||||
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
|
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
|
||||||
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
|
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
|
||||||
const defaultOptions = {};
|
const defaultOptions = {}
|
||||||
/** All built-in and custom scalars, mapped to their actual values */
|
/** All built-in and custom scalars, mapped to their actual values */
|
||||||
export type Scalars = {
|
export type Scalars = {
|
||||||
ID: string;
|
ID: string;
|
||||||
@@ -12,38 +12,62 @@ export type Scalars = {
|
|||||||
Boolean: boolean;
|
Boolean: boolean;
|
||||||
Int: number;
|
Int: number;
|
||||||
Float: number;
|
Float: number;
|
||||||
|
/** Date with time (isoformat) */
|
||||||
|
DateTime: any;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Product = {
|
export type Product = {
|
||||||
__typename?: "Product";
|
__typename?: 'Product';
|
||||||
id: Scalars["ID"];
|
id: Scalars['ID'];
|
||||||
name: Scalars["String"];
|
name: Scalars['String'];
|
||||||
salePriceExt: Scalars["Float"];
|
salePriceExt: Scalars['Float'];
|
||||||
salePriceInt: Scalars["Float"];
|
salePriceInt: Scalars['Float'];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ProductFilter = {
|
export type ProductFilter = {
|
||||||
active?: Maybe<Scalars["Boolean"]>;
|
active?: Maybe<Scalars['Boolean']>;
|
||||||
id?: Maybe<Scalars["ID"]>;
|
id?: Maybe<Scalars['ID']>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Query = {
|
export type Query = {
|
||||||
__typename?: "Query";
|
__typename?: 'Query';
|
||||||
allProducts: Array<Product>;
|
allProducts: Array<Product>;
|
||||||
|
allUsers: Array<User>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export type QueryAllProductsArgs = {
|
export type QueryAllProductsArgs = {
|
||||||
filters?: Maybe<ProductFilter>;
|
filters?: Maybe<ProductFilter>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type User = {
|
||||||
|
__typename?: 'User';
|
||||||
|
firstName: Scalars['String'];
|
||||||
|
id: Scalars['ID'];
|
||||||
|
lastName: Scalars['String'];
|
||||||
|
profile?: Maybe<UserProfile>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type UserProfile = {
|
||||||
|
__typename?: 'UserProfile';
|
||||||
|
balance: Scalars['Float'];
|
||||||
|
id: Scalars['ID'];
|
||||||
|
image: Scalars['String'];
|
||||||
|
lastPurchaseDate: Scalars['DateTime'];
|
||||||
|
};
|
||||||
|
|
||||||
export type AllProductsQueryVariables = Exact<{
|
export type AllProductsQueryVariables = Exact<{
|
||||||
active: Scalars["Boolean"];
|
active: Scalars['Boolean'];
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
export type AllProductsQuery = {
|
|
||||||
__typename?: "Query";
|
export type AllProductsQuery = { __typename?: 'Query', allProducts: Array<{ __typename?: 'Product', id: string, name: string, salePriceExt: number, salePriceInt: number }> };
|
||||||
allProducts: Array<{ __typename?: "Product"; id: string; name: string; salePriceExt: number; salePriceInt: number }>;
|
|
||||||
};
|
export type AllUsersQueryVariables = Exact<{ [key: string]: never; }>;
|
||||||
|
|
||||||
|
|
||||||
|
export type AllUsersQuery = { __typename?: 'Query', allUsers: Array<{ __typename?: 'User', id: string, firstName: string, lastName: string, profile?: { __typename?: 'UserProfile', id: string, balance: number, image: string, lastPurchaseDate: any } | null | undefined }> };
|
||||||
|
|
||||||
|
|
||||||
export const AllProductsDocument = gql`
|
export const AllProductsDocument = gql`
|
||||||
query allProducts($active: Boolean!) {
|
query allProducts($active: Boolean!) {
|
||||||
@@ -73,15 +97,55 @@ export const AllProductsDocument = gql`
|
|||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
export function useAllProductsQuery(baseOptions: Apollo.QueryHookOptions<AllProductsQuery, AllProductsQueryVariables>) {
|
export function useAllProductsQuery(baseOptions: Apollo.QueryHookOptions<AllProductsQuery, AllProductsQueryVariables>) {
|
||||||
const options = { ...defaultOptions, ...baseOptions };
|
const options = {...defaultOptions, ...baseOptions}
|
||||||
return Apollo.useQuery<AllProductsQuery, AllProductsQueryVariables>(AllProductsDocument, options);
|
return Apollo.useQuery<AllProductsQuery, AllProductsQueryVariables>(AllProductsDocument, options);
|
||||||
}
|
}
|
||||||
export function useAllProductsLazyQuery(
|
export function useAllProductsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<AllProductsQuery, AllProductsQueryVariables>) {
|
||||||
baseOptions?: Apollo.LazyQueryHookOptions<AllProductsQuery, AllProductsQueryVariables>
|
const options = {...defaultOptions, ...baseOptions}
|
||||||
) {
|
|
||||||
const options = { ...defaultOptions, ...baseOptions };
|
|
||||||
return Apollo.useLazyQuery<AllProductsQuery, AllProductsQueryVariables>(AllProductsDocument, options);
|
return Apollo.useLazyQuery<AllProductsQuery, AllProductsQueryVariables>(AllProductsDocument, options);
|
||||||
}
|
}
|
||||||
export type AllProductsQueryHookResult = ReturnType<typeof useAllProductsQuery>;
|
export type AllProductsQueryHookResult = ReturnType<typeof useAllProductsQuery>;
|
||||||
export type AllProductsLazyQueryHookResult = ReturnType<typeof useAllProductsLazyQuery>;
|
export type AllProductsLazyQueryHookResult = ReturnType<typeof useAllProductsLazyQuery>;
|
||||||
export type AllProductsQueryResult = Apollo.QueryResult<AllProductsQuery, AllProductsQueryVariables>;
|
export type AllProductsQueryResult = Apollo.QueryResult<AllProductsQuery, AllProductsQueryVariables>;
|
||||||
|
export const AllUsersDocument = gql`
|
||||||
|
query allUsers {
|
||||||
|
allUsers {
|
||||||
|
id
|
||||||
|
firstName
|
||||||
|
lastName
|
||||||
|
profile {
|
||||||
|
id
|
||||||
|
balance
|
||||||
|
image
|
||||||
|
lastPurchaseDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* __useAllUsersQuery__
|
||||||
|
*
|
||||||
|
* To run a query within a React component, call `useAllUsersQuery` and pass it any options that fit your needs.
|
||||||
|
* When your component renders, `useAllUsersQuery` returns an object from Apollo Client that contains loading, error, and data properties
|
||||||
|
* you can use to render your UI.
|
||||||
|
*
|
||||||
|
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const { data, loading, error } = useAllUsersQuery({
|
||||||
|
* variables: {
|
||||||
|
* },
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
export function useAllUsersQuery(baseOptions?: Apollo.QueryHookOptions<AllUsersQuery, AllUsersQueryVariables>) {
|
||||||
|
const options = {...defaultOptions, ...baseOptions}
|
||||||
|
return Apollo.useQuery<AllUsersQuery, AllUsersQueryVariables>(AllUsersDocument, options);
|
||||||
|
}
|
||||||
|
export function useAllUsersLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<AllUsersQuery, AllUsersQueryVariables>) {
|
||||||
|
const options = {...defaultOptions, ...baseOptions}
|
||||||
|
return Apollo.useLazyQuery<AllUsersQuery, AllUsersQueryVariables>(AllUsersDocument, options);
|
||||||
|
}
|
||||||
|
export type AllUsersQueryHookResult = ReturnType<typeof useAllUsersQuery>;
|
||||||
|
export type AllUsersLazyQueryHookResult = ReturnType<typeof useAllUsersLazyQuery>;
|
||||||
|
export type AllUsersQueryResult = Apollo.QueryResult<AllUsersQuery, AllUsersQueryVariables>;
|
||||||
+2
-16
@@ -1,27 +1,13 @@
|
|||||||
import strawberry
|
import strawberry
|
||||||
import strawberry_django
|
import strawberry_django
|
||||||
from strawberry_django import auto
|
|
||||||
|
|
||||||
from main import models
|
from main.api.types import Product, User
|
||||||
|
|
||||||
|
|
||||||
@strawberry_django.filters.filter(models.Product)
|
|
||||||
class ProductFilter:
|
|
||||||
id: auto
|
|
||||||
active: auto
|
|
||||||
|
|
||||||
|
|
||||||
@strawberry_django.type(models.Product, filters=ProductFilter)
|
|
||||||
class Product:
|
|
||||||
id: auto
|
|
||||||
name: auto
|
|
||||||
sale_price_int: auto
|
|
||||||
sale_price_ext: auto
|
|
||||||
|
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
class Query:
|
class Query:
|
||||||
all_products: list[Product] = strawberry_django.field()
|
all_products: list[Product] = strawberry_django.field()
|
||||||
|
all_users: list[User] = strawberry_django.field()
|
||||||
|
|
||||||
|
|
||||||
schema = strawberry.Schema(query=Query)
|
schema = strawberry.Schema(query=Query)
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import strawberry
|
||||||
|
import strawberry_django
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from strawberry_django import auto
|
||||||
|
|
||||||
|
from main import models
|
||||||
|
|
||||||
|
UserModel = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry_django.filters.filter(models.Product)
|
||||||
|
class ProductFilter:
|
||||||
|
id: auto
|
||||||
|
active: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry_django.type(models.Product, filters=ProductFilter)
|
||||||
|
class Product:
|
||||||
|
id: auto
|
||||||
|
name: auto
|
||||||
|
sale_price_int: auto
|
||||||
|
sale_price_ext: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry_django.type(models.UserProfile)
|
||||||
|
class UserProfile:
|
||||||
|
id: auto
|
||||||
|
balance: auto
|
||||||
|
image: auto
|
||||||
|
|
||||||
|
@strawberry.field()
|
||||||
|
def last_purchase_date(self) -> datetime:
|
||||||
|
return self.last_purchase_date()
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry_django.type(UserModel)
|
||||||
|
class User:
|
||||||
|
id: auto
|
||||||
|
first_name: auto
|
||||||
|
last_name: auto
|
||||||
|
profile: UserProfile
|
||||||
|
# todo
|
||||||
Reference in new issue
Block a user