Skip to content

Commit

Permalink
Custom hooks for clicked value implementing
Browse files Browse the repository at this point in the history
  • Loading branch information
hacagahasanli committed Apr 5, 2023
1 parent 9edfe1c commit 17874fd
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
13 changes: 3 additions & 10 deletions src/components/KeyItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
capsClickHandler,
shiftClickHandler
} from "store/reducers"
import { useKeyClickedMethod } from "hooks";

interface IKeyClickedSelector {
keyClicked: {
Expand All @@ -18,23 +19,15 @@ interface IKeyClickedSelector {
}

export const KeyItem = ({ id, size, name, subName, c, img, hasImage }: IKeyItemType) => {
const dispatch = useDispatch()
const { capsClicked, shiftClicked } = useSelector((state: IKeyClickedSelector) => state.keyClicked)

const addValue = () => {
if (validValues.includes(name))
return dispatch(addClickedKeyValue({ name, capsClicked, shiftClicked }))
if (name === "backspace")
return dispatch(deleteValue())
if (id === "caps-lock")
return dispatch(capsClickHandler())
if (id === "shift1" || id === "shift2")
return dispatch(shiftClickHandler())
useKeyClickedMethod({ id, subName, name, capsClicked, shiftClicked })
}

const isUpperCasedValue = capsClicked && validValues.includes(name)

const isActive = (!(!subName) && shiftClicked)
const isActive = !(!subName) && shiftClicked

const currentScript = hasImage && img
? <Icon name={img} />
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useKeyClickedMethod } from "./useKeyClickedMethod";
26 changes: 26 additions & 0 deletions src/hooks/useKeyClickedMethod/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { validValues } from "constants/index"
import { useDispatch } from "react-redux"
import { addClickedKeyValue, capsClickHandler, deleteValue, shiftClickHandler } from "store/reducers"

interface IKeyClickedHook {
subName?: string | null;
name: string;
capsClicked: boolean;
shiftClicked: boolean;
id: string
}

export const useKeyClickedMethod = ({ id, subName, name, capsClicked, shiftClicked }: IKeyClickedHook) => {
const dispatch = useDispatch();

if (validValues.includes(name))
return dispatch(addClickedKeyValue({ subName, name, capsClicked, shiftClicked }))
if (id === "delete")
return dispatch(deleteValue())
if (id === "caps-lock")
return dispatch(capsClickHandler())
if (id === "shift1" || id === "shift2")
return dispatch(shiftClickHandler())


}
3 changes: 3 additions & 0 deletions src/store/reducers/keyClicked/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const KeyClickedSlice = createSlice({
},
shiftClickHandler: (state) => {
state.shiftClicked = !state.shiftClicked
// if (!state.shiftClicked) {
// state.capsClicked = true
// } else
state.capsClicked = !state.capsClicked
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/store/reducers/keyboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ const KeyboardSlice = createSlice({
state.allKeyValues = [...rowKeys];
},
addClickedKeyValue: (state: any, action: AddClickedKeyValueAction) => {
const { name, capsClicked, shiftClicked } = action.payload;
const { subName, name, capsClicked, shiftClicked } = action.payload;
const hasSubName = !(!subName);

if ((capsClicked || shiftClicked) && letters.includes(name)) {
const upperName = name.toUpperCase()
state.typedValue = [...state.typedValue, upperName]
} else
}
else if (shiftClicked && hasSubName)
state.typedValue.push(name)

else if (hasSubName)
state.typedValue = [...state.typedValue, subName]

else
state.typedValue = [...state.typedValue, name]
},
deleteValue: (state) => {
Expand Down
1 change: 1 addition & 0 deletions src/store/reducers/store-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export interface AddClickedKeyValueAction {
name: string;
capsClicked?: boolean;
shiftClicked?: boolean;
subName?: string | null;
};
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"types/*": [
"types/*"
],
"hooks/*": [
"hooks/*"
],
},
"target": "ESNext",
"useDefineForClassFields": true,
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineConfig({
store: "/src/store",
utils: "/src/utils",
types: "/src/types",
hooks: "/src/hooks",
}
},
})

0 comments on commit 17874fd

Please sign in to comment.