Skip to content

Commit

Permalink
add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniere-Mathieu committed Sep 30, 2023
1 parent 0e6ba57 commit 7c203c8
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 5 deletions.
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
make details
rework UX add to make it more readable
make image work on .projects
refactor the code
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "side-project",
"private": true,
"version": "0.0.1",
"version": "0.0.11",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
45 changes: 44 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl fmt::Display for Project {
}
}

/**
* Stores the projects vector in the data.json file
* @ return Result<(), Box<dyn std::error::Error>> (should return Ok or an error)
*/
fn store_projects(
projects: &Vec<Project>,
file_path: &str,
Expand All @@ -48,6 +52,10 @@ fn store_projects(
Ok(())
}

/**
* Reads the data.json file and returns a vector of Project structs
* @ return Result<Vec<Project>, Box<dyn std::error::Error>> (should return a vector of Project structs or an error)
*/
fn read_projects(file_path: &str) -> Result<Vec<Project>, Box<dyn std::error::Error>> {
let file = File::open(file_path);

Expand Down Expand Up @@ -77,15 +85,28 @@ fn read_projects(file_path: &str) -> Result<Vec<Project>, Box<dyn std::error::Er
Ok(projects)
}

/**
* Returns the path to the user's home directory
* @ return String (should return the path to the user's home directory)
*/
fn get_home_dir() -> String {
return env::var("HOME").expect("Failed to get the user's home directory.");
}

/**
* Returns the path to the data.json file
* @ return String (should return the path to the data.json file)
*/
fn get_file_path() -> String {
let home_dir = get_home_dir();
let file_path = format!("{}/.projects/data.json", home_dir);
file_path
}

/**
* Extracts the filename from a path and returns it as a String
* @ return Result<String, Box<dyn Error>> (should return a String as file.extension or an error)
*/
fn extract_filename(path: &str) -> Result<String, Box<dyn Error>> {
let path = Path::new(path);
let filename_osstr = path.file_name().ok_or("No file name present in path")?;
Expand All @@ -95,6 +116,11 @@ fn extract_filename(path: &str) -> Result<String, Box<dyn Error>> {
Ok(filename_str.to_string())
}

/**
* Copies the logo to the .projects folder
* @ return Result<PathBuf, Box<dyn Error>> (should return the path to the logo or an error)
*/
*/
fn copy_logo(project: &Project) -> Result<PathBuf, Box<dyn Error>> {
let logo_path = match &project.logo {
Some(path) => path,
Expand All @@ -114,7 +140,10 @@ fn copy_logo(project: &Project) -> Result<PathBuf, Box<dyn Error>> {
}

#[tauri::command]

/**
* Returns the project with the given id
* @ return Option<Project> (should return the project with the given id or None if the project is not found)
*/
fn get_project(id: u32) -> Option<Project> {
let projects = get_projects();

Expand All @@ -128,6 +157,9 @@ fn get_project(id: u32) -> Option<Project> {
}

#[tauri::command]
/**
* Deletes the project with the given id
*/
async fn delete_project(id: u32) {
let mut projects = get_projects();
projects.retain(|project| project.id != id);
Expand All @@ -138,6 +170,10 @@ async fn delete_project(id: u32) {
}

#[tauri::command]
/**
* Returns a vector of all projects
* @ return Vec<Project> (should return a vector of all projects)
*/
fn get_projects() -> Vec<Project> {
let file_path = get_file_path();
match read_projects(&file_path) {
Expand All @@ -149,6 +185,10 @@ fn get_projects() -> Vec<Project> {
}
}

/**
* Adds a project to the data.json file
* @ return Result<bool, String> (should return true if the project was added successfully or an error)
*/
#[tauri::command]
fn add_project(mut project: Project) -> Result<bool, String> {
let mut projects = get_projects();
Expand All @@ -168,6 +208,9 @@ fn add_project(mut project: Project) -> Result<bool, String> {
}
}

/**
* Runs the Tauri application
*/
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
Expand Down
4 changes: 4 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<script lang="ts">
// this component is the main component of the app
// it contains the sidebar and the routes
// it also contains the router
// the router is used to navigate between the routes
import SideBar from "./lib/SideBar.svelte";
import { Router, Route } from "svelte-routing";
Expand Down
9 changes: 9 additions & 0 deletions src/lib/Add.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
logo: "",
};
// this is used to get the options for the status (object to select array)
let options = Object.values(Status) as string[];
project.status = options[0];
/**
* @description this function is used to add a logo to the project
* @returns {Promise<void>}
*/
async function addLogo() {
const selected = await open({
multiple: false,
Expand All @@ -40,6 +45,10 @@
}
}
/**
* @description this function is used to save the data to the database
* @returns {Promise<void>}
*/
async function saveData() {
try {
if (!project.name || !project.description || !project.status) {
Expand Down
12 changes: 9 additions & 3 deletions src/lib/Deleted.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
const dispatch = createEventDispatcher();
export let id: number;
/**
* @description this function is used to delete a project
* @returns {Promise<void>}
*/
async function deleteProject() {
console.log("here");
await parseTauriCommand("delete_project", { id });
console.log("pass after delete");
dispatch("deleted");
}
/**
* @description this function is used to handle the keydown event for accesibility
* @param {KeyboardEvent} event
* @returns {void}
*/
function handleKeydown(event: KeyboardEvent) {
// Activate on Enter or Space keys
if (event.key === "Enter" || event.key === " ") {
deleteProject();
}
Expand Down
18 changes: 18 additions & 0 deletions src/lib/List.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
let list: Project[] = [];
/**
* @description this function is used to get the list from tauri and set it to the list variable
* @returns {Promise<void>}
*
*/
async function getList() {
try {
list = await parseTauriCommand<Project[]>("get_projects");
Expand All @@ -20,12 +25,21 @@
console.error(error);
}
}
/**
* @description this function is used to update the list after a project is deleted
*/
async function updateProject() {
await getList();
}
onMount(getList);
/**
* @description this function is used to compute the time between now and the creation of the project
* @param item {Project} the project to compute the time for
* @returns {number} the time between now and the creation of the project (days)
*/
function computeTime(item: Project): number {
if (item.status !== Status.Active && item.status !== Status.Inactive)
return 0;
Expand All @@ -35,6 +49,10 @@
return diff;
}
/**
* @description this function is used to notify the user if a project is not updated for more than 7 days
* @returns {void}
*/
function notify() {
if ($hasInitialized === false) {
list.forEach((item) => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/Routes.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script lang="ts">
// this component contains the routes of the app
import { Route } from "svelte-routing";
import Add from "./Add.svelte";
import Details from "./Details.svelte";
Expand Down
3 changes: 3 additions & 0 deletions src/lib/SideBar.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<script lang="ts">
// this component is the sidebar of the app
// it contains the links to the routes
// it also contains the icons for the links
import { Link } from "svelte-routing";
import IoIosAdd from "svelte-icons/io/IoIosAdd.svelte";
import IoMdHome from "svelte-icons/io/IoMdHome.svelte";
Expand Down
1 change: 1 addition & 0 deletions src/lib/StatusIcon.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
// this component is used to display the status of a project
import { Status } from "/src/project";
import IoMdPause from "svelte-icons/io/IoMdPause.svelte";
import IoMdCheckmark from "svelte-icons/io/IoMdCheckmark.svelte";
Expand Down

0 comments on commit 7c203c8

Please sign in to comment.