Skip to content

Commit

Permalink
Merge pull request #66 from Opetushallitus/OK-746-esiparannuksia-refa…
Browse files Browse the repository at this point in the history
…ktorointeja

OK-746: Esiparannuksia ja refaktorointeja
  • Loading branch information
pretseli authored Jan 22, 2025
2 parents d0ead8d + 03c4781 commit 3e502f5
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 315 deletions.
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"@emotion/cache": "^11.14.0",
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@mui/icons-material": "^6.2.1",
"@mui/material": "^6.2.1",
"@mui/material-nextjs": "^6.2.1",
"@mui/icons-material": "^6.4.0",
"@mui/material": "^6.4.0",
"@mui/material-nextjs": "^6.3.1",
"@opetushallitus/oph-design-system": "github:opetushallitus/oph-design-system#v0.1.8",
"@tanstack/react-query": "^5.62.8",
"@tanstack/react-query-devtools": "^5.62.8",
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/action-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const Button = withDefaultProps(
},
})),
{ variant: 'text' },
);
) as typeof OphButton;

export const Container = styled(Box)(({ theme }) => ({
display: 'flex',
Expand Down
31 changes: 0 additions & 31 deletions src/app/components/download-button.tsx

This file was deleted.

50 changes: 50 additions & 0 deletions src/app/components/file-download-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { FileResult } from '@/app/lib/http-client';
import useToaster from '@/app/hooks/useToaster';
import { useFileDownloadMutation } from '../hooks/useFileDownloadMutation';
import { FileDownloadOutlined } from '@mui/icons-material';
import { OphButton } from '@opetushallitus/oph-design-system';
import { ButtonProps } from '@mui/material';

type FileDownloadProps = {
component?: typeof OphButton;
icon?: React.ReactNode;
getFile: () => Promise<FileResult>;
children: React.ReactNode;
defaultFileName: string;
errorKey: string;
errorMessage: string;
} & Omit<ButtonProps, 'onClick' | 'loading'>;

export function FileDownloadButton({
defaultFileName,
children,
getFile,
errorKey,
errorMessage,
startIcon,
...props
}: FileDownloadProps) {
const { addToast } = useToaster();
const mutation = useFileDownloadMutation({
onError: () => {
addToast({
key: errorKey,
message: errorMessage,
type: 'error',
});
},
getFile,
defaultFileName,
});

return (
<OphButton
startIcon={startIcon ?? <FileDownloadOutlined />}
loading={mutation.isPending}
onClick={() => mutation.mutate()}
{...props}
>
{children}
</OphButton>
);
}
1 change: 1 addition & 0 deletions src/app/components/localized-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const LocalizedSelect = (
return (
<OphSelect
inputProps={{ 'aria-label': t('yleinen.valitsevaihtoehto') }}
MenuProps={{ id: 'select-menu' }}
placeholder={t('yleinen.valitse')}
{...props}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,10 @@ import {
NoteOutlined,
} from '@mui/icons-material';
import { ActionBar } from '@/app/components/action-bar';
import { useMutation } from '@tanstack/react-query';
import useToaster from '@/app/hooks/useToaster';
import { DownloadButton } from '@/app/components/download-button';
import { getOsoitetarratHakemuksille } from '@/app/lib/valintalaskentakoostepalvelu';
import { downloadBlob } from '@/app/lib/common';
import { HarkinnanvaraisetTilatByHakemusOids } from '@/app/lib/types/harkinnanvaraiset-types';

const useOsoitetarratMutation = ({ selection }: { selection: Set<string> }) => {
const { addToast } = useToaster();

return useMutation({
mutationFn: async () => {
const { fileName, blob } = await getOsoitetarratHakemuksille({
tag: 'harkinnanvaraiset',
hakemusOids: Array.from(selection),
});
downloadBlob(fileName ?? 'osoitetarrat.pdf', blob);
},
onError: (e) => {
addToast({
key: 'get-osoitetarrat',
message: 'harkinnanvaraiset.virhe-osoitetarrat',
type: 'error',
});
console.error(e);
},
});
};
import { FileDownloadButton } from '@/app/components/file-download-button';
import { useCallback } from 'react';

const HyvaksyValitutButton = ({
selection,
Expand Down Expand Up @@ -71,19 +47,27 @@ const OsoitetarratDownloadButton = ({
}) => {
const { t } = useTranslation();

const osoitetarratMutation = useOsoitetarratMutation({
selection,
});
const getFile = useCallback(
() =>
getOsoitetarratHakemuksille({
tag: 'harkinnanvaraiset',
hakemusOids: Array.from(selection),
}),
[selection],
);

return (
<DownloadButton
Component={ActionBar.Button}
<FileDownloadButton
component={ActionBar.Button}
disabled={selection.size === 0}
mutation={osoitetarratMutation}
startIcon={<NoteOutlined />}
defaultFileName="osoitetarrat.pdf"
getFile={getFile}
errorKey="get-osoitetarrat-error"
errorMessage="harkinnanvaraiset.virhe-osoitetarrat"
>
{t('harkinnanvaraiset.muodosta-osoitetarrat')}
</DownloadButton>
</FileDownloadButton>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useTranslations } from '@/app/hooks/useTranslations';
import { CircularProgress, Stack } from '@mui/material';
import { OphButton } from '@opetushallitus/oph-design-system';
import { ExcelUploadButton } from './pistesyotto-excel-upload-button';
import { ExcelDownloadButton } from './pistesyotto-excel-download-button';
import { PistesyottoExcelUploadButton } from './pistesyotto-excel-upload-button';
import { PistesyottoExcelDownloadButton } from './pistesyotto-excel-download-button';

export const PisteSyottoActions = ({
hakuOid,
Expand All @@ -29,8 +29,8 @@ export const PisteSyottoActions = ({
{isUpdating && (
<CircularProgress aria-label={t('valinnanhallinta.lasketaan')} />
)}
<ExcelDownloadButton hakuOid={hakuOid} hakukohdeOid={hakukohdeOid} />
<ExcelUploadButton hakuOid={hakuOid} hakukohdeOid={hakukohdeOid} />
<PistesyottoExcelDownloadButton hakuOid={hakuOid} hakukohdeOid={hakukohdeOid} />
<PistesyottoExcelUploadButton hakuOid={hakuOid} hakukohdeOid={hakukohdeOid} />
</Stack>
);
};
Loading

0 comments on commit 3e502f5

Please sign in to comment.