Skip to content

Commit

Permalink
Downloaded geojson file is named after project name
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfranklin committed Nov 12, 2024
1 parent 8364d17 commit f2dffb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions react/src/components/AssetsPanel/AssetsPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { act } from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import AssetsPanel from './AssetsPanel';
import { featureCollection } from '@hazmapper/__fixtures__/featuresFixture';
import { projectMock } from '@hazmapper/__fixtures__/projectFixtures';
import { useFeatures } from '@hazmapper/hooks';

jest.mock('@hazmapper/hooks', () => ({
Expand All @@ -18,8 +19,8 @@ jest.mock('@hazmapper/components/FeatureFileTree', () => {
describe('AssetsPanel', () => {
const defaultProps = {
featureCollection,
projectId: 1,
isPublic: false,
project: projectMock,
isPublicView: false,
};

beforeEach(() => {
Expand Down
28 changes: 16 additions & 12 deletions react/src/components/AssetsPanel/AssetsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import React from 'react';
import styles from './AssetsPanel.module.css';
import FeatureFileTree from '@hazmapper/components/FeatureFileTree';
import { FeatureCollection } from '@hazmapper/types';
import { FeatureCollection, Project } from '@hazmapper/types';
import { Button } from '@tacc/core-components';
import { useFeatures } from '@hazmapper/hooks';

const getFilename = (projectName: string) => {
// Convert to lowercase filename based on projectName
const sanitizedString = projectName.toLowerCase().replace(/[^a-z0-9]/g, '_');
return `${sanitizedString}.json`;
};

interface DownloadFeaturesButtonProps {
projectId: number;
isPublic: boolean;
project: Project;
}

const DownloadFeaturesButton: React.FC<DownloadFeaturesButtonProps> = ({
projectId,
isPublic,
project,
}) => {
const { isLoading: isDownloading, refetch: triggerDownload } = useFeatures({
projectId,
isPublic,
projectId: project.id,
isPublicView: project.public,
assetTypes: [], // Empty array to get all features
options: {
enabled: false, // Only fetch when triggered by user clicking button
Expand All @@ -31,7 +35,7 @@ const DownloadFeaturesButton: React.FC<DownloadFeaturesButtonProps> = ({
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `hazmapper.json`;
link.download = getFilename(project.name);

document.body.appendChild(link);
link.click();
Expand Down Expand Up @@ -61,9 +65,9 @@ interface Props {
isPublicView: boolean;

/**
* active project id
* active project
*/
projectId: number;
project: Project;
}

/**
Expand All @@ -72,7 +76,7 @@ interface Props {
const AssetsPanel: React.FC<Props> = ({
isPublicView,
featureCollection,
projectId,
project,
}) => {
return (
<div className={styles.root}>
Expand All @@ -87,7 +91,7 @@ const AssetsPanel: React.FC<Props> = ({
/>
</div>
<div className={styles.bottomSection}>
<DownloadFeaturesButton projectId={projectId} isPublic={isPublic} />
<DownloadFeaturesButton project={project} />
</div>
</div>
);
Expand Down

0 comments on commit f2dffb7

Please sign in to comment.