-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Share.js
242 lines (223 loc) · 6.77 KB
/
Share.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import Close from '@mui/icons-material/Close';
import Code from '@mui/icons-material/Code';
import Email from '@mui/icons-material/Email';
import FacebookIcon from '@mui/icons-material/Facebook';
import ShareIcon from '@mui/icons-material/Share';
import TwitterIcon from '@mui/icons-material/Twitter';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogTitle from '@mui/material/DialogTitle';
import Grid from '@mui/material/Grid';
import IconButton from '@mui/material/IconButton';
import Snackbar from '@mui/material/Snackbar';
import TextField from '@mui/material/TextField';
import Tooltip from '@mui/material/Tooltip';
import Typography from '@mui/material/Typography';
import { green } from '@mui/material/colors';
import makeStyles from '@mui/styles/makeStyles';
import log from 'loglevel';
import React from 'react';
import CustomShareIcon from './common/CustomShareIcon';
const useStyles = makeStyles((theme) => ({
DialogTitle: {
[theme.breakpoints.down('sm')]: {
fontSize: '18px',
},
},
closeIcon: {
width: '32px',
height: '32px',
borderRadius: '4px',
backgroundColor: theme.palette.secondary.lightGreen,
},
box1: {
padding: theme.spacing(4),
width: '100%',
[theme.breakpoints.down('sm')]: {
padding: theme.spacing(2),
},
},
code: {
minWidth: 400,
margin: 10,
},
linkText: {
fontWeight: 'bold',
},
inputField: {
width: '100%',
height: '30px',
border: 'none',
borderRadius: '4px',
padding: '8px',
marginTop: '8px',
backgroundColor: '#F5F5F5',
},
}));
function Share() {
const shareUrl = encodeURI(window.location.href);
const classes = useStyles();
const [isOpen, setIsOpen] = React.useState(false);
const [isEmbedOpen, setEmbedOpen] = React.useState(false);
const [embedCode, setEmbedCode] = React.useState('');
const [isMessageOpen, setMessageOpen] = React.useState(false);
const [message, setMessage] = React.useState('');
const [link, setLink] = React.useState('');
function handleClick() {
setIsOpen(true);
}
function handleClose() {
setIsOpen(false);
}
function handleTwitter() {
window.open(
`https://twitter.com/intent/tweet?url=${shareUrl}&via=green_stand&related=Greestand,treetracker`,
);
}
function handleFaceBook() {
window.open(
`https://www.facebook.com/dialog/share?app_id=87741124305&href=${shareUrl}&display=popup`,
);
}
const mailString = `mailto:?subject=A tree from Greenstand&body=I want to share this tree from Greenstand with you, please click this link to check it! ${shareUrl}`;
function handleEmbed() {
setIsOpen(false);
setEmbedOpen(true);
}
function handleEmbedClose() {
setEmbedOpen(false);
}
function handleChange(e) {
setEmbedCode(e.target.value);
}
React.useEffect(() => {
setEmbedCode(
`<iframe width="560" height="315" src="${window.location.href}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`,
);
setLink(window.location.href);
}, []);
function showMessage(text) {
setMessage(text);
setMessageOpen(true);
}
function handleCopy() {
log.log('copy...');
const copyTextarea = document.getElementById('EmbedCode');
copyTextarea.focus();
copyTextarea.select();
try {
const successful = document.execCommand('copy');
const msg = successful ? 'successful' : 'unsuccessful';
log.log(`Copying text command was ${msg}`);
} catch (err) {
log.log('Oops, unable to copy');
}
showMessage('Code has been copied!');
}
function handleMessageClose() {
setMessageOpen(false);
setMessage('');
}
return (
<>
<Tooltip title="share tree">
<IconButton onClick={handleClick}>
<ShareIcon style={{ color: green[500] }} />
</IconButton>
</Tooltip>
<Dialog
open={isOpen}
onClose={handleClose}
PaperProps={{
style: {
margin: '8px',
},
}}
>
<DialogTitle>
<Grid container justifyContent="space-between" alignItems="center">
<Grid item xs={8} className={classes.DialogTitle}>
Share this token
</Grid>
<Grid item>
<IconButton
className={classes.closeIcon}
onClick={handleClose}
size="large"
>
<Close style={{ color: green[500] }} />
</IconButton>
</Grid>
</Grid>
</DialogTitle>
<Grid container justifyContent="center" className={classes.box1}>
<CustomShareIcon handleOnClick={handleEmbed}>
<Code />
</CustomShareIcon>
<CustomShareIcon handleOnClick={handleFaceBook}>
<FacebookIcon />
</CustomShareIcon>
<CustomShareIcon handleOnClick={handleTwitter}>
<TwitterIcon />
</CustomShareIcon>
<CustomShareIcon mailString={mailString}>
<Email />
</CustomShareIcon>
<Grid container justifyContent="space-between" alignItems="center">
<Grid item xs={8} className={classes.linkText}>
<Typography>Or this link</Typography>
</Grid>
<Grid item xs={12}>
<input
type="text"
className={classes.inputField}
value={link}
readOnly
/>
</Grid>
</Grid>
</Grid>
</Dialog>
<Dialog open={isEmbedOpen} onClose={handleEmbedClose}>
<DialogTitle>
<Grid container justifyContent="space-between" alignItems="center">
<Grid item xs={8}>
Embed Greenstand
</Grid>
<Grid item>
<IconButton onClick={handleEmbedClose} size="large">
<Close />
</IconButton>
</Grid>
</Grid>
</DialogTitle>
<TextField
id="EmbedCode"
multiline
variant="outlined"
value={embedCode}
maxRows={4}
onChange={handleChange}
className={classes.code}
/>
<DialogActions>
<Button onClick={handleEmbedClose}>Cancel</Button>
<Button onClick={handleCopy}>Copy</Button>
</DialogActions>
</Dialog>
<Snackbar
open={isMessageOpen}
autoHideDuration={6000}
onClose={handleMessageClose}
message={message}
action={
<IconButton color="primary" onClick={handleMessageClose} size="large">
<Close />
</IconButton>
}
/>
</>
);
}
export default Share;