Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-qc committed Nov 27, 2020
1 parent 5c7e535 commit 3cffddb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const CollectionDetails = (props: IProps) => {
<h2 className="ng-text-display-s ng-body-color ng-margin-medium-bottom ng-margin-top-remove">
{t('Download metrics')}
&nbsp;
<i className="ng-icon-download-outline" style={{ verticalAlign: 'middle' }} />
<i className="ng-icon-download-outline ng-vertical-align-middle" />
</h2>
<p>
{isDownloadingMetrics ? (
Expand Down Expand Up @@ -205,7 +205,8 @@ const CollectionDetails = (props: IProps) => {
onCancel={() => setIsOnDownloadMetrics(false)}
onDownloadStart={() => [setIsDownloadingMetrics(true), setIsOnDownloadMetrics(false)]}
onDownloadEnd={() => setIsDownloadingMetrics(false)}
onDownloadError={(err) => setDownloadError(err)}
onDownloadError={setDownloadError}
onDownloadSuccess={() => setDownloadError('')}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Controller, useForm } from 'react-hook-form';

import { TitleHero, Card } from '@marapp/earth-shared';
import { TitleHero, Card, ReactSelect, serializeFilters } from '@marapp/earth-shared';
import MetricService from 'services/MetricService';
import { ReactSelect, serializeFilters } from '@marapp/earth-shared';

import JSZip from 'jszip';
import FileSaver from 'file-saver';
Expand All @@ -38,10 +37,18 @@ interface IProps {
onDownloadStart: () => void;
onDownloadEnd: () => void;
onDownloadError: (err: string) => void;
onDownloadSuccess: () => void;
}

export function CollectionDownloadMetrics(props: IProps) {
const { collection, onCancel, onDownloadStart, onDownloadEnd, onDownloadError } = props;
const {
collection,
onCancel,
onDownloadStart,
onDownloadEnd,
onDownloadError,
onDownloadSuccess,
} = props;
const { t } = useTranslation();
const { name, organization, slug: collectionSlug } = collection;
const [metricSlugs, setMetricSlugs] = useState([]);
Expand Down Expand Up @@ -159,10 +166,11 @@ export function CollectionDownloadMetrics(props: IProps) {
const zip = new JSZip();

const metricTypes = groupBy(data, 'slug');
const locationNameField = '#';

Object.keys(metricTypes).forEach((metricType) => {
const normalizedData = metricTypes[metricType].map((item) => ({
'#': item.location.name,
[locationNameField]: item.location.name,
...item.metric,
}));
const fileName = `${metricType}.${fileType}`;
Expand All @@ -173,9 +181,9 @@ export function CollectionDownloadMetrics(props: IProps) {
zip.file(
fileName,
json2csvParser.parse(
normalizedData.map((item) => ({
'#': item['#'],
...flatten(omit(item, '#')),
normalizedData.map(({ [locationNameField]: locationName, ...item }) => ({
[locationNameField]: locationName,
...flatten(item),
}))
)
);
Expand All @@ -188,6 +196,7 @@ export function CollectionDownloadMetrics(props: IProps) {
const zipContent = await zip.generateAsync({ type: 'blob' });

FileSaver.saveAs(zipContent, zipName);
onDownloadSuccess();
} catch (e) {
onDownloadError('Something went wrong');
console.log(e);
Expand Down

0 comments on commit 3cffddb

Please sign in to comment.