Skip to content

Commit

Permalink
refactor: improve task movement logic in TaskCard and remove unused u…
Browse files Browse the repository at this point in the history
…seLocalStorage hook
  • Loading branch information
hasib-devs committed Nov 25, 2024
1 parent 308a838 commit 4ecb287
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 41 deletions.
12 changes: 10 additions & 2 deletions src/components/TaskCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ const TaskCard = ({ status }: Props) => {
const { moveTask, getTasksByStatus } = useTaskContext();
const tasks = getTasksByStatus(status);

function handleMove(item: { task: TaskType; type: TaskStatus }) {
moveTask(item.task, status as TaskStatus);
}

const [{ isOver }, dropRef] = useDrop(() => {
return {
accept: Object.values(TaskStatus),
drop(item: { task: TaskType; type: TaskStatus }) {
moveTask(item.task, status as TaskStatus);
drop(item: { task: TaskType; type: TaskStatus }, monitor) {
const didDrop = monitor.didDrop();
if (didDrop) {
return;
}
handleMove(item);
},
collect(monitor) {
return {
Expand Down
3 changes: 1 addition & 2 deletions src/contexts/task-context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createContext, FC, ReactNode, useState } from "react";
import { TaskCreateType, TaskStatus, TaskType } from "../types";
import { useLocalStorage } from "../hooks";

type TaskContextType = {
tasks: TaskType[];
Expand All @@ -16,7 +15,7 @@ export const TaskContext = createContext<TaskContextType | undefined>(
);

export const TaskProvider: FC<{ children: ReactNode }> = ({ children }) => {
const [tasks, setTasks] = useLocalStorage<TaskType[]>("tasks", [
const [tasks, setTasks] = useState<TaskType[]>([
{
id: 1,
name: "Task One",
Expand Down
38 changes: 1 addition & 37 deletions src/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect, useState } from "react";
import { useContext } from "react";
import { TaskContext } from "../contexts/task-context";

export function useTaskContext() {
Expand All @@ -8,39 +8,3 @@ export function useTaskContext() {
}
return context;
}

export function useLocalStorage<T>(key: string, initialValue: T) {
const [storedValue, setStoredValue] = useState<T>(() => {
try {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.error("Error reading from localStorage:", error);
return initialValue;
}
});

const setValue = (value: T | ((val: T) => T)) => {
try {
const valueToStore =
value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
localStorage.setItem(key, JSON.stringify(valueToStore));
} catch (error) {
console.error("Error writing to localStorage:", error);
}
};

useEffect(() => {
try {
const item = localStorage.getItem(key);
if (item) {
setStoredValue(JSON.parse(item));
}
} catch (error) {
console.error("Error reading from localStorage on key change:", error);
}
}, [key]);

return [storedValue, setValue] as const;
}

0 comments on commit 4ecb287

Please sign in to comment.