Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#657 Usuwanie instancji listu z modalem potwierdzającym #749

Merged
merged 3 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions frontend-project/src/components/Modals/Confirmation/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React, { useEffect, useState } from 'react';
import { Modal, Button } from 'antd';
import { localeKeys } from '@/locales/pl-PL';
import { formatMessage } from 'umi-plugin-react/locale';

type ConfirmationProps<T> = {
title?: string;
onSuccess?: () => void;
onFailure?: (args: T) => void;
};

type ConfirmationModalProps<T> = ConfirmationProps<T> & {
title: string;
submitArgs: T;
hideModal: () => void;
onSubmit: () => void;
};

const ConfirmationModal = <T extends {}>({
onSubmit,
title,
onSuccess,
onFailure,
hideModal,
submitArgs,
}: ConfirmationModalProps<T>) => {
const [isModalVisible, setIsModalVisible] = useState(true);
const [isInProgress, setIsInProgress] = useState(false);

useEffect(() => {
if (!isModalVisible) {
hideModal();
}
}, [isModalVisible]);

const handleOk = async () => {
setIsInProgress(true);
try {
await onSubmit();
setIsModalVisible(false);
onSuccess();
} catch (error) {
if (onFailure) {
onFailure(submitArgs);
}
} finally {
setIsInProgress(false);
}
};

const handleCancel = () => {
setIsModalVisible(false);
};

return (
<>
<Modal
title={title}
visible={isModalVisible}
onOk={handleOk}
onCancel={handleCancel}
footer={[
<Button key="confirmation-button-cancel" disabled={isInProgress} onClick={handleCancel}>
{formatMessage({ id: localeKeys.modal.cancel })}
</Button>,
<Button
key="confirmation-button-confirm"
type="primary"
disabled={isInProgress}
loading={isInProgress}
onClick={handleOk}
>
{formatMessage({ id: localeKeys.modal.confirm })}
</Button>,
]}
>
<p>{formatMessage({ id: localeKeys.modal.description })}</p>
</Modal>
</>
);
};

export const useConfirmationModal = <T extends {}>(
props: ConfirmationProps<T>,
onSubmit: (args: T) => void,
) => {
const [modal, setModal] = useState(null);

const hideModal = () => setModal(null);

const showModal = (args: T, title?: string) => {
setModal(
<ConfirmationModal
{...props}
{...(title ? { title } : undefined)}
submitArgs={args}
onSubmit={() => onSubmit(args)}
hideModal={hideModal}
/>,
);
};

return [modal, showModal];
};

export default ConfirmationModal;
2 changes: 1 addition & 1 deletion frontend-project/src/locales/en-US.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tagsLocale } from '@/pages/tags/locales/en-US';
import component from './en-US/component';
import globalHeader from './en-US/globalHeader';
import { menuLocale } from './en-US/menu';
Expand All @@ -8,7 +9,6 @@ import BaseLocales from './pl-PL';
import { structuredLocale } from '../utils/structedLocale';
import { casesLocale } from '../pages/cases/locales/en-US';
import { globalsLocale } from './en-US/globals';
import { tagsLocale } from '@/pages/tags/locales/en-US';

const [labels] = structuredLocale({
...menuLocale,
Expand Down
7 changes: 6 additions & 1 deletion frontend-project/src/locales/en-US/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const globalsLocale = {
error: 'Błąd',
error: 'Error',
lists: {
edit: 'Edit',
delete: 'Delete',
Expand All @@ -12,4 +12,9 @@ export const globalsLocale = {
save: 'Save',
reset: 'Reset',
},
modal: {
confirm: 'Confirm',
cancel: 'Cancel',
description: 'Are you sure you want to proceed?',
},
};
2 changes: 1 addition & 1 deletion frontend-project/src/locales/pl-PL.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tagsLocale } from '@/pages/tags/locales/pl-PL';
import component from './pl-PL/component';
import globalHeader from './pl-PL/globalHeader';
import pwa from './pl-PL/pwa';
Expand All @@ -7,7 +8,6 @@ import { menuLocale } from './pl-PL/menu';
import { structuredLocale } from '../utils/structedLocale';
import { casesLocale } from '../pages/cases/locales/pl-PL';
import { globalsLocale } from './pl-PL/globals';
import { tagsLocale } from '@/pages/tags/locales/pl-PL';

const [labels, keys] = structuredLocale({
...menuLocale,
Expand Down
5 changes: 5 additions & 0 deletions frontend-project/src/locales/pl-PL/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ export const globalsLocale = {
save: 'Zapisz',
reset: 'Wyczyść',
},
modal: {
confirm: 'Potwierdź',
cancel: 'Anuluj',
description: 'Czy chcesz kontynuować?',
},
};
8 changes: 8 additions & 0 deletions frontend-project/src/models/letters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Letter } from '@/services/definitions';
import { LettersService } from '@/services/letters';
import { ReadWriteReduxResource } from '@/utils/reduxModel';

export default ReadWriteReduxResource<Letter>({
namespace: 'letters',
service: LettersService,
});
2 changes: 1 addition & 1 deletion frontend-project/src/pages/cases/CasesDetailView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
import { Institution, User, Tag, Case, Feature } from '@/services/definitions';
import { ReduxResourceState } from '@/utils/reduxModel';
import router from 'umi/router';
import { localeKeys } from '../../locales/pl-PL';
import { RouterTypes } from 'umi';
import { ServiceResponse } from '@/services/service';
import smallEodSDK from '@/utils/sdk';
import { openNotificationWithIcon } from '@/models/global';
import { localeKeys } from '../../locales/pl-PL';

interface CasesDetailViewProps {
cases: ReduxResourceState<Case>;
Expand Down
92 changes: 88 additions & 4 deletions frontend-project/src/pages/letters/list/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
import { ProColumns } from '@ant-design/pro-table';
import React, { FC } from 'react';
import { ActionType, ProColumns } from '@ant-design/pro-table';
import React, { FC, useRef } from 'react';
import { formatMessage } from 'umi-plugin-react/locale';

import { fetchLettersPage } from '@/services/letters';
import { Letter } from '@/services/definitions';
import Table from '@/components/Table';
import ChannelName from '@/components/Table/ChannelName';
import InstitutionName from '@/components/Table/InstitutionName';
import CaseName from '@/components/Table/CaseName';
import DocumentTypeName from '@/components/Table/DocumentTypeName';
import { Button, Space, Tooltip } from 'antd';
import { DeleteOutlined } from '@ant-design/icons';
import { useDispatch } from 'dva';
import { openNotificationWithIcon } from '@/models/global';
import { ServiceResponse } from '@/services/service';
import { PaginationParams, PaginationResponse } from '@/services/common';
import { LettersService } from '@/services/letters';
import { localeKeys } from '@/locales/pl-PL';
import { useConfirmationModal } from '@/components/Modals/Confirmation';

const TableList: FC<{}> = () => {
const dispatch = useDispatch();
const tableActionRef = useRef<ActionType>();

function onRemove(id: number): Promise<void> {
return new Promise((resolve, reject) => {
dispatch({
type: 'letters/remove',
payload: {
id,
onResponse: (response: ServiceResponse<number>) =>
response.status === 'failed' ? reject() : resolve(),
},
});
});
}

const [modal, showModal] = useConfirmationModal(
{
onSuccess: () => tableActionRef.current.reload(),
onFailure: id =>
openNotificationWithIcon(
'error',
formatMessage({ id: localeKeys.error }),
`${formatMessage({ id: 'letters-list.table.notification.remove' })} ${id}`,
),
},
onRemove,
);

async function fetchPage(props: PaginationParams): Promise<PaginationResponse<Letter>> {
const response = await LettersService.fetchPage(props);
if (response.status === 'failed') {
openNotificationWithIcon(
'error',
formatMessage({ id: localeKeys.error }),
formatMessage({ id: localeKeys.lists.failedDownload }),
);
return { data: [], total: 0 };
}
return response.data;
}

const columns: ProColumns<Letter>[] = [
{
title: formatMessage({ id: 'letters-list.table.columns.documentType.title' }),
Expand Down Expand Up @@ -70,9 +120,43 @@ const TableList: FC<{}> = () => {
dataIndex: 'attachments',
render: (attachments: []) => attachments.length,
},
{
title: formatMessage({ id: localeKeys.lists.actions }),
dataIndex: 'id',
render: (_, { id, referenceNumber }: Letter) => (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W projekcie jest gdzieś lodash, proponuję jakąś inną nazwę zmiennej zamiast _. Jakąkolwiek. Np. x.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Underscore jest ignorowany przez prettiera, przez co nie rzuca błędów unused variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dokładnie tak. To dość powszechna konwencja dla nazwania nie używanych parametrów funkcji, nawet była gdzieś użyta w projekcie ;)

<Space>
<Tooltip title={formatMessage({ id: localeKeys.lists.delete })}>
<Button
type="default"
danger
shape="circle"
icon={<DeleteOutlined />}
onClick={() =>
showModal(
id,
formatMessage(
{
id: 'letters-list.table.modal.remove.title',
},
{
referenceNumber: referenceNumber || '',
},
),
)
}
/>
</Tooltip>
</Space>
),
},
];

return <Table type="letters" columns={columns} fetchData={fetchLettersPage} />;
return (
<>
<Table type="letters" columns={columns} actionRef={tableActionRef} fetchData={fetchPage} />
{modal}
</>
);
};

export default TableList;
2 changes: 2 additions & 0 deletions frontend-project/src/pages/letters/list/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export default {
'letters-list.table.total': 'Total',
'letters-list.table.direction.in': 'received',
'letters-list.table.direction.out': 'sent',
'letters-list.table.notification.remove': 'Cannot delete list',
'letters-list.table.modal.remove.title': 'Do you want to delete {referenceNumber} letter?',
};
2 changes: 2 additions & 0 deletions frontend-project/src/pages/letters/list/locales/pl-PL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export default {
'letters-list.table.total': 'Łącznie',
'letters-list.table.direction.in': 'odebrane',
'letters-list.table.direction.out': 'wysłane',
'letters-list.table.notification.remove': 'Nie udało się usuąć listu',
'letters-list.table.modal.remove.title': 'Czy chcesz usunąć list {referenceNumber}?',
};
2 changes: 1 addition & 1 deletion frontend-project/src/pages/tags/TagsDetailView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
import { Tag } from '@/services/definitions';
import { ReduxResourceState } from '@/utils/reduxModel';
import router from 'umi/router';
import { localeKeys } from '../../locales/pl-PL';
import { RouterTypes } from 'umi';
import { ServiceResponse } from '@/services/service';
import smallEodSDK from '@/utils/sdk';
import { openNotificationWithIcon } from '@/models/global';
import { localeKeys } from '../../locales/pl-PL';

interface TagsDetailViewProps {
tags: ReduxResourceState<Tag>;
Expand Down
Loading