-
Notifications
You must be signed in to change notification settings - Fork 602
/
Title.js
154 lines (140 loc) · 4.68 KB
/
Title.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// @flow
import * as React from "react";
import { graphql } from "react-apollo";
import { compose, withHandlers, withState } from "recompose";
import styled from "react-emotion";
import { unregisterPlugin } from "webiny-plugins";
import { Typography } from "webiny-ui/Typography";
import { IconButton } from "webiny-ui/Button";
import { withSnackbar } from "webiny-admin/components";
import { withConfirmation, type WithConfirmationProps } from "webiny-ui/ConfirmationDialog";
import { ReactComponent as DeleteIcon } from "webiny-app-page-builder/editor/assets/icons/close.svg";
import { ReactComponent as EditIcon } from "webiny-app-page-builder/editor/assets/icons/edit.svg";
import { deleteElement } from "./graphql";
import EditElementDialog from "./EditElementDialog";
import createElementPlugin from "webiny-app-page-builder/admin/components/withSavedElements/createElementPlugin";
import { updateElement } from "./graphql";
const EditIconWrapper = styled("div")({
position: "absolute",
top: 0,
right: 48
});
const DeleteIconWrapper = styled("div")({
position: "absolute",
top: 0,
right: 16
});
type Props = WithConfirmationProps & {
title: string,
plugin: string,
refresh: Function,
deleteElement: Function,
updateElement: Function,
editDialogOpened: Function,
openEditDialog: Function
};
const Title = (props: Props) => {
const {
plugin: pluginName,
deleteElement,
title,
updateElement,
editDialogOpened,
openEditDialog
} = props;
return (
<>
<Typography use="overline">
{title}
<>
<EditIconWrapper>
<IconButton icon={<EditIcon />} onClick={() => openEditDialog(true)} />
</EditIconWrapper>{" "}
<EditElementDialog
onSubmit={updateElement}
plugin={pluginName}
open={!!editDialogOpened}
onClose={() => openEditDialog(false)}
/>
</>
<DeleteIconWrapper>
<IconButton icon={<DeleteIcon />} onClick={deleteElement} />
</DeleteIconWrapper>
</Typography>
</>
);
};
export default compose(
withConfirmation(({ title }) => ({
title: "Delete saved element",
message: (
<p>
Are you sure you want to permanently delete the <strong>{title}</strong> element?
</p>
)
})),
withSnackbar(),
withState("editDialogOpened", "openEditDialog", false),
graphql(deleteElement, { name: "deleteElement" }),
graphql(updateElement, { name: "updateElement" }),
withHandlers({
deleteElement: ({
id,
title,
plugin,
showConfirmation,
showSnackbar,
deleteElement,
refresh
}) => () => {
showConfirmation(async () => {
const { data: res } = await deleteElement({
variables: { id }
});
const { error } = res.pageBuilder.deleteElement;
if (error) {
return showSnackbar(error.message);
}
unregisterPlugin(plugin);
refresh();
showSnackbar(
<span>
Element <strong>{title}</strong> deleted!
</span>
);
});
},
updateElement: ({
id,
showSnackbar,
updateElement,
openEditDialog,
refresh
}) => async plugin => {
const { title: name } = plugin;
const response = await updateElement({
variables: {
id,
data: { name }
}
});
const { error, data } = response.data.pageBuilder.updateElement;
if (error) {
openEditDialog(false);
setTimeout(() => {
// For better UX, success message is shown after 300ms has passed.
showSnackbar(error.message);
}, 300);
return;
}
// This will replace previously registered block plugin.
createElementPlugin(data);
openEditDialog(false);
refresh();
setTimeout(() => {
// For better UX, success message is shown after 300ms has passed.
showSnackbar("Element " + plugin.title + " successfully saved.");
}, 300);
}
})
)(Title);