Skip to content

Commit

Permalink
Merge pull request #153 from vtex-apps/bugfix/WISHLIST-exportList-gra…
Browse files Browse the repository at this point in the history
…phql

WISHLIST exportList graphql
  • Loading branch information
auroraaaashen authored Jan 26, 2023
2 parents 789e4a3 + 11cc365 commit 4a057ca
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions dotnet/GraphQL/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -213,6 +214,26 @@ public Query(IWishListService wishListService)
return listName.ToArray();
}
);

FieldAsync<ListGraphType<WishListWrapperType>>(
"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;
}
);
}
}
}
22 changes: 22 additions & 0 deletions dotnet/GraphQL/Types/ListItemsType.cs
Original file line number Diff line number Diff line change
@@ -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<ListItemsWrapper>
{
public ListItemsType()
{
Name = "ListItemsType";

Field(b => b.ListItems, type: typeof(ListGraphType<ListItemType>)).Description("List items");
Field(b => b.IsPublic, nullable: true).Description("If the list is public");
Field(b => b.Name, nullable: true).Description("List name");
}
}
}
23 changes: 23 additions & 0 deletions dotnet/GraphQL/Types/WishListWrapperType.cs
Original file line number Diff line number Diff line change
@@ -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<WishListWrapper>
{
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<ListItemsType>)).Description("Wrapper of the list items");
}
}
}
1 change: 1 addition & 0 deletions dotnet/Services/IWishListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public interface IWishListService
Task<IList<ListItem>> LimitList(IList<ListItem> listItems, int from, int to);
Task<HttpStatusCode> IsValidAuthUser();
Task<ValidatedUser> ValidateUserToken(string token);
Task<WishListsWrapper> ExportAllWishLists();
}
}
6 changes: 6 additions & 0 deletions dotnet/Services/WishListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,11 @@ public async Task<HttpStatusCode> IsValidAuthUser()

return HttpStatusCode.OK;
}

public async Task<WishListsWrapper> ExportAllWishLists()
{
WishListsWrapper wishListsWrapper = await _wishListRepository.GetAllLists();
return wishListsWrapper;
}
}
}
12 changes: 12 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
21 changes: 13 additions & 8 deletions react/WishlistAdmin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { FC, useState } from 'react'
import { injectIntl, defineMessages } from 'react-intl'
import { useQuery } from 'react-apollo'
import {
Layout,
PageBlock,
Expand All @@ -9,6 +10,8 @@ import {
} from 'vtex.styleguide'
import XLSX from 'xlsx'

import exportList from './queries/exportList.gql'

const WishlistAdmin: FC<any> = ({ intl }) => {
const [state, setState] = useState<any>({
loading: false,
Expand Down Expand Up @@ -43,15 +46,17 @@ const WishlistAdmin: FC<any> = ({ 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 })
}

Expand Down Expand Up @@ -81,7 +86,7 @@ const WishlistAdmin: FC<any> = ({ intl }) => {
icon={download}
isLoading={loading}
onClick={() => {
getAllWishlists()
GetAllWishlists()
}}
>
{intl.formatMessage(messages.download)}
Expand Down
13 changes: 13 additions & 0 deletions react/queries/exportList.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query ExportList {
exportList @context(provider: "vtex.wish-list") {
email
listItemsWrapper {
listItems {
id
productId
sku
title
}
}
}
}

0 comments on commit 4a057ca

Please sign in to comment.