diff --git a/CHANGELOG.md b/CHANGELOG.md index 3148817..2eaf21b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Added exportList graphql for the download feature + ## [1.15.5] - 2023-01-25 ### Fixed diff --git a/dotnet/GraphQL/Query.cs b/dotnet/GraphQL/Query.cs index 4675775..7c11d50 100644 --- a/dotnet/GraphQL/Query.cs +++ b/dotnet/GraphQL/Query.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using WishList.GraphQL.Types; using WishList.Models; using WishList.Services; @@ -213,6 +214,26 @@ public Query(IWishListService wishListService) return listName.ToArray(); } ); + + FieldAsync>( + "exportList", + resolve: async context => + { + HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser(); + if (isValidAuthUser != HttpStatusCode.OK) + { + context.Errors.Add(new ExecutionError(isValidAuthUser.ToString()) + { + Code = isValidAuthUser.ToString() + }); + + return null; + } + + WishListsWrapper wishListsWrapper = await wishListService.ExportAllWishLists(); + return wishListsWrapper.WishLists; + } + ); } } } \ No newline at end of file diff --git a/dotnet/GraphQL/Types/ListItemsType.cs b/dotnet/GraphQL/Types/ListItemsType.cs new file mode 100644 index 0000000..7f64859 --- /dev/null +++ b/dotnet/GraphQL/Types/ListItemsType.cs @@ -0,0 +1,22 @@ +using GraphQL; +using GraphQL.Types; +using System; +using System.Collections.Generic; +using System.Text; +using WishList.Models; + +namespace WishList.GraphQL.Types +{ + [GraphQLMetadata("ListItem")] + public class ListItemsType : ObjectGraphType + { + public ListItemsType() + { + Name = "ListItemsType"; + + Field(b => b.ListItems, type: typeof(ListGraphType)).Description("List items"); + Field(b => b.IsPublic, nullable: true).Description("If the list is public"); + Field(b => b.Name, nullable: true).Description("List name"); + } + } +} diff --git a/dotnet/GraphQL/Types/WishListWrapperType.cs b/dotnet/GraphQL/Types/WishListWrapperType.cs new file mode 100644 index 0000000..a5f3b87 --- /dev/null +++ b/dotnet/GraphQL/Types/WishListWrapperType.cs @@ -0,0 +1,23 @@ +using GraphQL; +using GraphQL.Types; +using System; +using System.Collections.Generic; +using System.Text; +using WishList.Models; +using WishList.Services; + +namespace WishList.GraphQL.Types +{ + [GraphQLMetadata("WishListWrapperType")] + public class WishListWrapperType : ObjectGraphType + { + public WishListWrapperType(IWishListService wishListService) + { + Name = "WishListWrapperType"; + + Field(b => b.Id).Description("The wishlist Id"); + Field(b => b.Email).Description("The shopper Id of the wishlist"); + Field(b => b.ListItemsWrapper, type: typeof(ListGraphType)).Description("Wrapper of the list items"); + } + } +} \ No newline at end of file diff --git a/dotnet/Services/IWishListService.cs b/dotnet/Services/IWishListService.cs index d5ea81e..bf63592 100644 --- a/dotnet/Services/IWishListService.cs +++ b/dotnet/Services/IWishListService.cs @@ -15,5 +15,6 @@ public interface IWishListService Task> LimitList(IList listItems, int from, int to); Task IsValidAuthUser(); Task ValidateUserToken(string token); + Task ExportAllWishLists(); } } \ No newline at end of file diff --git a/dotnet/Services/WishListService.cs b/dotnet/Services/WishListService.cs index 2e59438..c7d5048 100644 --- a/dotnet/Services/WishListService.cs +++ b/dotnet/Services/WishListService.cs @@ -265,5 +265,11 @@ public async Task IsValidAuthUser() return HttpStatusCode.OK; } + + public async Task ExportAllWishLists() + { + WishListsWrapper wishListsWrapper = await _wishListRepository.GetAllLists(); + return wishListsWrapper; + } } } diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 5e13cfa..f955d76 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -32,6 +32,16 @@ input ListItemInputType { title: String } +type WishListWrapperType { + email: String + listItemsWrapper: [ListItems] +} + +type ListItems { + listItems: [ListItem] + isPublic: Boolean +} + type Query { viewList(shopperId: String!, name: String!, from: Int, to: Int): WishList @cacheControl(scope: PRIVATE, maxAge: ZERO) @@ -40,6 +50,8 @@ type Query { checkList(shopperId: String!, productId: String!, sku: String): CheckListType @cacheControl(scope: PRIVATE) listNames(shopperId: String!): [String] + exportList: [WishListWrapperType] + @cacheControl(scope: PRIVATE) } type Mutation { diff --git a/react/WishlistAdmin.tsx b/react/WishlistAdmin.tsx index 5126d66..c66e86c 100644 --- a/react/WishlistAdmin.tsx +++ b/react/WishlistAdmin.tsx @@ -1,5 +1,6 @@ import React, { FC, useState } from 'react' import { injectIntl, defineMessages } from 'react-intl' +import { useQuery } from 'react-apollo' import { Layout, PageBlock, @@ -9,6 +10,8 @@ import { } from 'vtex.styleguide' import XLSX from 'xlsx' +import exportList from './queries/exportList.gql' + const WishlistAdmin: FC = ({ intl }) => { const [state, setState] = useState({ loading: false, @@ -43,15 +46,17 @@ const WishlistAdmin: FC = ({ intl }) => { XLSX.writeFile(wb, exportFileName) } - const getAllWishlists = async () => { - setState({ ...state, loading: true }) + const { data, loading: queryLoading } = useQuery(exportList, { + fetchPolicy: 'no-cache', + }) - const data: any = await fetch(`/_v/wishlist/export-lists`).then(response => - response.json() - ) - const wishlistArr = data.wishLists + const GetAllWishlists = async () => { + setState({ ...state, loading: true }) - downloadWishlist(wishlistArr) + if (!queryLoading) { + const parsedData = data?.exportList + downloadWishlist(parsedData) + } setState({ ...state, loading: false }) } @@ -81,7 +86,7 @@ const WishlistAdmin: FC = ({ intl }) => { icon={download} isLoading={loading} onClick={() => { - getAllWishlists() + GetAllWishlists() }} > {intl.formatMessage(messages.download)} diff --git a/react/queries/exportList.gql b/react/queries/exportList.gql new file mode 100644 index 0000000..8759625 --- /dev/null +++ b/react/queries/exportList.gql @@ -0,0 +1,13 @@ +query ExportList { + exportList @context(provider: "vtex.wish-list") { + email + listItemsWrapper { + listItems { + id + productId + sku + title + } + } + } +} \ No newline at end of file