Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.6 kB
2
Indexable
Never
//main.js

/*const electron = require("electron");
const url = require("url");
const path = require("path");*/

const {app,BrowserWindow,ipcMain} = require('electron');
const user = require('./models/user.js')
let mainWindow;

function createWindow(){
   const win= new BrowserWindow({
width:800,
height :700,
webPreferences :{
   nodeIntegration: true,
   contextIsolation:false,
   traceProcessWarnings:true
  },
    })
    win.loadFile('src/index.html')
}

function createWindow_1(){
   const mainWindow = new BrowserWindow({width:1400,height:1050,
     });

    mainWindow.loadFile('src/main.html' );}
 /*   mainWindow.on('closed', () => {
        mainWindow = null;
    })
}

app.on("ready", createWindow);

app.on('window-all-closed', () => {
    if(process.platform !== 'darwin'){
        app.quit()
    }
});

app.on('activate',() => {
    if (win === null){
        createWindow()
    }
});*/

ipcMain.handle('new', async (e, arg) => {
   const newUser = new user(arg)

   const userSaved = await newUser.save()
   console.log(userSaved)
  e.sender.send('new-user-created',JSON.stringify(userSaved))
 })

 ipcMain.handle("get-tasks", async (e, arg) => {
    const tasks = await Task.find();
    e.sender.send("get-tasks", JSON.stringify(tasks));
  });
  
  ipcMain.handle("delete-task", async (e, args) => {
    const taskDeleted = await Task.findByIdAndDelete(args);
    e.sender.send("delete-task-success", JSON.stringify(taskDeleted));
  });
  
  ipcMain.handle("update-task", async (e, args) => {
    console.log(args);
    const updatedTask = await Task.findByIdAndUpdate(
      args.idTaskToUpdate,
      { name: args.name, pass: args.pass },
      { new: true }
    );
    e.sender.send("update-task-success", JSON.stringify(updatedTask));
  });


module.exports= { createWindow_1,createWindow}
app.whenReady().then(createWindow_1,createWindow)



//app.js

 const taskForm = document.querySelector('#taskForm1')
const Username = document.querySelector('#Username')
const Password = document.querySelector('#Password')

 const { ipcRenderer } = require('electron')


taskForm.addEventListener("submit",async e=>{
  e.preventDefault();
const user ={
        name: Username.value,
        pass: Password.value
    }
   
   console.log(user)
    ipcRenderer.invoke('new', user).then(()=> console.log('new user entered')).catch((err)=> console.error('Error'))

     
      
}),
     
   
    ipcRenderer.on('new-user-created', (e, arg) => {
        
             const newUser= JSON.parse(arg);
             console.log(newUser)
    })