Skip to content

Commit

Permalink
Merge pull request #2 from okplanbo/bugfix/S2/fix_progress_bar
Browse files Browse the repository at this point in the history
fix: [S2] progress bar fix
  • Loading branch information
okplanbo authored Dec 15, 2023
2 parents 63e8723 + 72de655 commit ea28634
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sia",
"private": true,
"version": "0.2.0",
"version": "0.2.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
44 changes: 14 additions & 30 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ import AddIcon from '@mui/icons-material/Add';
import DeleteIcon from '@mui/icons-material/Delete';
import { useTheme } from '@mui/material/styles';

import { debounce } from ":src/helpers";
import { basicList, debounce_delay, storage_key,
task_input_limit, tooptip_offset, total_percent } from ":src/constants";
import { Task } from ":src/types";
import { calcProgress, debounce } from ":src/helpers";
import { basicList, debounce_delay, storage_key, task_input_limit, tooptip_offset } from ":src/constants";

import "./App.scss";

type Task = {
description: string;
checked: boolean;
key: string;
};

function getInitialTasks() {
const initialTasks: Task[] = basicList.map(description => {
return { description, checked: false, key: uuidv4() }
Expand All @@ -31,25 +25,15 @@ function getInitialTasks() {

const saved_tasks = localStorage.getItem(storage_key);

const calcProgress = (tasks: Task[]) => {
let completedCount = 0;
tasks.forEach(item => {
if (item.checked) {
completedCount++;
}
});
return total_percent / basicList.length * completedCount;
}

export default function App(): JSX.Element {
const [tasks, setTasks] = useState<Task[]>(saved_tasks ? JSON.parse(saved_tasks) : getInitialTasks());
const [editedTasks, setEditedTasks] = useState<Task[]>([]);
const [progress, setProgress] = useState(calcProgress(tasks));
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false);
const [isConfirmOpen, setIsConfirmOpen] = useState(false);
const addButtonRef = useRef<HTMLButtonElement>(null);
const theme = useTheme();
const fullScreen = useMediaQuery(theme.breakpoints.down('md'));
const addButtonRef = useRef<HTMLButtonElement>(null);

const updateTasks = (newTasks: Task[]) => {
localStorage.setItem(storage_key, JSON.stringify(newTasks));
Expand All @@ -63,6 +47,16 @@ export default function App(): JSX.Element {
updateTasks(newTasks);
}, [tasks]);

const debouncedUpdate = debounce((newDescription: string, key: string) => {
const tasks = editedTasks.map(task => {
if (task.key === key) {
task.description = newDescription;
}
return task;
});
setEditedTasks(tasks);
}, debounce_delay);

const handleRestart = useCallback(() => {
const newTasks = tasks.slice();
newTasks.forEach(task => task.checked = false);
Expand Down Expand Up @@ -104,16 +98,6 @@ export default function App(): JSX.Element {
updateTasks(newTasks);
};

const debouncedUpdate = debounce((newDescription: string, key: string) => {
const tasks = editedTasks.map(task => {
if (task.key === key) {
task.description = newDescription;
}
return task;
});
setEditedTasks(tasks);
}, debounce_delay);

const handleTaskEdit = (value: string, key: string) => {
debouncedUpdate(value, key);
};
Expand Down
13 changes: 13 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Task } from "./types";
import { total_percent } from "./constants";

export function debounce<T extends (...args: string[]) => void>(fn: T, time: number) {
let timeoutId: NodeJS.Timeout | null;
return wrapper
Expand All @@ -10,4 +13,14 @@ export function debounce<T extends (...args: string[]) => void>(fn: T, time: num
fn(...args)
}, time)
}
}

export function calcProgress (tasks: Task[]) {
let completedCount = 0;
tasks.forEach(item => {
if (item.checked) {
completedCount++;
}
});
return total_percent / tasks.length * completedCount;
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Task = {
description: string;
checked: boolean;
key: string;
};

0 comments on commit ea28634

Please sign in to comment.