/
asset-notifications-custom.tsx
133 lines (124 loc) · 3.67 KB
/
asset-notifications-custom.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { ProgramNotificationSettingList } from "gv-api-web";
import React, { useCallback } from "react";
import { WithTranslation, withTranslation as translate } from "react-i18next";
import { connect, ResolveThunks } from "react-redux";
import {
ActionCreatorsMapObject,
bindActionCreators,
compose,
Dispatch
} from "redux";
import Chip, { CHIP_TYPE } from "shared/components/chip/chip";
import Dialog from "shared/components/dialog/dialog";
import GVButton from "shared/components/gv-button";
import withLoader, { WithLoaderProps } from "shared/decorators/with-loader";
import useIsOpen from "shared/hooks/is-open.hook";
import { AuthRootState, SetSubmittingType } from "shared/utils/types";
import {
TAddNotification,
TRemoveNotification,
TToggleNotification
} from "./asset-notifications.types";
import CustomNotification from "./custom-notification";
import CustomNotificationCreateForm, {
ICustomNotificationCreateFormValues
} from "./custom-notification-create-form";
const _AssetNotificationsCustom: React.FC<Props> = ({
t,
service,
asset,
errorMessage,
removeNotification,
toggleNotification
}) => {
const [isOpenPopup, setOpenPopup, setClosePopup] = useIsOpen();
const handleSubmit = useCallback(
(
values: ICustomNotificationCreateFormValues,
setSubmitting: SetSubmittingType
) =>
service
.addNotification(
{
assetId: asset.assetId,
...values
},
t(`notifications-page.custom.create-alert`)
)
.then(setClosePopup)
.catch(() => {
setSubmitting(false);
}),
[asset.assetId, service, setClosePopup, t]
);
return (
<div className="notification-settings custom-notifications">
<h3 className="notification-settings__subtitle">
{t("notifications-page.custom.title")}
</h3>
{asset.settingsCustom.map(settings => (
<CustomNotification
settings={settings}
key={settings.id}
removeNotification={removeNotification}
toggleNotifications={toggleNotification}
/>
))}
<div className="custom-notification__create">
<GVButton variant="text" onClick={setOpenPopup}>
<>
<Chip type={CHIP_TYPE.POSITIVE}>+</Chip>
{t("notifications-page.create.title")}
</>
</GVButton>
</div>
<Dialog open={isOpenPopup} onClose={setClosePopup}>
<CustomNotificationCreateForm
asset={asset}
errorMessage={errorMessage}
onSubmit={handleSubmit}
/>
</Dialog>
</div>
);
};
const mapStateToProps = (state: AuthRootState): StateProps => ({
errorMessage: "" //state.programNotifications.errorMessage TODO
});
const mapDispatchToProps = (
dispatch: Dispatch,
{ addNotification }: OwnProps
): DispatchProps => ({
service: bindActionCreators<ServiceThunks, ResolveThunks<ServiceThunks>>(
{ addNotification },
dispatch
)
});
interface Props extends OwnProps, StateProps, DispatchProps, WithTranslation {}
interface OwnProps {
asset: ProgramNotificationSettingList;
addNotification: TAddNotification;
removeNotification: TRemoveNotification;
toggleNotification: TToggleNotification;
}
interface StateProps {
errorMessage?: string;
}
interface ServiceThunks extends ActionCreatorsMapObject {
addNotification: TAddNotification;
}
interface DispatchProps {
service: ResolveThunks<ServiceThunks>;
}
const AssetNotificationsCustom = compose<
React.ComponentType<OwnProps & WithLoaderProps>
>(
withLoader,
translate(),
connect(
mapStateToProps,
mapDispatchToProps
),
React.memo
)(_AssetNotificationsCustom);
export default AssetNotificationsCustom;