Skip to content

Instantly share code, notes, and snippets.

@lawreyios
Last active July 15, 2019 02:09
Show Gist options
  • Save lawreyios/4705aa12920d0e46368d48e5c46e76a5 to your computer and use it in GitHub Desktop.
Save lawreyios/4705aa12920d0e46368d48e5c46e76a5 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
import NotificationForm from './NotificationForm';
import ProgressIndicator from './ProgressIndicator';
const App = props => {
const [notificationState, setNotificationState] = useState({
title: '',
subtitle: ''
});
const [loadingState, setLoadingState] = useState({
isLoading: false
});
const _resetFields = () => {
setNotificationState({
title: '',
subtitle: ''
});
}
const _handleTitleChange = (e) => {
setNotificationState({
title: e.target.value,
subtitle: notificationState.subtitle
});
}
const _handleSubtitleChange = (e) => {
setNotificationState({
title: notificationState.title,
subtitle: e.target.value
});
}
const _handleSend = () => {
setLoadingState({
isLoading: true
});
var data = {
app_id: "[INSERT APP ID]",
headings: { "en": notificationState.title },
contents: { "en": notificationState.subtitle },
included_segments: ["All"]
};
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Basic [INSERT REST API KEY]"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
var https = require('https');
var req = https.request(options, function (res) {
res.on('data', function (data) {
_resetFields();
setLoadingState({
isLoading: false
});
});
});
req.on('error', function (e) {
setLoadingState({
isLoading: false
});
});
req.write(JSON.stringify(data));
req.end();
}
return loadingState.isLoading
? <ProgressIndicator/>
: <NotificationForm
titleChange={_handleTitleChange}
subtitleChange={_handleSubtitleChange}
click={_sendNotification}
/>
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment