Skip to content

Commit

Permalink
Merge pull request #2 from watchdogpolska/dev
Browse files Browse the repository at this point in the history
tworzenie, edytowanie i usuwanie tagów - close watchdogpolska#632 watchdogpolska#631 watchdogpolska#629 (watchdogpolska#716)
  • Loading branch information
MichalKarol authored Dec 28, 2020
2 parents b2ac659 + 4f92650 commit dc4dd87
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 315 deletions.
10 changes: 8 additions & 2 deletions frontend-project/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,19 @@ export default {
name: 'new',
icon: 'FileAddOutlined',
path: '/tags/new',
component: './tags/new',
component: './tags/TagsDetailView',
},
{
name: 'list',
icon: 'HomeOutlined',
path: '/tags/list',
component: './tags/list',
component: './tags/TagsListView',
},
{
name: 'edit',
path: '/tags/edit/:id',
component: './tags/TagsDetailView',
hideInMenu: true,
},
],
},
Expand Down
2 changes: 2 additions & 0 deletions frontend-project/src/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ 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,
...globalsLocale,
...casesLocale,
...tagsLocale,
});

const Locale = {
Expand Down
2 changes: 2 additions & 0 deletions frontend-project/src/locales/pl-PL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ 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,
...globalsLocale,
...casesLocale,
...tagsLocale,
});
export const localeKeys = keys;

Expand Down
5 changes: 2 additions & 3 deletions frontend-project/src/models/global.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { notification } from 'antd';
import { ReactNode } from 'react';
import { IconType } from 'antd/lib/notification';

export interface GlobalModelState {
collapsed: boolean;
}

export const openNotificationWithIcon = (message: IconType, description: ReactNode) => {
notification[message]({ message, description });
export const openNotificationWithIcon = (message: IconType, title: string, description: string) => {
notification[message]({ message: title, description });
};

const GlobalModel = {
Expand Down
59 changes: 0 additions & 59 deletions frontend-project/src/models/tag.ts

This file was deleted.

81 changes: 5 additions & 76 deletions frontend-project/src/models/tags.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,8 @@
import { fetchAll, fetchPage, create } from '@/services/tags';
import { Effect } from 'dva';
import { Reducer } from 'redux';
import { router } from 'umi';
import { TagsService } from '@/services/tags';
import { Tag } from '@/services/definitions';
import { openNotificationWithIcon } from '@/models/global';
import { formatMessage } from 'umi-plugin-react/locale';
import { ReadWriteReduxResource } from '@/utils/reduxModel';

export type TagDefaultState = Tag[];

export interface TagModelType {
namespace: string;
state: Tag[];
effects: {
create: Effect;
fetchAll: Effect;
fetchPage: Effect;
};
reducers: {
saveAll: Reducer<TagDefaultState>;
savePage: Reducer<TagDefaultState>;
};
}
const defaultTagsState: TagDefaultState = [];

const TagsModel: TagModelType = {
export default ReadWriteReduxResource<Tag>({
namespace: 'tags',
state: defaultTagsState,
effects: {
*create({ payload }, { call }) {
try {
const response = yield call(create, payload);
openNotificationWithIcon(
'success',
formatMessage({ id: 'tags-new.page-create-notiffy-success' }) + response.data.id,
);
router.replace(`/tags/list`);
} catch (err) {
if (err.response.status === 400 && err.response.body.name) {
err.response.body.name.forEach(message =>
openNotificationWithIcon(
'error',
formatMessage({ id: 'tags-new.page-create-notiffy-error' }) + message,
),
);
} else {
openNotificationWithIcon(
'error',
formatMessage({ id: 'tags-new.page-create-notiffy-error' }) + err.response.body.detail,
);
}
}
},
*fetchAll(_, { call, put }) {
const response = yield call(fetchAll);
yield put({
type: 'saveAll',
payload: response,
});
},
*fetchPage({ payload }, { call, put }) {
const response = yield call(fetchPage, payload);
yield put({
type: 'savePage',
payload: response,
});
return response;
},
},
reducers: {
saveAll(_, { payload }) {
return payload.results;
},
savePage(_, { payload }) {
return payload.data;
},
},
};
export default TagsModel;
service: TagsService,
});
12 changes: 9 additions & 3 deletions frontend-project/src/pages/cases/CasesDetailView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { openNotificationWithIcon } from '@/models/global';

interface CasesDetailViewProps {
cases: ReduxResourceState<Case>;
tags: Tag[];
tags: ReduxResourceState<Tag>;
users: User[];
institutions: Institution[];
features: ReduxResourceState<Feature>;
Expand Down Expand Up @@ -100,7 +100,13 @@ function CasesDetailView({
if (isEdit) dispatch({ type: 'cases/fetchOne', payload: { id: Number(match.params.id) } });
}, []);

if (cases.isLoading || features.isLoading || (isEdit && !editedCase) || isSubmitting) {
if (
cases.isLoading ||
tags.isLoading ||
features.isLoading ||
(isEdit && !editedCase) ||
isSubmitting
) {
return (
<Row justify="center">
<Col>
Expand Down Expand Up @@ -176,7 +182,7 @@ function CasesDetailView({
mode="tags"
placeholder={formatMessage({ id: localeKeys.cases.detailView.placeholders.tags })}
>
{tags.map(tag => (
{tags.data.map(tag => (
<Option key={tag.name} value={tag.name}>
{tag.name}
</Option>
Expand Down
141 changes: 141 additions & 0 deletions frontend-project/src/pages/tags/TagsDetailView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Button, Col, Card, Form, Input, Row, Space, Spin } from 'antd';
import { connect, useDispatch } from 'dva';
import React, { useEffect, useState } from 'react';
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';

interface TagsDetailViewProps {
tags: ReduxResourceState<Tag>;
match: RouterTypes['match'] & { params: { id: string | undefined } };
}

const layout = {
labelCol: { span: 8 },
wrapperCol: { span: 16 },
};

const tailLayout = {
wrapperCol: { offset: 8, span: 16 },
};

function TagsDetailView({ tags, match }: TagsDetailViewProps) {
const dispatch = useDispatch();
const isEdit = Boolean(match.params.id);
const editetTags = tags.data.find(value => value.id === Number(match.params.id));
const [isSubmitting, setIsSubmitting] = useState(false);
const [form] = Form.useForm();
(window as any).se = smallEodSDK;
function onRequestDone(response: ServiceResponse<Tag>) {
setIsSubmitting(false);
if (response.status === 'success') {
router.push('/tags/list');
} else if (response.statusCode === 400) {
form.setFields(
Array.from(
Object.entries(response.errorBody),
).map(([name, errors]: [string, Array<string>]) => ({ name, errors })),
);
} else {
openNotificationWithIcon(
'error',
formatMessage({ id: localeKeys.error }),
`${formatMessage({
id: isEdit
? localeKeys.tags.detailView.errors.updateFailed
: localeKeys.tags.detailView.errors.createFailed,
})} ${response.errorText}`,
);
}
}

function onFinish() {
if (isEdit) {
dispatch({
type: 'tags/update',
payload: {
...form.getFieldsValue(),
id: match.params.id,
onResponse: onRequestDone,
},
});
} else {
dispatch({
type: 'tags/create',
payload: {
...form.getFieldsValue(),
onResponse: onRequestDone,
},
});
}
setIsSubmitting(true);
}

useEffect(() => {
if (isEdit) dispatch({ type: 'tags/fetchOne', payload: { id: Number(match.params.id) } });
}, []);

if (tags.isLoading || (isEdit && !editetTags) || isSubmitting) {
return (
<Row justify="center">
<Col>
<Spin size="large" />
</Col>
</Row>
);
}
if (isEdit) form.setFieldsValue(editetTags);

return (
<Form {...layout} form={form} onFinish={onFinish}>
<PageHeaderWrapper
content={formatMessage({
id: isEdit
? localeKeys.tags.detailView.editPageHeaderContent
: localeKeys.tags.detailView.newPageHeaderContent,
})}
>
<Card bordered={false}>
<Row>
<Col span={16}>
<Form.Item
label={formatMessage({ id: localeKeys.tags.fields.name })}
name="name"
rules={[
{
required: true,
message: formatMessage({ id: localeKeys.tags.detailView.errors.name }),
},
]}
>
<Input
placeholder={formatMessage({ id: localeKeys.tags.detailView.placeholders.name })}
/>
</Form.Item>
</Col>
</Row>
<Row>
<Col span={16}>
<Form.Item {...tailLayout}>
<Space>
<Button type="primary" htmlType="submit">
<FormattedMessage id={localeKeys.form.save} />
</Button>
</Space>
</Form.Item>
</Col>
</Row>
</Card>
</PageHeaderWrapper>
</Form>
);
}

export default connect((props: TagsDetailViewProps) => props)(TagsDetailView);
Loading

0 comments on commit dc4dd87

Please sign in to comment.