Added gitignore
This commit is contained in:
48
vidow-front/src/app/hooks.ts
Normal file
48
vidow-front/src/app/hooks.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { ChangeEvent } from 'react'
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
|
||||
|
||||
import type { AppDispatch, AppState } from './store'
|
||||
|
||||
export const useForm =
|
||||
<TContent>(defaultValues: TContent) =>
|
||||
(handler: (content: TContent) => void) =>
|
||||
async (event: ChangeEvent<HTMLFormElement>) => {
|
||||
event.preventDefault()
|
||||
event.persist()
|
||||
|
||||
const form = event.target as HTMLFormElement
|
||||
const elements = Array.from(form.elements) as HTMLInputElement[]
|
||||
const data = elements
|
||||
.filter((element) => element.hasAttribute('name'))
|
||||
.reduce(
|
||||
(object, element) => ({
|
||||
...object,
|
||||
[`${element.getAttribute('name')}`]: element.value,
|
||||
}),
|
||||
defaultValues
|
||||
)
|
||||
await handler(data)
|
||||
form.reset()
|
||||
}
|
||||
|
||||
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
|
||||
export const useInterval = (callback: Function, delay: number) => {
|
||||
const savedCallback = useRef<Function>()
|
||||
useEffect(() => {
|
||||
savedCallback.current = callback
|
||||
}, [callback])
|
||||
useEffect(() => {
|
||||
const handler = (...args: any) => savedCallback.current?.(...args)
|
||||
|
||||
if (delay !== null) {
|
||||
const id = setInterval(handler, delay)
|
||||
return () => clearInterval(id)
|
||||
}
|
||||
}, [delay])
|
||||
}
|
||||
|
||||
// Use throughout your app instead of plain `useDispatch` and `useSelector`
|
||||
export const useAppDispatch = () => useDispatch<AppDispatch>()
|
||||
|
||||
export const useAppSelector: TypedUseSelectorHook<AppState> = useSelector
|
24
vidow-front/src/app/store.ts
Normal file
24
vidow-front/src/app/store.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit'
|
||||
|
||||
import counterReducer from '../features/counter/counterSlice'
|
||||
|
||||
export function makeStore() {
|
||||
return configureStore({
|
||||
reducer: { counter: counterReducer },
|
||||
})
|
||||
}
|
||||
|
||||
const store = makeStore()
|
||||
|
||||
export type AppState = ReturnType<typeof store.getState>
|
||||
|
||||
export type AppDispatch = typeof store.dispatch
|
||||
|
||||
export type AppThunk<ReturnType = void> = ThunkAction<
|
||||
ReturnType,
|
||||
AppState,
|
||||
unknown,
|
||||
Action<string>
|
||||
>
|
||||
|
||||
export default store
|
Reference in New Issue
Block a user