Skip to content

Commit

Permalink
questions, resume
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Jan 29, 2024
1 parent 12e414c commit 121181b
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/app/questions/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import React from "react";
import _get from "lodash/get";
import { questionSamples } from "./list.sample";
import { uniq } from "lodash";

// import {
// ClockIcon,
// CogIcon,
Expand All @@ -27,7 +28,6 @@ import { uniq } from "lodash";
// StatusOnlineIcon,
// } from "@heroicons/react/outline";


const cities = [
{
city: "Athens",
Expand Down Expand Up @@ -210,6 +210,11 @@ export const ListQuestions = () => {
}
};

const handleSaveQuestion = () => {
console.log("save question");

}

return (
<Card className="max-w-xl">
<Bold>Questions</Bold>
Expand Down Expand Up @@ -249,6 +254,12 @@ export const ListQuestions = () => {
question={selectedQuestion}
handleChangeAnswer={handleChangeAnswer}
/>

<div className="flex justify-center mt-2">
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Save
</button>
</div>
</Card>
</Col>
)}
Expand Down
27 changes: 25 additions & 2 deletions src/app/resume/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@ import React from "react";
import { Textarea } from "@tremor/react";

export const ResumePage = () => {
const [resume, setResume] = React.useState("");

React.useEffect(() => {
const getResume = async () => {
const resume = await (window as any).api.invoke("resume:get");
console.log("current resume", resume);
setResume(resume);
};
getResume();
}, []);

const saveResume = async () => {
await (window as any).api.invoke("resume:save", resume);
};

return (
<div>
<Textarea rows={100} placeholder="Resume" />
<Textarea
value={resume}
rows={100}
placeholder="Resume"
onChange={(e) => setResume(e.target.value)}
/>
<div className="flex justify-center mt-2 mb-2">
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={() => saveResume()}
>
Save
</button>
</div>
Expand Down
25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { APPEVENTS, AppEvents } from './events';
import { BrowserWindow, app, dialog, ipcMain, shell } from 'electron';
import { getState, setState } from "./utils/state";
import { getAllQuestion, getResume, getState, saveQuestion, saveResume, setState } from "./utils/state";

import { gotoMainPage } from './config/app';
import packageJson from '../package.json';
Expand Down Expand Up @@ -63,7 +63,7 @@ const createWindow = (): void => {
webPreferences: {
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
nodeIntegration: true,
devTools: false,
// devTools: false,
},
});

Expand Down Expand Up @@ -138,6 +138,27 @@ ipcMain.handle('state', async (event) => {
return state;
});


ipcMain.handle('questions:getall', async (event) => {
const questions = await getAllQuestion();
return questions;
});

ipcMain.handle('questions:save', async (event, question) => {
const savedQuestion = await saveQuestion(question as any);
return savedQuestion;
});

ipcMain.handle('resume:get', async (event, question) => {
const savedResume = await getResume();
return savedResume;
});

ipcMain.handle('resume:save', async (event, resume) => {
const savedResume = await saveResume(resume as any);
return savedResume;
});

ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
const state = await getState();
// const browser = await getBrowser();
Expand Down
9 changes: 8 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ const {
ipcRenderer
} = require("electron");

let validChannels = ["my-invokable-ipc", "state", "app:start", "app:stop", "list:start", "list:stop", "settings:save"]; // list of ipcMain.handle channels you want access in frontend to
let validChannels = ["my-invokable-ipc",
"state",
"app:start", "app:stop",
"list:start", "list:stop",
"settings:save",
"questions:getall", "questions:save",
"resume:get", "resume:save",
]; // list of ipcMain.handle channels you want access in frontend to
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
// window.addEventListener('DOMContentLoaded', () => {
Expand Down
81 changes: 80 additions & 1 deletion src/utils/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isEmpty, uniqBy } from "lodash";

import { Question } from "../app/questions/interfaces";
import fs from "fs";
import path from "path";

Expand Down Expand Up @@ -104,4 +105,82 @@ export const addJob = async (job: AppJob) => {
const newState = { ...state, jobs: newJobs };
await setState(newState);
return newState;
}
};


function getReadableId(question: string) {
return question.replace(/[^a-zA-Z0-9]/g, "-").toLowerCase().substring(0, 200);
};


// QUESTIONS

// get questions
export function getAllQuestion(): Question[] {
try {
const appDataDirPath = getAppDataPath();
const questionDirPath = path.join(appDataDirPath, "questions");

// get all files in questions dir
const files = fs.readdirSync(questionDirPath);
const questions = files.map((file) => {
const questionFile = path.join(questionDirPath, file);
const questionDataString = fs.readFileSync(questionFile);
const questionData = JSON.parse(questionDataString.toString());
return questionData as Question;
});

return questions;
}
catch (error) {
return null;
}
};

export function saveQuestion(question: Question): boolean {
try {
const appDataDirPath = getAppDataPath();
const questionDirPath = path.join(appDataDirPath, "questions");
const questionFile = `${getReadableId(question.question)}.json`;
// Create appDataDir if not exist
if (!fs.existsSync(questionDirPath)) {
fs.mkdirSync(questionDirPath);
}

const questionFilDestination = path.join(appDataDirPath, questionFile);
const state = JSON.stringify(question, null, 2);

fs.writeFileSync(questionFilDestination, state);
return true;
}
catch (error) {
return false;
}
};


// save resume text to file
// load resume text from file
export const getResume = () => {
try {
const appDataDirPath = getAppDataPath();
const resumeFilePath = path.join(appDataDirPath, "resume.txt");
const resumeText = fs.readFileSync(resumeFilePath);
return resumeText.toString();
}
catch (error) {
return null;
}
};

export const saveResume = (resume: string) => {
try {
const appDataDirPath = getAppDataPath();
const resumeFilePath = path.join(appDataDirPath, "resume.txt");
fs.writeFileSync(resumeFilePath, resume);
return true;
}
catch (error) {
return false;
}
};

0 comments on commit 121181b

Please sign in to comment.