-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from vtex-apps/feature/wishlist-download
Wishlist Download
- Loading branch information
Showing
16 changed files
with
415 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"section": "products", | ||
"subSection": "catalog", | ||
"subSectionItems": [ | ||
{ | ||
"labelId": "admin/wishlist.menu.label", | ||
"searchKeyWordsHelpers": "", | ||
"path": "/admin/wishlist" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"admin.app.wishlist": { | ||
"component": "WishlistAdmin", | ||
"path": "/admin/app/wishlist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,38 @@ | ||
namespace service.Controllers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Newtonsoft.Json; | ||
using Vtex.Api.Context; | ||
using WishList.Data; | ||
using WishList.Models; | ||
|
||
public class RoutesController : Controller | ||
{ | ||
private readonly IIOServiceContext _context; | ||
private readonly IWishListRepository _wishListRepository; | ||
|
||
public RoutesController(IIOServiceContext context) | ||
public RoutesController(IIOServiceContext context, IWishListRepository wishListRepository) | ||
{ | ||
this._context = context ?? throw new ArgumentNullException(nameof(context)); | ||
this._wishListRepository = wishListRepository ?? throw new ArgumentNullException(nameof(wishListRepository)); | ||
} | ||
|
||
public async Task<IActionResult> ExportAllLists() | ||
{ | ||
WishListsWrapper wishListsWrapper = await _wishListRepository.GetAllLists(); | ||
var queryString = HttpContext.Request.Query; | ||
int from = int.Parse(queryString["from"]); | ||
int to = int.Parse(queryString["to"]); | ||
IList<WishListWrapper> wishListsWrappers = wishListsWrapper.WishLists; | ||
wishListsWrapper.WishLists = wishListsWrappers.Skip(from - 1).Take(to - from).ToList(); | ||
|
||
return Json(wishListsWrapper); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-await-in-loop */ | ||
/* eslint-disable no-loop-func */ | ||
import React, { FC, useState } from 'react' | ||
import { injectIntl, defineMessages } from 'react-intl' | ||
import { | ||
Layout, | ||
PageBlock, | ||
PageHeader, | ||
ButtonWithIcon, | ||
IconDownload, | ||
} from 'vtex.styleguide' | ||
import XLSX from 'xlsx' | ||
|
||
const WishlistAdmin: FC<any> = ({ intl }) => { | ||
const [state, setState] = useState<any>({ | ||
loading: false, | ||
}) | ||
|
||
const { loading } = state | ||
|
||
const fetchWishlists = async (from: number, to: number) => { | ||
const response: any = await fetch( | ||
`/_v/wishlist/export-lists?from=${from}&to=${to}`, | ||
{ mode: 'no-cors' } | ||
) | ||
|
||
return response.json() | ||
} | ||
|
||
const downloadWishlist = (allWishlists: any) => { | ||
const header = ['Email', 'Product ID', 'SKU', 'Title'] | ||
const data: any = [] | ||
|
||
for (const shopper of allWishlists) { | ||
const wishlists = shopper.listItemsWrapper | ||
for (const wishlist of wishlists) { | ||
for (const wishlistItem of wishlist.listItems) { | ||
const shopperData = { | ||
Email: shopper.email, | ||
'Product ID': wishlistItem.productId, | ||
SKU: wishlistItem.sku, | ||
Title: wishlistItem.title, | ||
} | ||
|
||
data.push(shopperData) | ||
} | ||
} | ||
} | ||
|
||
const ws = XLSX.utils.json_to_sheet(data, { header }) | ||
const wb = XLSX.utils.book_new() | ||
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1') | ||
const exportFileName = `wishlists.xls` | ||
XLSX.writeFile(wb, exportFileName) | ||
} | ||
|
||
let allWishlists: any = [] | ||
|
||
const getAllWishlists = async () => { | ||
let status = true | ||
let i = 0 | ||
const chunkLength = 100 | ||
setState({ ...state, loading: true }) | ||
|
||
while (status) { | ||
await fetchWishlists(i, i + chunkLength).then(data => { | ||
const wishlistArr = data.wishLists | ||
|
||
if (!wishlistArr.length) { | ||
status = false | ||
} | ||
allWishlists = [...allWishlists, ...wishlistArr] | ||
}) | ||
|
||
i += 100 | ||
} | ||
|
||
downloadWishlist(allWishlists) | ||
setState({ ...state, loading: false }) | ||
} | ||
|
||
const messages = defineMessages({ | ||
title: { | ||
id: 'admin/wishlist.menu.label', | ||
defaultMessage: 'Wishlist', | ||
}, | ||
exportLabel: { | ||
id: 'admin/settings.title', | ||
defaultMessage: 'Wishlist Export', | ||
}, | ||
download: { | ||
id: 'admin/settings.download', | ||
defaultMessage: 'Download Wishlists', | ||
}, | ||
}) | ||
|
||
const download = <IconDownload /> | ||
|
||
return ( | ||
<Layout | ||
pageHeader={<PageHeader title={intl.formatMessage(messages.title)} />} | ||
> | ||
<PageBlock variation="full"> | ||
<ButtonWithIcon | ||
icon={download} | ||
isLoading={loading} | ||
onClick={() => { | ||
getAllWishlists() | ||
}} | ||
> | ||
{intl.formatMessage(messages.download)} | ||
</ButtonWithIcon> | ||
</PageBlock> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default injectIntl(WishlistAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,8 @@ | |
"lodash": "^4.17.15", | ||
"react-apollo": "^3.1.3", | ||
"react-dom": "^16.9.2", | ||
"react-intl": "^5.10.17" | ||
"react-intl": "^5.10.17", | ||
"xlsx": "^0.15.6" | ||
}, | ||
"devDependencies": { | ||
"@types/classnames": "^2.2.7", | ||
|
@@ -23,13 +24,26 @@ | |
"react": "^16.9.2", | ||
"react-apollo": "^3.1.3", | ||
"typescript": "3.9.7", | ||
"vtex.add-to-cart-button": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.add-to-cart-button", | ||
"vtex.css-handles": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles", | ||
"vtex.render-runtime": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.render-runtime", | ||
"vtex.rich-text": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.rich-text", | ||
"vtex.search-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.search-graphql", | ||
"vtex.store-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-graphql", | ||
"vtex.store-resources": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-resources", | ||
"vtex.styleguide": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.styleguide" | ||
"vtex.flex-layout": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.flex-layout", | ||
"vtex.list-context": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.list-context", | ||
"vtex.my-account": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.my-account", | ||
"vtex.my-account-commons": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.my-account-commons", | ||
"vtex.product-context": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.product-context", | ||
"vtex.product-list-context": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.product-list-context", | ||
"vtex.product-quantity": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.product-quantity", | ||
"vtex.product-specification-badges": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.product-specification-badges", | ||
"vtex.product-summary": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.product-summary", | ||
"vtex.render-runtime": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.render-runtime", | ||
"vtex.rich-text": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.rich-text", | ||
"vtex.search-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.search-graphql", | ||
"vtex.slider-layout": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.slider-layout", | ||
"vtex.store": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store", | ||
"vtex.store-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-graphql", | ||
"vtex.store-icons": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-icons", | ||
"vtex.store-resources": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-resources", | ||
"vtex.styleguide": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.styleguide" | ||
}, | ||
"version": "1.6.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
{ | ||
"extends": "@vtex/tsconfig", | ||
"compilerOptions": { | ||
"jsx": "react", | ||
"esModuleInterop": true, | ||
"lib": ["es2017", "dom", "es2018.promise"] | ||
"lib": ["es2017", "dom", "es2018.promise"], | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"target": "es2017" | ||
}, | ||
"include": ["./typings/*.d.ts", "./**/*.tsx", "./**/*.ts"] | ||
} |
Oops, something went wrong.