Skip to content

Commit

Permalink
Get db after login
Browse files Browse the repository at this point in the history
  • Loading branch information
audrey-yang committed Dec 9, 2024
1 parent c3a52b6 commit 4877406
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 50 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "taskflow2",
"version": "0.1.3",
"version": "0.1.4",
"description": "a little task manager",
"main": ".vite/build/main/index.js",
"author": "@audrey-yang",
Expand Down Expand Up @@ -59,6 +59,6 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/audrey-yang/taskflow.git"
"url": "https://github.com/audrey-yang/taskflow2.git"
}
}
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
interface Window {
api: {
initDb: (user: string) => Promise<any>;
createTask: (task: Task) => Promise<any>;
getChildTasksIncomplete: (parentId: string) => Promise<Task[]>;
getChildTasksComplete: (parentId: string) => Promise<Task[]>;
Expand Down
49 changes: 47 additions & 2 deletions src/main/backend/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { app } from "electron";
import path from "path";
import PouchDB from "pouchdb";
PouchDB.plugin(require("pouchdb-find"));
import { Priority, STATUS, Status, Task } from "../../renderer/types";
import { db } from "./db";

let db: PouchDB.Database;

const updateTaskField = async (id: string, field: Partial<Task>) => {
const task = await db.get(id);
Expand All @@ -10,6 +15,46 @@ const updateTaskField = async (id: string, field: Partial<Task>) => {
};

export const api = {
initDb: async (user: string) => {
db = new PouchDB(path.join(app.getPath("sessionData"), "leveldb"));
const remoteDb = new PouchDB(`${import.meta.env.VITE_CLOUDANT_URL}_${user}`, {
auth: {
username: `${import.meta.env.VITE_CLOUDANT_USERNAME}`,
password: `${import.meta.env.VITE_CLOUDANT_PASSWORD}`,
},
});

remoteDb
.info()
.then((_) => {
console.log("Successfully connected to Cloudant");
})
.catch((err: any) => {
console.error(err);
});

db.sync(remoteDb, {
live: true,
retry: true,
});

// Create indexes for Mango
db.createIndex({
index: { fields: ["parentId"] },
});
db.createIndex({
index: { fields: ["status"] },
});
db.createIndex({
index: { fields: ["parentId", "status"] },
});
db.createIndex({
index: { fields: ["priority", "status"] },
});
db.createIndex({
index: { fields: ["parentId", "priority", "status"], ddoc: "parent-status-priority" },
});
},
createTask: async (task: Task) => {
return await db.put({
...task,
Expand All @@ -21,8 +66,8 @@ export const api = {
.find({
selector: {
parentId,
priority: { $exists: true },
status: { $gte: STATUS.IN_PROGRESS },
priority: { $exists: true },
},
sort: [{ priority: "desc" }, { status: "desc" }],
})
Expand Down
42 changes: 0 additions & 42 deletions src/main/backend/db.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ app.whenReady().then(() => {
});

// Set IPC handlers
ipcMain.handle("initDb", async (_, user: string) => {
return await api.initDb(user);
});
ipcMain.handle("createTask", async (_, task: Task) => {
return await api.createTask(task);
});
Expand Down
1 change: 1 addition & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Priority, Status, Task } from "../renderer/types";

// Custom APIs for renderer
const api = {
initDb: (user: string) => ipcRenderer.invoke("initDb", user),
createTask: (task: Task) => ipcRenderer.invoke("createTask", task),
getChildTasksIncomplete: (parentId: string) =>
ipcRenderer.invoke("getChildTasksIncomplete", parentId),
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lazy, useState } from "react";
import { lazy, useState, useEffect } from "react";
import { createTheme, ThemeProvider } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import Typography from "@mui/material/Typography";
Expand Down Expand Up @@ -35,6 +35,15 @@ const App: () => JSX.Element = () => {
setNumCompletedTasks(await window.api.countChildTasksByStatus("", STATUS.COMPLETED));
};

useEffect(() => {
const logIn = async () => {
if (isLoggedIn) {
await window.api.initDb(window.localStorage.getItem("username"));
}
};
logIn();
}, [isLoggedIn]);

return (
<ThemeProvider theme={darkTheme}>
<CssBaseline />
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Login = ({ setIsLoggedIn }: { setIsLoggedIn: (isLoggedIn: boolean) => void
const submitPassword = async () => {
if (await window.api.checkPassword(password)) {
window.localStorage.setItem("loggedIn", "y");
window.localStorage.setItem("username", password);
setIsLoggedIn(true);
} else {
setHasError(true);
Expand All @@ -21,7 +22,6 @@ const Login = ({ setIsLoggedIn }: { setIsLoggedIn: (isLoggedIn: boolean) => void
<div className="flex flex-row my-4">
<TextField
label={hasError ? "Try again" : "Enter password"}
spellCheck={true}
onChange={(event) => setPassword(event.target.value)}
className="w-1/2"
error={hasError}
Expand Down

0 comments on commit 4877406

Please sign in to comment.