Skip to content

Commit

Permalink
feat(storefront): ✨ added download invoice button
Browse files Browse the repository at this point in the history
  • Loading branch information
sahrohit committed Aug 10, 2023
1 parent 4ce7e1c commit 3e85637
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
10 changes: 10 additions & 0 deletions apps/admin/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,11 @@ export type Query = {
allReviews?: Maybe<Array<ProductReview>>;
categories: Array<ProductCategory>;
categoriesSummary?: Maybe<Array<ProductCategoryWithProductCount>>;
emailInvoice: Scalars["Boolean"];
favourites: Array<Favourite>;
favouritesWithProduct: Array<Favourite>;
fetchCartItems?: Maybe<Array<Cart>>;
generate?: Maybe<Scalars["String"]>;
hello: Scalars["String"];
me?: Maybe<User>;
orderById?: Maybe<OrderDetail>;
Expand All @@ -464,6 +466,14 @@ export type QueryAllReviewsArgs = {
productId: Scalars["Int"];
};

export type QueryEmailInvoiceArgs = {
orderId: Scalars["String"];
};

export type QueryGenerateArgs = {
orderId: Scalars["String"];
};

export type QueryOrderByIdArgs = {
orderId: Scalars["String"];
};
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import theme from "@/config/theme";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
import "@/styles/globals.css";
import { ChakraProvider } from "@chakra-ui/react";
import type { AppProps } from "next/app";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
import theme from "@/config/theme";

const App = ({ Component, pageProps }: AppProps) => {
const client = new ApolloClient({
Expand Down
Binary file removed apps/api/644e9668-33b1-4d0d-ac41-431fd9f3378b.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion apps/api/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const FORGOT_PASSWORD_PREFIX = "forgot-password:";
export const VERIFY_EMAIL_PREFIX = "verify-email:";
export const COMPANY = {
name: "Hamropasal",
logo: `${process.env.CLIENT_URL}/logo.png`,
logo: `https://hamropasal.vercel.app/logo.png`,
address: "Tikathali, Mahalaxmi",
zip: "44708",
city: "Kathmandu",
Expand Down
26 changes: 24 additions & 2 deletions apps/storefront/src/components/pages/account/order/OrderCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import dayjs from "dayjs";
import { useMemo } from "react";
import { BiDownload } from "react-icons/bi";
import { Link } from "@chakra-ui/next-js";
import { OrderDetail, OrderItem } from "@/generated/graphql";
import {
OrderDetail,
OrderItem,
useGenerateInvoiceLazyQuery,
} from "@/generated/graphql";
import {
OrderInfo,
colorFromStatus,
Expand All @@ -44,6 +48,9 @@ const OrderCard = ({ orderItem }: OrderCardProps) => {
(payment) => payment.status === "COMPLETED"
);

const [generateInvoice, { loading: invoiceLoading }] =
useGenerateInvoiceLazyQuery();

const subTotal = useMemo(
() =>
orderItem.orderitems?.reduce(
Expand Down Expand Up @@ -79,7 +86,22 @@ const OrderCard = ({ orderItem }: OrderCardProps) => {
<Button size="sm" as={Link} href={`/order/${orderItem.id}`}>
View Order
</Button>
<Button size="sm" leftIcon={<BiDownload />}>
<Button
size="sm"
leftIcon={<BiDownload />}
isLoading={invoiceLoading}
onClick={async () => {
const res = await generateInvoice({
variables: {
orderId: orderItem.id,
},
});
window.open(
`data:application/pdf;base64,${res.data?.generate}`,
"_blank"
);
}}
>
Download Invoice
</Button>
</HStack>
Expand Down
75 changes: 75 additions & 0 deletions apps/storefront/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,11 @@ export type Query = {
allReviews?: Maybe<Array<ProductReview>>;
categories: Array<ProductCategory>;
categoriesSummary?: Maybe<Array<ProductCategoryWithProductCount>>;
emailInvoice: Scalars["Boolean"];
favourites: Array<Favourite>;
favouritesWithProduct: Array<Favourite>;
fetchCartItems?: Maybe<Array<Cart>>;
generate?: Maybe<Scalars["String"]>;
hello: Scalars["String"];
me?: Maybe<User>;
orderById?: Maybe<OrderDetail>;
Expand All @@ -464,6 +466,14 @@ export type QueryAllReviewsArgs = {
productId: Scalars["Int"];
};

export type QueryEmailInvoiceArgs = {
orderId: Scalars["String"];
};

export type QueryGenerateArgs = {
orderId: Scalars["String"];
};

export type QueryOrderByIdArgs = {
orderId: Scalars["String"];
};
Expand Down Expand Up @@ -1825,6 +1835,15 @@ export type FavouritesWithProductQuery = {
}>;
};

export type GenerateInvoiceQueryVariables = Exact<{
orderId: Scalars["String"];
}>;

export type GenerateInvoiceQuery = {
__typename?: "Query";
generate?: string | null;
};

export type OrderByIdQueryVariables = Exact<{
orderId: Scalars["String"];
}>;
Expand Down Expand Up @@ -4230,6 +4249,62 @@ export type FavouritesWithProductQueryResult = Apollo.QueryResult<
FavouritesWithProductQuery,
FavouritesWithProductQueryVariables
>;
export const GenerateInvoiceDocument = gql`
query GenerateInvoice($orderId: String!) {
generate(orderId: $orderId)
}
`;

/**
* __useGenerateInvoiceQuery__
*
* To run a query within a React component, call `useGenerateInvoiceQuery` and pass it any options that fit your needs.
* When your component renders, `useGenerateInvoiceQuery` 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 } = useGenerateInvoiceQuery({
* variables: {
* orderId: // value for 'orderId'
* },
* });
*/
export function useGenerateInvoiceQuery(
baseOptions: Apollo.QueryHookOptions<
GenerateInvoiceQuery,
GenerateInvoiceQueryVariables
>
) {
const options = { ...defaultOptions, ...baseOptions };
return Apollo.useQuery<GenerateInvoiceQuery, GenerateInvoiceQueryVariables>(
GenerateInvoiceDocument,
options
);
}
export function useGenerateInvoiceLazyQuery(
baseOptions?: Apollo.LazyQueryHookOptions<
GenerateInvoiceQuery,
GenerateInvoiceQueryVariables
>
) {
const options = { ...defaultOptions, ...baseOptions };
return Apollo.useLazyQuery<
GenerateInvoiceQuery,
GenerateInvoiceQueryVariables
>(GenerateInvoiceDocument, options);
}
export type GenerateInvoiceQueryHookResult = ReturnType<
typeof useGenerateInvoiceQuery
>;
export type GenerateInvoiceLazyQueryHookResult = ReturnType<
typeof useGenerateInvoiceLazyQuery
>;
export type GenerateInvoiceQueryResult = Apollo.QueryResult<
GenerateInvoiceQuery,
GenerateInvoiceQueryVariables
>;
export const OrderByIdDocument = gql`
query OrderById($orderId: String!) {
orderById(orderId: $orderId) {
Expand Down
3 changes: 3 additions & 0 deletions apps/storefront/src/graphql/query/invoice/generate.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query GenerateInvoice($orderId: String!) {
generate(orderId: $orderId)
}

2 comments on commit 3e85637

@vercel
Copy link

@vercel vercel bot commented on 3e85637 Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 3e85637 Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ecommerce-admin-client – ./apps/admin

ecommerce-admin-client-git-main-sahrohit.vercel.app
ecommerce-admin-client.vercel.app
ecommerce-admin-client-sahrohit.vercel.app

Please sign in to comment.