Skip to content

Commit

Permalink
perf: implemeting jotai finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Zafkiel45 committed Aug 15, 2024
1 parent ebe54ff commit 83f3fec
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 86 deletions.
6 changes: 6 additions & 0 deletions todo-list/src/app/(atoms)/(global)/global-atoms.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use client';
import { atom } from "jotai";

type toggleSideBarAtom = 'left-0' | 'left-[-100%]';

export const globalToggleSidebarAtoms = atom<toggleSideBarAtom>('left-0')
14 changes: 14 additions & 0 deletions todo-list/src/app/(atoms)/(input)/input-atoms.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client';
import { atom } from "jotai";

interface warnModalStateTypes {
display: 'hidden' | 'flex';
blur?: boolean;
}

export const inputTaskNameAtom = atom<string>('');
export const inputBlurModalAtom = atom<boolean>(false);
export const switchModeButtonAtom = atom<string>('');
export const warnModalStateAtom = atom<warnModalStateTypes>({
display: 'hidden',
});
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
'use client';
import { useContext, useState, KeyboardEvent } from "react";
import { todoContext } from "./context";
import { SwitchModeButton } from "./(input_estruture)/switch_button";
import { HowToUse } from "./(input_estruture)/howToUse";
import { InputNameTask } from "./(input_estruture)/inputNameTask";
import { AddButton } from "./(input_estruture)/addButton";
import { DeleteButton } from "./(input_estruture)/deleteButton";
import { Modal } from "./(input_estruture)/Modal";
import { useIndexedDB } from "./(database)/useOpenDB";
import { UpdateDB } from "./utility/updateDB";
import { HeaderInput } from "./(input_estruture)/header";

import { useSetAtom } from "jotai";
import { useContext, KeyboardEvent } from "react";
import { todoContext } from "../components/context";
import { SwitchModeButton } from "../components/(input_estruture)/switch_button";
import { HowToUse } from "../components/(input_estruture)/howToUse";
import { InputNameTask } from "../components/(input_estruture)/inputNameTask";
import { AddButton } from "../components/(input_estruture)/addButton";
import { DeleteButton } from "../components/(input_estruture)/deleteButton";
import { Modal } from "../components/(input_estruture)/Modal";
import { useIndexedDB } from "../components/(database)/useOpenDB";
import { UpdateDB } from "../components/utility/updateDB";
import { HeaderInput } from "../components/(input_estruture)/header";
// hooks
import { useAtom, useSetAtom, useAtomValue } from "jotai";
// atoms
import { tasksStateAtom } from "../(atoms)/(tasks)/tasks-atoms";

export interface controler {
visible: string;
blur?: boolean;
}
import { inputTaskNameAtom } from "../(atoms)/(input)/input-atoms";
import { tasksDescriptionStateAtom } from "../(atoms)/(modal)/modal-atoms";
import { warnModalStateAtom } from "../(atoms)/(input)/input-atoms";
import { inputBlurModalAtom } from "../(atoms)/(input)/input-atoms";

export const InputTask = () => {

// only update:
const setTasksState = useSetAtom(tasksStateAtom);
// update and read
const [inputTaskNameState, setInputTaskNameState] = useAtom(inputTaskNameAtom);
const [warnModalState, setWarnModalState] = useAtom(warnModalStateAtom);
const [inputBlurModalState, setInputBlurModalState] = useAtom(inputBlurModalAtom);
// only read:
const tasksDescriptionState = useAtomValue(tasksDescriptionStateAtom);

const { toggleSideBarFunction } = useContext(todoContext);

const [displayControl, setDisplayControl] = useState<controler>({
visible: "hidden",
});
const {
setBlur,
blur,
name,
setName,
descrebe,
toggleSideBarFunction,
} = useContext(todoContext);

// ===========================================================================
useIndexedDB("tasks", "database");
const addElementInDB = async () => {
const currentDataBase: IDBOpenDBRequest = indexedDB.open("database");
Expand All @@ -51,37 +45,37 @@ export const InputTask = () => {
window.alert("mesmo nome!");
};

if (name === "" || name.trim() === "" || name.length >= 20) {
if (inputTaskNameState === "" || inputTaskNameState.trim() === "" || inputTaskNameState.length >= 20) {
window.alert("Erro!");
setName("");
setInputTaskNameState("");
database.abort();
return;
}

objectStorage.add({
title: name,
description: descrebe,
title: inputTaskNameState,
description: tasksDescriptionState,
type: '',
color: '',
});

UpdateDB(setTasksState, setName);
UpdateDB(setTasksState, setInputTaskNameState);
toggleSideBarFunction();
};

currentDataBase.onerror = () => {
console.log("ops, algo deu errado!");
};
};
// ===========================================================================

const keyPressEvent = (event: KeyboardEvent<HTMLInputElement>) => {
if (event.key === "Enter") {
addElementInDB();
toggleSideBarFunction();
event.currentTarget.blur();
}
};
// ===========================================================================

const removeAllElements = (): void => {
const openDatabase: IDBOpenDBRequest = indexedDB.open("database");

Expand All @@ -103,16 +97,16 @@ export const InputTask = () => {
};

setTasksState(() => []);
setBlur(() => false);
setDisplayControl({ ...displayControl, visible: "hidden", blur: false });
setInputBlurModalState(() => false);
setWarnModalState({ ...warnModalState, display: "hidden", blur: false });
};
};
// ===========================================================================

const controlElementsDisplay = () => {
setDisplayControl({ ...displayControl, visible: "flex" });
setBlur(() => true);
setWarnModalState({ ...warnModalState, display: "flex" });
setInputBlurModalState(() => true);
};
// ===========================================================================

return (
<>
<div
Expand All @@ -121,7 +115,7 @@ export const InputTask = () => {
pt-3
w-full
transition-all
${blur ? "blur-sm" : null}
${inputBlurModalState ? "blur-sm" : null}
h-full
overflow-auto
mobileMini:pb-8
Expand All @@ -147,9 +141,6 @@ export const InputTask = () => {
<HowToUse />
</div>
<Modal
Blur={setBlur}
dispatch={setDisplayControl}
objectComplete={displayControl}
removeAllElements={removeAllElements}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';
import { SearchTask } from "./(tasks_estruture)/search";
import { FilterTasks } from "./(tasks_estruture)/filter";
import { SearchTask } from "../components/(tasks_estruture)/search";
import { FilterTasks } from "../components/(tasks_estruture)/filter";
// hooks
import { useMemo } from "react";
import { useAtom } from "jotai";
Expand Down
27 changes: 14 additions & 13 deletions todo-list/src/app/components/(input_estruture)/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
// removeAllElements, objectComplete, dispacth, setBlur

import { Dispatch, SetStateAction } from "react";
import { controler } from "../input";
'use client'
import CloseButton from '../utility/svg_components/x'
// hooks
import { useAtom, useSetAtom } from 'jotai';
// atoms
import { warnModalStateAtom } from '@/app/(atoms)/(input)/input-atoms';
import { inputBlurModalAtom } from '@/app/(atoms)/(input)/input-atoms';

interface TypeOfProps {
Blur: Dispatch<SetStateAction<boolean>>;
dispatch: Dispatch<SetStateAction<controler>>;
objectComplete: controler;
removeAllElements: () => void;
}

export const Modal = ({
Blur,
dispatch,
objectComplete,
removeAllElements,
}: TypeOfProps) => {

const [warnModalState, setWarnModalState]= useAtom(warnModalStateAtom);
// only update
const setInputBlurModalState = useSetAtom(inputBlurModalAtom);

return (
<div
className={`
fixed
top-0
${objectComplete.visible}
${warnModalState.display}
text-black
dark:text-white
gap-4 items-center
Expand Down Expand Up @@ -64,8 +65,8 @@ export const Modal = ({
AVISO!
</h1>
<div onClick={() => {
dispatch({ ...objectComplete, visible: "hidden" });
Blur(() => false);
setWarnModalState({ ...warnModalState, display: "hidden" });
setInputBlurModalState(false);
}}>
<CloseButton height="h-[22px] w-[22px]" />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const CloseButton = (

}:{
toggleSideBarFunction: () => void,

}) => {

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export const CloseButton = (
{
toggleSideBarFunction,

}:{toggleSideBarFunction: () => void}) => {
return (
<div
Expand Down
13 changes: 8 additions & 5 deletions todo-list/src/app/components/(input_estruture)/inputNameTask.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
'use client'
import { todoContext } from "../context"
import { useContext } from "react"
// hooks
import { useAtom } from "jotai"
// atoms
import { inputTaskNameAtom } from "@/app/(atoms)/(input)/input-atoms"

interface TypeOfProps {
keyEvent: (e:any) => void;
}

export const InputNameTask = ({keyEvent}:TypeOfProps) => {
const { setName, name } = useContext(todoContext);

const [inputTaskNameState, setInputTaskNameState] = useAtom(inputTaskNameAtom);

return (
<div className="flex justify-center w-full items-center">
<input
type="text"
value={name}
value={inputTaskNameState}
onKeyDown={keyEvent}
onChange={(e) => setName(e.target.value)}
onChange={(e) => setInputTaskNameState(e.target.value)}
placeholder="Digite um nome..."
aria-label="inserir o nome da tarefa"
className={`
Expand Down
16 changes: 10 additions & 6 deletions todo-list/src/app/components/(input_estruture)/switch_button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import {useEffect, useState} from "react";
import MoonSvg from '../utility/svg_components/moon';
import SunSvg from '../utility/svg_components/sun';

import { useAtom } from "jotai";
// atoms
import { switchModeButtonAtom } from "@/app/(atoms)/(input)/input-atoms";

export const SwitchModeButton = () => {

const [currentState, setCurrentState] = useState('')
const [switchModeButtonState, setSwitchModeButtonState] = useAtom(switchModeButtonAtom);

useEffect(() => {
try {
if(window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.remove('light');
localStorage.ThemeStorage = 'dark'
setCurrentState('dark')
setSwitchModeButtonState('dark')
document.documentElement.classList.add(localStorage.ThemeStorage);
} else {
document.documentElement.classList.remove('dark');
localStorage.ThemeStorage= 'light'
setCurrentState('light')
setSwitchModeButtonState('light')
document.documentElement.classList.add(localStorage.ThemeStorage);
}

Expand All @@ -33,11 +37,11 @@ export const SwitchModeButton = () => {

if(newCurrentMode === 'dark') {
document.documentElement.classList.remove('light');
setCurrentState('dark')
setSwitchModeButtonState('dark')
document.documentElement.classList.add(localStorage.ThemeStorage);
} else {
document.documentElement.classList.remove('dark');
setCurrentState('light')
setSwitchModeButtonState('light')
document.documentElement.classList.add(localStorage.ThemeStorage);
}

Expand Down Expand Up @@ -67,7 +71,7 @@ export const SwitchModeButton = () => {
shadow-sm
w-4/5`}>
{
currentState === 'dark' ?
switchModeButtonState === 'dark' ?
<MoonSvg/>
:
<SunSvg/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputTask } from "../input"
import { InputTask } from "../../(components)/input"
export const MainContainerInputs = ({toggleSideBar}:{toggleSideBar:string}) => {
return (
<div className={`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tasks } from "../task"
import { Tasks } from "@/app/(components)/task"
import { ButtonsOfTasks } from "../(tasks_estruture)/buttons"

export const MainContainerTasks = ({blur}:{blur:boolean}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use client";
import { todoContext } from "../context";
import { useContext } from "react";
import RenameSVG from "../utility/svg_components/rename";
// hooks
import { useAtom } from "jotai";
Expand All @@ -12,7 +10,6 @@ interface TypeOfProps {
}

export const InputRename = ({ renameKey, addRename }: TypeOfProps) => {
const { rename, setRename } = useContext(todoContext);

const [renameState, setRenameState] = useAtom(renameStateAtom);

Expand Down
Loading

0 comments on commit 83f3fec

Please sign in to comment.