Skip to content

Commit 0c7f118

Browse files
committed
add withFlash() context HOC
1 parent b92c970 commit 0c7f118

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

resources/scripts/components/server/files/RenameFileModal.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ import tw from 'twin.macro';
99
import Button from '@/components/elements/Button';
1010
import useServer from '@/plugins/useServer';
1111
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
12-
import useFlash from '@/plugins/useFlash';
12+
import withFlash, { WithFlashProps } from '@/hoc/withFlash';
1313

1414
interface FormikValues {
1515
name: string;
1616
}
1717

18-
type Props = RequiredModalProps & { files: string[]; useMoveTerminology?: boolean };
18+
type OwnProps = WithFlashProps & RequiredModalProps & { files: string[]; useMoveTerminology?: boolean };
1919

20-
export default ({ files, useMoveTerminology, ...props }: Props) => {
20+
const RenameFileModal = ({ flash, files, useMoveTerminology, ...props }: OwnProps) => {
2121
const { uuid } = useServer();
2222
const { mutate } = useFileManagerSwr();
23-
const { clearFlashes, clearAndAddHttpError } = useFlash();
2423
const directory = ServerContext.useStoreState(state => state.files.directory);
2524
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
2625

2726
const submit = ({ name }: FormikValues, { setSubmitting }: FormikHelpers<FormikValues>) => {
28-
clearFlashes('files');
27+
flash.clearFlashes('files');
2928

3029
const len = name.split('/').length;
3130
if (files.length === 1) {
@@ -51,7 +50,7 @@ export default ({ files, useMoveTerminology, ...props }: Props) => {
5150
.catch(error => {
5251
mutate();
5352
setSubmitting(false);
54-
clearAndAddHttpError({ key: 'files', error });
53+
flash.clearAndAddHttpError({ key: 'files', error });
5554
})
5655
.then(() => props.onDismissed());
5756
};
@@ -96,3 +95,5 @@ export default ({ files, useMoveTerminology, ...props }: Props) => {
9695
</Formik>
9796
);
9897
};
98+
99+
export default withFlash(RenameFileModal);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
import useFlash from '@/plugins/useFlash';
3+
import { Actions } from 'easy-peasy';
4+
import { ApplicationStore } from '@/state';
5+
6+
export interface WithFlashProps {
7+
flash: Actions<ApplicationStore>['flashes'];
8+
}
9+
10+
function withFlash<TOwnProps> (Component: React.ComponentType<TOwnProps & WithFlashProps>): React.ComponentType<TOwnProps> {
11+
return (props: TOwnProps) => {
12+
const { addError, addFlash, clearFlashes, clearAndAddHttpError } = useFlash();
13+
14+
return (
15+
<Component
16+
flash={{ addError, addFlash, clearFlashes, clearAndAddHttpError }}
17+
{...props}
18+
/>
19+
);
20+
};
21+
}
22+
23+
export default withFlash;

0 commit comments

Comments
 (0)