Browse Source

Initial app built to do API requests based on the status of the sytem (on/off)

master
kj1352 10 months ago
commit
b1a942b727
5 changed files with 271 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +143
    -0
      index.html
  3. +105
    -0
      main.js
  4. +14
    -0
      package.json
  5. +6
    -0
      preload.js

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
node_modules
dist
package-lock.json

+ 143
- 0
index.html View File

@@ -0,0 +1,143 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Monitoring Tool</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}

table {
width: 100%;
max-width: 600px;
margin: 20px auto;
border-collapse: collapse;
}

td {
padding: 10px;
}

label {
font-weight: bold;
}

button {
display: block;
margin: 20px auto;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
}

button:hover {
background-color: #0056b3;
}

textarea {
width: 100%;
min-height: 60px;
}

input, select {
width: 100%;
}

h1 {
text-align: center;
color: #333;
}

.section {
margin: 40px 0;
}
</style>
</head>

<body>
<!-- Token Saving Section -->
<div class="section">
<h1>Save Token</h1>
<form id="tokenForm">
<table>
<tr>
<td>
<label for="token">Token:</label>
</td>
<td>
<input type="text" id="token" placeholder="Enter your token" required />
</td>
</tr>
</table>
<button type="submit">Save Token</button>
</form>
</div>

<!-- Check-Out Form Section -->
<div class="section">
<h1>Check Out Form</h1>
<form id="taskForm">
<table>
<tr>
<td>
<label for="tasks">Tasks:</label>
</td>
<td>
<textarea id="tasks" placeholder="Describe your tasks..." required></textarea>
</td>
</tr>
</table>
<button type="submit">Submit Check-Out</button>
</form>
</div>

<script>
// Handle token saving
const tokenForm = document.getElementById('tokenForm');
const tokenInput = document.getElementById('token');
if (localStorage.token) {
tokenInput.value = localStorage.token;
}

tokenForm.addEventListener('submit', (e) => {
e.preventDefault();
const token = tokenInput.value.trim();
if (token) {
localStorage.token = token;
alert('Token saved successfully!');
} else {
alert('Please enter a valid token.');
}
});

// Handle check-out form submission
const taskForm = document.getElementById('taskForm');
taskForm.addEventListener('submit', (e) => {
e.preventDefault();
const project = document.getElementById('project').value;
const tasks = document.getElementById('tasks').value;
const token = localStorage.token || '';

if (!token) {
alert('Please save your token first.');
return;
}

// Send data to Electron main process
window.electronAPI.submitCheckout({ project, tasks, token });

// Display success message
alert('Check-Out submitted successfully!');
});
</script>
</body>

</html>

+ 105
- 0
main.js View File

@@ -0,0 +1,105 @@
const { app, BrowserWindow, ipcMain, powerMonitor } = require('electron');
const path = require('path');

let mainWindow;

app.on('ready', () => {
createMainWindow();

// Automatically check in on app startup
const token = getToken();
if (token) {
console.log('Automatically checking in...');
sendAPIRequest('https://workx.webtrigon.com/api/v1/slack/check-in/', { token });
} else {
console.warn('No token found for automatic check-in.');
}

// Handle sleep or lid close event
powerMonitor.on('suspend', () => {
console.log('System is going to sleep.');
const token = getToken();
if (token) {
sendAPIRequest('http://workx.webtrigon.com/api/v1/slack/check-out/', { token });
} else {
console.warn('No token found for check-out.');
}
});

powerMonitor.on('resume', () => {
console.log('System is resuming.');
const token = getToken();
if (token) {
sendAPIRequest('https://workx.webtrigon.com/api/v1/slack/check-in/', { token });
} else {
console.warn('No token found for check-in.');
}
createMainWindow(); // Reopen the window on resume
});
});

function createMainWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
});

mainWindow.loadFile('index.html');
}

// Handle manual check-out
ipcMain.on('submit-checkout', (event, taskData) => {
const { token, project, tasks } = taskData;
if (!token) {
console.error('Token is missing for check-out.');
return;
}
sendAPIRequest('http://workx.webtrigon.com/api/v1/slack/check-out/', { project, tasks, token });
});


// API request function
async function sendAPIRequest(endpoint, data = {}) {
const token = data.token || getToken(); // Retrieve token
if (!token) {
console.error('Token is required for API calls.');
return;
}

try {
const response = await fetch(endpoint, {
method: 'POST', // Assuming POST; modify if needed
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
});

if (!response.ok) {
console.error(`API call failed with status: ${response.status}`);
} else {
const result = await response.json();
console.log('API call succeeded:', result);
}
} catch (error) {
console.error('Error during API call:', error);
}
}

// Helper function to get token from local storage
function getToken() {
// Assuming `localStorage` is used to persist token via preload script
try {
const token = require('electron').ipcRenderer.sendSync('get-token');
return token || null;
} catch (error) {
// console.error('Error retrieving token:', error);
return '2c920ac10c960614a6c73ed15d11c79529910d24';
}
}

+ 14
- 0
package.json View File

@@ -0,0 +1,14 @@
{
"name": "checkin-bot",
"version": "1.0.0",
"description": "A cross-platform monitoring tool",
"main": "main.js",
"scripts": {
"start": "electron .",
"build": "electron-builder"
},
"devDependencies": {
"electron": "^25.0.0",
"electron-builder": "^25.1.8"
}
}

+ 6
- 0
preload.js View File

@@ -0,0 +1,6 @@
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
submitCheckout: (taskData) => ipcRenderer.send('submit-checkout', taskData),
getToken: () => localStorage.token || '2c920ac10c960614a6c73ed15d11c79529910d24', // Fetch token securely
});

Loading…
Cancel
Save