Skip to content

Commit

Permalink
Add login component
Browse files Browse the repository at this point in the history
  • Loading branch information
audrey-yang committed Dec 7, 2024
1 parent cf4ba01 commit 993902a
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 10 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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "taskflow2",
"version": "0.0.2",
"version": "0.1.0",
"description": "a little task manager",
"main": ".vite/build/main/index.js",
"author": "@audrey-yang",
Expand Down
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ interface Window {
changeTaskTitle: (id: string, title: string) => Promise<any>;
changeTaskNote: (id: string, note: string) => Promise<any>;
deleteTask: (id: string) => Promise<void>;
checkPassword: (password: string) => Promise<boolean>;
};
}
3 changes: 3 additions & 0 deletions src/main/backend/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@ export const api = {
// Recursively delete descendants
await recursiveDelete(id);
},
checkPassword: (password: string) => {
return password === import.meta.env.VITE_APP_PASSWORD;
},
};
3 changes: 3 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ app.whenReady().then(() => {
ipcMain.handle("deleteTask", async (_, id: string) => {
return await api.deleteTask(id);
});
ipcMain.handle("checkPassword", async (_, password) => {
return api.checkPassword(password);
});

createWindow();

Expand Down
1 change: 1 addition & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const api = {
changeTaskTitle: (id: string, title: string) => ipcRenderer.invoke("changeTaskTitle", id, title),
changeTaskNote: (id: string, note: string) => ipcRenderer.invoke("changeTaskNote", id, note),
deleteTask: (id: string) => ipcRenderer.invoke("deleteTask", id),
checkPassword: (password: string) => ipcRenderer.invoke("checkPassword", password),
};

// Use `contextBridge` APIs to expose Electron APIs to
Expand Down
21 changes: 15 additions & 6 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createTheme, ThemeProvider } from "@mui/material/styles";
import { CssBaseline, Typography } from "@mui/material";
import Header from "./components/Header";
import TaskList from "./components/TaskList";
import Login from "./components/Login";
import { STATUS } from "./types";

const darkTheme = createTheme({
Expand All @@ -19,6 +20,8 @@ const darkTheme = createTheme({
});

const App: () => JSX.Element = () => {
const [isLoggedIn, setIsLoggedIn] = useState(window.sessionStorage.getItem("loggedIn") === "y");

// Lift state out of Header for refresh
const [numUnstartedTasks, setNumUnstartedTasks] = useState(0);
const [numInProgressTasks, setNumInProgressTasks] = useState(0);
Expand All @@ -37,12 +40,18 @@ const App: () => JSX.Element = () => {
<Typography variant="h3" className="my-2">
Taskflow
</Typography>
<Header
numUnstartedTasks={numUnstartedTasks}
numInProgressTasks={numInProgressTasks}
numCompletedTasks={numCompletedTasks}
/>
<TaskList refreshHeader={getTaskCounts} />
{isLoggedIn ? (
<>
<Header
numUnstartedTasks={numUnstartedTasks}
numInProgressTasks={numInProgressTasks}
numCompletedTasks={numCompletedTasks}
/>
<TaskList refreshHeader={getTaskCounts} />
</>
) : (
<Login setIsLoggedIn={setIsLoggedIn} />
)}
</div>
</ThemeProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Header = ({
<Typography>Completed: {numCompletedTasks}</Typography>
</div>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DateCalendar sx={{ margin: 0 }} />
<DateCalendar sx={{ margin: 0 }} readOnly />
<DateCalendar
sx={{ margin: 0 }}
readOnly
Expand Down
41 changes: 41 additions & 0 deletions src/renderer/components/Login.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState } from "react";
import TextField from "@mui/material/TextField";
import IconButton from "@mui/material/IconButton";
import DoneIcon from "@mui/icons-material/Done";

const Login = ({ setIsLoggedIn }: { setIsLoggedIn: (isLoggedIn: boolean) => void }) => {
const [password, setPassword] = useState("");
const [hasError, setHasError] = useState(false);

const submitPassword = async () => {
if (await window.api.checkPassword(password)) {
window.sessionStorage.setItem("loggedIn", "y");
setIsLoggedIn(true);
} else {
setHasError(true);
}
setPassword("");
};

return (
<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}
onKeyDown={(event) => {
if (event.key === "Enter") {
submitPassword();
}
}}
/>
<IconButton onClick={submitPassword}>
<DoneIcon />
</IconButton>
</div>
);
};

export default Login;

0 comments on commit 993902a

Please sign in to comment.