-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- allow for previewing of files - merged the multiple places where uploaded files were being viewed in a single component that can list/download/preview/delete uploaded files
- Loading branch information
Showing
8 changed files
with
276 additions
and
168 deletions.
There are no files selected for viewing
77 changes: 0 additions & 77 deletions
77
src/components/admin/campaign-news/UploadedCampaignFile.tsx
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/components/admin/campaign-news/UploadedCampaignNewsArticleFile.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { AxiosError, AxiosResponse } from 'axios' | ||
import { useTranslation } from 'next-i18next' | ||
|
||
import { endpoints } from 'service/apiEndpoints' | ||
import { ApiErrors } from 'service/apiErrors' | ||
|
||
import { UploadedFilesList } from 'components/common/file-upload/UploadedFilesList' | ||
import { CampaignNewsFile } from 'gql/campaign-news' | ||
import { Session } from 'next-auth' | ||
import { useSession } from 'next-auth/react' | ||
import { deleteCampaignNewsFile, downloadCampaignNewsFile } from 'service/campaign-news' | ||
import { AlertStore } from 'stores/AlertStore' | ||
|
||
type Props = { | ||
articleId: string | ||
files: CampaignNewsFile[] | ||
} | ||
|
||
export default function UploadedCampaignNewsArticleFiles({ files, articleId }: Props) { | ||
const { t } = useTranslation('campaigns') | ||
const queryClient = useQueryClient() | ||
const { data: session } = useSession() | ||
|
||
const mutation = useMutation< | ||
AxiosResponse<CampaignNewsFile>, | ||
AxiosError<ApiErrors>, | ||
[string, Session | null] | ||
>({ | ||
mutationFn: ([id, session]) => deleteCampaignNewsFile(id, session), | ||
onError: () => AlertStore.show(t('alerts.error'), 'error'), | ||
onSuccess: () => { | ||
AlertStore.show(t('alerts.deletedFile'), 'success') | ||
queryClient.invalidateQueries([endpoints.campaignNews.viewNewsArticleById(articleId).url]) | ||
}, | ||
}) | ||
|
||
return ( | ||
<UploadedFilesList | ||
files={files} | ||
downloadQuery={(f) => downloadCampaignNewsFile(f.id, session).then((r) => r.data)} | ||
deleteMutation={(f) => mutation.mutateAsync([f.id, session])} | ||
title={t('campaigns:cta.attached-files')} | ||
downloadText={t('cta.download')} | ||
/> | ||
) | ||
} |
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
82 changes: 22 additions & 60 deletions
82
src/components/admin/campaigns/grid/UploadedCampaignFile.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,85 +1,47 @@ | ||
import { useTranslation } from 'next-i18next' | ||
import { AxiosError, AxiosResponse } from 'axios' | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { AxiosError, AxiosResponse } from 'axios' | ||
import { useTranslation } from 'next-i18next' | ||
|
||
import FilePresentIcon from '@mui/icons-material/FilePresent' | ||
import { | ||
Avatar, | ||
Button, | ||
IconButton, | ||
ListItem, | ||
ListItemAvatar, | ||
ListItemText, | ||
Tooltip, | ||
} from '@mui/material' | ||
|
||
import { ApiErrors } from 'service/apiErrors' | ||
import { endpoints } from 'service/apiEndpoints' | ||
import { ApiErrors } from 'service/apiErrors' | ||
|
||
import { AlertStore } from 'stores/AlertStore' | ||
import { useSession } from 'next-auth/react' | ||
import { UploadedFilesList } from 'components/common/file-upload/UploadedFilesList' | ||
import { CampaignFile } from 'gql/campaigns' | ||
import { useSession } from 'next-auth/react' | ||
import { deleteCampaignFile, downloadCampaignFile } from 'service/campaign' | ||
import { Delete } from '@mui/icons-material' | ||
import { AlertStore } from 'stores/AlertStore' | ||
import { Session } from 'next-auth' | ||
|
||
type Props = { | ||
campaignId: string | ||
file: CampaignFile | ||
files: CampaignFile[] | ||
} | ||
|
||
export default function UploadedCampaignFile({ file, campaignId }: Props) { | ||
export default function UploadedCampaignFiles({ files, campaignId }: Props) { | ||
const { t } = useTranslation('campaigns') | ||
const queryClient = useQueryClient() | ||
const { data: session } = useSession() | ||
|
||
const mutation = useMutation<AxiosResponse<CampaignFile>, AxiosError<ApiErrors>>({ | ||
mutationFn: deleteCampaignFile(file.id), | ||
const mutation = useMutation< | ||
AxiosResponse<CampaignFile>, | ||
AxiosError<ApiErrors>, | ||
[string, Session | null] | ||
>({ | ||
mutationFn: deleteCampaignFile, | ||
onError: () => AlertStore.show(t('alerts.error'), 'error'), | ||
onSuccess: () => { | ||
AlertStore.show(t('alerts.deletedFile'), 'success') | ||
queryClient.invalidateQueries([endpoints.campaign.viewCampaignById(campaignId).url]) | ||
}, | ||
}) | ||
const downloadFileHandler = async () => { | ||
downloadCampaignFile(file.id, session) | ||
.then((response) => { | ||
const url = window.URL.createObjectURL(new Blob([response.data])) | ||
const link = document.createElement('a') | ||
link.href = url | ||
link.setAttribute('download', `${file.filename}`) | ||
link.click() | ||
}) | ||
.catch((error) => console.log(error)) | ||
} | ||
|
||
const deleteFileHandler = () => { | ||
mutation.mutate() | ||
} | ||
|
||
// in a preview file handler function we would (much like the downloadFileHandler function above) | ||
// downloadCampaignFile(file.id, session) | ||
// .then((response) => { | ||
// const url = window.URL.createObjectURL(new Blob([response.data], type: {file.mimetype})) | ||
const blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'], { type: 'text/html' }) | ||
const newurl = window.URL.createObjectURL(blob) | ||
|
||
return ( | ||
<ListItem key={file.id}> | ||
<ListItemAvatar> | ||
<Avatar> | ||
<FilePresentIcon /> | ||
</Avatar> | ||
</ListItemAvatar> | ||
<ListItemText primary={file.filename} /> | ||
<ListItemText primary={file.role} sx={{ textAlign: 'right', pr: 'inherit' }} /> | ||
<></> | ||
<Tooltip title={'download'}> | ||
<Button onClick={downloadFileHandler}>{t('cta.download')}</Button> | ||
</Tooltip> | ||
<IconButton edge="end" aria-label="delete" onClick={deleteFileHandler}> | ||
<Delete /> | ||
</IconButton> | ||
<iframe id={file.id} src={newurl} /> | ||
</ListItem> | ||
<UploadedFilesList | ||
files={files} | ||
downloadQuery={(f) => downloadCampaignFile(f.id, session).then((r) => r.data)} | ||
deleteMutation={(f) => mutation.mutateAsync([f.id, session])} | ||
title={t('campaigns:cta.attached-files')} | ||
downloadText={t('cta.download')} | ||
/> | ||
) | ||
} |
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
Oops, something went wrong.