-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: elevate download with donation to component level
- Loading branch information
Michael
committed
Sep 27, 2024
1 parent
c6c37cb
commit 4496a19
Showing
8 changed files
with
223 additions
and
287 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
96 changes: 87 additions & 9 deletions
96
packages/components/src/DownloadFileFromLink/DownloadFileFromLink.test.tsx
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,18 +1,96 @@ | ||
import '@testing-library/jest-dom/vitest' | ||
|
||
import { describe, expect, it } from 'vitest' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import { render } from '../test/utils' | ||
import { Default } from './DownloadFileFromLink.stories' | ||
import { DownloadFileFromLink } from './DownloadFileFromLink' | ||
|
||
import type { DownloadFileFromLinkProps } from './DownloadFileFromLink' | ||
import type { IUploadedFileMeta } from './types' | ||
|
||
describe('DownloadFiles', () => { | ||
it('validates the component behaviour', () => { | ||
const { getByText } = render( | ||
<Default {...(Default.args as DownloadFileFromLinkProps)} />, | ||
const mockedUsedNavigate = vi.fn() | ||
vi.mock('react-router-dom', () => ({ | ||
useNavigate: () => mockedUsedNavigate, | ||
})) | ||
|
||
vi.mock('src/common/hooks/useCommonStores', () => ({ | ||
__esModule: true, | ||
useCommonStores: vi.fn(), | ||
})) | ||
|
||
describe('DownloadFileFromLink', () => { | ||
it('when logged out, requires users to login', () => { | ||
const { getAllByTestId } = render( | ||
<DownloadFileFromLink | ||
handleClick={vi.fn()} | ||
isLoggedIn={false} | ||
fileDownloadCount={0} | ||
fileLink="http://youtube.com/" | ||
files={[]} | ||
themeStoreDonationProps={{}} | ||
/>, | ||
) | ||
|
||
expect(getByText('Download files')).toBeInTheDocument() | ||
const downloadButton = getAllByTestId('downloadButton')[0] | ||
fireEvent.click(downloadButton) | ||
|
||
expect(mockedUsedNavigate).toHaveBeenCalledWith('/sign-in') | ||
}) | ||
|
||
it('when logged in, opens the donation modal for fileLink', () => { | ||
const handleClick = vi.fn() | ||
const fileLink = 'http://youtube.com/' | ||
|
||
const { getAllByTestId } = render( | ||
<DownloadFileFromLink | ||
handleClick={handleClick} | ||
isLoggedIn={true} | ||
fileDownloadCount={0} | ||
fileLink={fileLink} | ||
files={[]} | ||
themeStoreDonationProps={{}} | ||
/>, | ||
) | ||
|
||
const downloadButton = getAllByTestId('downloadButton')[0] | ||
fireEvent.click(downloadButton) | ||
|
||
expect(getAllByTestId('DonationRequestSkip')[0]).toHaveAttribute( | ||
'href', | ||
fileLink, | ||
) | ||
expect(getAllByTestId('DonationRequest')[0]).toBeInTheDocument() | ||
expect(handleClick).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('when logged in, opens the donation modal for files', () => { | ||
const downloadUrl = 'http://great-url.com/' | ||
const handleClick = vi.fn() | ||
|
||
const { getAllByTestId } = render( | ||
<DownloadFileFromLink | ||
handleClick={handleClick} | ||
isLoggedIn={true} | ||
fileDownloadCount={0} | ||
fileLink={undefined} | ||
files={[ | ||
{ | ||
downloadUrl, | ||
name: 'first-file', | ||
size: 435435, | ||
} as IUploadedFileMeta, | ||
]} | ||
themeStoreDonationProps={{}} | ||
/>, | ||
) | ||
|
||
const downloadButton = getAllByTestId('downloadButton')[0] | ||
fireEvent.click(downloadButton) | ||
|
||
expect(getAllByTestId('DonationRequestSkip')[0]).toHaveAttribute( | ||
'href', | ||
downloadUrl, | ||
) | ||
expect(getAllByTestId('DonationRequest')[0]).toBeInTheDocument() | ||
expect(handleClick).not.toHaveBeenCalled() | ||
}) | ||
}) |
93 changes: 78 additions & 15 deletions
93
packages/components/src/DownloadFileFromLink/DownloadFileFromLink.tsx
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,28 +1,91 @@ | ||
import { useState } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
|
||
import { DonationRequestModal } from '../DonationRequestModal/DonationRequestModal' | ||
import { DownloadButton } from '../DownloadButton/DownloadButton' | ||
import { ExternalLink } from '../ExternalLink/ExternalLink' | ||
import { DownloadCounter } from '../DownloadCounter/DownloadCounter' | ||
import { DownloadStaticFile } from '../DownloadStaticFile/DownloadStaticFile' | ||
|
||
import type { IThemeStoreDonationProps, IUploadedFileMeta } from './types' | ||
|
||
export interface DownloadFileFromLinkProps { | ||
link: string | ||
handleClick?: () => Promise<void> | ||
redirectToSignIn?: () => Promise<void> | ||
export interface IProps { | ||
handleClick: () => Promise<void> | ||
isLoggedIn: boolean | ||
fileDownloadCount: number | ||
fileLink: string | undefined | ||
files: (IUploadedFileMeta | File | null)[] | undefined | ||
themeStoreDonationProps: IThemeStoreDonationProps | undefined | ||
} | ||
|
||
export const DownloadFileFromLink = (props: DownloadFileFromLinkProps) => { | ||
export const DownloadFileFromLink = (props: IProps) => { | ||
const { | ||
handleClick, | ||
fileDownloadCount, | ||
fileLink, | ||
files, | ||
isLoggedIn, | ||
themeStoreDonationProps, | ||
} = props | ||
const [isModalOpen, setIsModalOpen] = useState<boolean>(false) | ||
const [link, setLink] = useState<string>('') | ||
const navigate = useNavigate() | ||
|
||
const toggleIsModalOpen = () => setIsModalOpen(!isModalOpen) | ||
|
||
const callback = () => { | ||
handleClick() | ||
toggleIsModalOpen() | ||
} | ||
|
||
const filteredFiles: IUploadedFileMeta[] | undefined = files?.filter( | ||
(file): file is IUploadedFileMeta => file !== null && 'downloadUrl' in file, | ||
) | ||
|
||
return ( | ||
<> | ||
{!props.redirectToSignIn ? ( | ||
<ExternalLink | ||
href={props.link} | ||
onClick={() => props.handleClick && props.handleClick()} | ||
> | ||
<DownloadButton isLoggedIn onClick={() => {}} /> | ||
</ExternalLink> | ||
) : ( | ||
<DonationRequestModal | ||
body={themeStoreDonationProps?.body} | ||
callback={callback} | ||
iframeSrc={themeStoreDonationProps?.iframeSrc} | ||
imageURL={themeStoreDonationProps?.imageURL} | ||
isOpen={isModalOpen} | ||
link={link} | ||
onDidDismiss={() => toggleIsModalOpen()} | ||
/> | ||
|
||
{!isLoggedIn && ( | ||
<DownloadButton | ||
onClick={() => props.handleClick && props.handleClick()} | ||
onClick={() => navigate('/sign-in')} | ||
isLoggedIn={false} | ||
/> | ||
)} | ||
{isLoggedIn && ( | ||
<> | ||
{fileLink && ( | ||
<DownloadButton | ||
onClick={() => { | ||
setLink(fileLink) | ||
toggleIsModalOpen() | ||
}} | ||
isLoggedIn | ||
/> | ||
)} | ||
{filteredFiles && | ||
filteredFiles.map((file, index) => ( | ||
<DownloadStaticFile | ||
file={file} | ||
key={file ? file.name : `file-${index}`} | ||
handleClick={() => { | ||
setLink(file.downloadUrl) | ||
toggleIsModalOpen() | ||
}} | ||
forDonationRequest | ||
isLoggedIn | ||
/> | ||
))} | ||
</> | ||
)} | ||
<DownloadCounter total={fileDownloadCount} /> | ||
</> | ||
) | ||
} |
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,16 @@ | ||
export interface IUploadedFileMeta { | ||
downloadUrl: string | ||
contentType?: string | null | ||
fullPath: string | ||
name: string | ||
type: string | ||
size: number | ||
timeCreated: string | ||
updated: string | ||
} | ||
|
||
export interface IThemeStoreDonationProps { | ||
body?: string | ||
iframeSrc?: string | ||
imageURL?: string | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.