feat : btno fetching properly for electron

This commit is contained in:
karthik 2025-08-16 17:10:59 +05:30
parent 68bad8d8d7
commit c52b22a1ee
5 changed files with 75 additions and 51 deletions

View File

@ -1,5 +1,6 @@
const { app, BrowserWindow, screen, ipcMain } = require('electron');
const path = require('path');
const fs = require('fs');
let mainWindow;
let screenWindow;
@ -20,7 +21,7 @@ function createWindows() {
}
});
mainWindow.loadURL('http://10.83.77.61:4200/login');
mainWindow.loadURL('http://10.74.231.124:4200/login');
screenWindow = new BrowserWindow({
x: secondary.bounds.x,
@ -35,10 +36,23 @@ function createWindows() {
}
});
screenWindow.loadURL('http://10.83.77.61:4200/shared-display');
screenWindow.loadURL('http://10.74.231.124:4200/shared-display');
ipcMain.on('open-second-screen', () => screenWindow.show());
ipcMain.on('close-second-screen', () => screenWindow.hide());
// ✅ IPC to return BTID
ipcMain.handle('get-btid', () => {
try {
const filePath = path.join(process.env.HOME || process.env.USERPROFILE, 'BTID', 'betting.txt');
const content = fs.readFileSync(filePath, 'utf-8');
const match = content.match(/Btid\s*=\s*(\d+)/i);
return match ? match[1] : null;
} catch (err) {
console.error('Error reading betting.txt:', err);
return null;
}
});
}
app.whenReady().then(createWindows);

View File

@ -3,4 +3,7 @@ const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronAPI', {
openSecondScreen: () => ipcRenderer.send('open-second-screen'),
closeSecondScreen: () => ipcRenderer.send('close-second-screen'),
// ✅ Ask main process for Btid
getBtid: () => ipcRenderer.invoke('get-btid')
});

View File

@ -12,7 +12,7 @@
</span>
<span class="btno-label me-3">
<b>B.T.No: 1111</b>
<b>B.T.No: </b> {{ btid }}
</span>

View File

@ -18,6 +18,7 @@ export class NavbarComponent implements OnInit, OnDestroy {
screenWidth: number = window.innerWidth;
private subscription!: Subscription;
userName: string = '';
btid: string | null = null;
liveStatusOk: boolean = true;
@ -76,6 +77,7 @@ export class NavbarComponent implements OnInit, OnDestroy {
ngOnInit() {
this.userName = localStorage.getItem('userName') || '';
this.btid = localStorage.getItem('btid');
// Use NgZone to run setInterval outside Angular's change detection
this.zone.runOutsideAngular(() => {
setInterval(() => {

View File

@ -215,16 +215,17 @@ export class LoginComponent implements OnInit, OnDestroy {
onSubmit(): void {
// ✅ Updated onSubmit with BTID
async onSubmit(): Promise<void> {
if (this.loginForm.invalid) {
this.loginForm.markAllAsTouched();
return;
}
const { email, password } = this.loginForm.value;
this.btcService.userLogin(email, password).subscribe({
next: (response) => {
next: async (response) => {
const employee = response.body;
console.log('🧠 Raw employee response:', employee);
@ -236,48 +237,52 @@ export class LoginComponent implements OnInit, OnDestroy {
this.passwordStatus = true;
// ✅ Fetch BTID from Electron preload
const btid = await (window as any).electronAPI?.getBtid?.();
console.log("📦 BTID from file:", btid);
// Prepare print data
const printData = {
name: userName,
employeeId: employeeId,
action: 'login',
type: 'login' // ← ADD THIS
};
type: 'login'
};
// Store in localStorage
// ✅ Store in localStorage
localStorage.setItem('userName', userName);
localStorage.setItem('employeeId', employeeId);
localStorage.setItem('password', password);
localStorage.setItem('btid', btid || "unknown");
// Optional print logic
fetch('http://localhost:9100/print', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(printData),
})
.then((res) => {
if (!res.ok) throw new Error('Print failed');
console.log('🖨️ Print successful');
// ✅ Optional second screen + navigate
// fetch('http://localhost:9100/print', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify(printData),
// })
// .then((res) => {
// if (!res.ok) throw new Error('Print failed');
// console.log('🖨️ Print successful');
// Open second screen
(window as any).electronAPI?.openSecondScreen?.();
// // Navigate to home
this.router.navigate(['/home']);
})
.catch((err) => {
console.error('‼️ Print failed', err);
this.loginError = 'Login OK, but printing failed.';
});
// })
// .catch((err) => {
// console.error('‼️ Print failed', err);
// this.loginError = 'Login OK, but printing failed.';
// });
},
error: () => {
this.loginError = 'Invalid login credentials';
// },
// error: () => {
// this.loginError = 'Invalid login credentials';
},
});
}
}
showConfirmModal : boolean = false;