Skip to content

Commit fabe7b7

Browse files
Merge pull request #3121 from hpe-dev-incubator/slack-invite-api
Slack invite api
2 parents f92d8ae + 60173a5 commit fabe7b7

File tree

1 file changed

+64
-86
lines changed

1 file changed

+64
-86
lines changed

src/pages/slack-signup/index.js

Lines changed: 64 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState,useContext } from 'react';
1+
import React, { useState, useContext } from 'react';
22
// import '../../css/style.css';
33
import Swal from 'sweetalert2';
44
import { Box, Image, Button, TextInput, Text } from 'grommet';
@@ -8,7 +8,7 @@ import { Layout, SEO } from '../../components';
88
import { AppContext } from '../../providers/AppProvider';
99

1010
const image = '/images/hero-pic.png';
11-
const buttonstyle = {
11+
const buttonStyle = {
1212
backgroundColor: '#dcdcdc',
1313
borderRadius: '100px',
1414
align: 'center',
@@ -17,93 +17,71 @@ const buttonstyle = {
1717
fontFamily: 'sans-serif',
1818
};
1919
export default function Slacksignup() {
20+
const slackInviteApi = `${process.env.GATSBY_WORKSHOPCHALLENGE_API_ENDPOINT}/api/slack/invite`;
2021
const { user: userDetails } = useContext(AppContext);
21-
const [email, setemail] = useState(userDetails?.email || '');
22-
const onsubmit = (evt) => {
22+
const [email, setEmail] = useState(userDetails?.email || '');
23+
24+
const onsubmit = async (evt) => {
2325
evt.preventDefault();
24-
if (email) {
25-
const doInvite = () => {
26-
const formData = new FormData();
27-
formData.append('email', email);
28-
formData.append('token', process.env.GATSBY_SLACK_TOKEN);
29-
formData.append('set_active', true);
30-
return fetch(process.env.GATSBY_SLACK_INVITE_URL, {
31-
method: 'POST',
32-
body: formData,
33-
json: true,
34-
})
35-
.then((res) => res.json())
36-
.then((res) => {
37-
if (res.ok) {
38-
const el = document.createElement('div');
39-
el.innerHTML = `Please check <b> ${email}</b>
40-
for an invite from slack`;
41-
Swal.fire({
42-
title: 'SUCCESS !',
43-
html: el,
44-
icon: 'success',
45-
});
46-
} else {
47-
let { error } = res;
48-
if (error === 'already_invited' || error === 'already_in_team') {
49-
const el = document.createElement('div');
50-
el.innerHTML =
51-
"It seems like you are already member of our slack.<br>Visit <a href=https://hpedev.slack.com target='_blank' > <b> HPE Developer Community</b></a> on slack";
52-
Swal.fire({
53-
title: 'Success',
54-
html: el,
55-
icon: 'success',
56-
});
57-
} else if (error === 'already_in_team_invited_user') {
58-
const l = document.createElement('div');
59-
l.innerHTML = `Please check again <b style="font-size:large;" > ${email} </b> for an invite from Slack.<br>Visit <a href=https://developer.hpe.com/ target="_blank"> HPE Developer Community</a>`;
60-
Swal.fire({
61-
title: 'It seems like we already sent you our slack invite',
62-
html: l,
63-
icon: 'info',
64-
});
65-
} else {
66-
if (error === 'invalid_email') {
67-
error = 'The email you entered is an invalid email.';
68-
} else if (error === 'invalid_auth') {
69-
error =
70-
'Something has gone wrong. Please' +
71-
' contact a system administrator.';
72-
}
73-
Swal.fire({
74-
title: 'Error',
75-
html: error,
76-
icon: 'error',
77-
});
78-
}
79-
}
80-
})
81-
.catch((err) => {
82-
Swal.fire({
83-
title: 'Error !',
84-
html: err,
85-
icon: 'error',
86-
});
87-
});
88-
}; // end of doInvite
89-
doInvite();
90-
} // end of if statement
91-
else {
92-
const errMsg = [];
93-
if (!email) {
94-
errMsg.push('your email is required');
95-
}
26+
27+
if (!email) {
9628
Swal.fire({
97-
html: `Failed! ${errMsg.join(' and ')}.`,
29+
html: `Failed! your email is required.`,
9830
icon: 'info',
9931
});
32+
return;
33+
}
34+
35+
try {
36+
const response = await fetch(slackInviteApi, {
37+
method: 'POST',
38+
headers: { 'Content-Type': 'application/json' },
39+
body: JSON.stringify({ email }),
40+
});
41+
42+
const res = await response.json();
43+
44+
if (response.ok) {
45+
Swal.fire({
46+
title: 'Success!',
47+
html: `Please check <b>${email}</b> for an invite from Slack.`,
48+
icon: 'success',
49+
});
50+
} else {
51+
const { error } = res;
52+
53+
let htmlContent = '';
54+
if (error === 'already_invited' || error === 'already_in_team') {
55+
htmlContent = `It looks like you're already a member. Visit <a href="https://hpedev.slack.com" target="_blank"><b>HPE Developer Community</b></a> on Slack.`;
56+
} else if (error === 'already_in_team_invited_user') {
57+
htmlContent = `An invite was already sent to <b>${email}</b>. Please check your inbox.<br>Visit <a href="https://developer.hpe.com/" target="_blank">HPE Developer Community</a>.`;
58+
} else if (error === 'invalid_email') {
59+
htmlContent = 'The email you entered is invalid.';
60+
} else {
61+
htmlContent =
62+
error || 'Something went wrong. Please try again later.';
63+
}
64+
65+
Swal.fire({
66+
title: 'Slack Invite Info',
67+
html: htmlContent,
68+
icon: 'info',
69+
});
70+
}
71+
} catch (err) {
72+
Swal.fire({
73+
title: 'Error!',
74+
html: err.message || 'Unknown error occurred.',
75+
icon: 'error',
76+
});
77+
} finally {
78+
setTimeout(() => {
79+
setEmail('');
80+
}, 2500);
10081
}
101-
setTimeout(() => {
102-
setemail('');
103-
}, 2500);
10482
};
105-
const handlechange = (event) => {
106-
setemail(event.target.value);
83+
const handleChange = (event) => {
84+
setEmail(event.target.value);
10785
};
10886
return (
10987
<Layout>
@@ -151,7 +129,7 @@ export default function Slacksignup() {
151129
placeholder="[email protected]"
152130
value={email}
153131
name="email"
154-
onChange={handlechange}
132+
onChange={handleChange}
155133
style={{ position: 'relative', marginLeft: '-10px' }}
156134
required
157135
plain
@@ -192,9 +170,9 @@ export default function Slacksignup() {
192170
align="center"
193171
style={{ marginTop: '-120px' }}
194172
>
195-
<Button label="50+ Channels" style={buttonstyle} />
196-
<Button label="Over 4,000 members" style={buttonstyle} />
197-
<Button label="6 Years+ Community" style={buttonstyle} />
173+
<Button label="50+ Channels" style={buttonStyle} />
174+
<Button label="Over 4,000 members" style={buttonStyle} />
175+
<Button label="6 Years+ Community" style={buttonStyle} />
198176
</Box>
199177
</Layout>
200178
);

0 commit comments

Comments
 (0)