Skip to content

Instantly share code, notes, and snippets.

View safronman's full-sized avatar
🚀

safronman safronman

🚀
View GitHub Profile
@safronman
safronman / scrollToBottom.js
Last active November 15, 2023 08:36
Плавная прокрутка сайта
// Функция для прокрутки страницы
function scrollToBottom() {
// Получаем высоту документа
var documentHeight = document.documentElement.scrollHeight;
// Получаем текущую позицию прокрутки
var currentScroll = window.scrollY;
// Устанавливаем новую позицию прокрутки
var newScroll = currentScroll + 1; // Измените значение 1 на любое другое, чтобы регулировать скорость прокрутки
@safronman
safronman / slice.ts
Last active February 19, 2023 14:45
thunkTryCatch
export const changeCourseId = createAsyncThunk(
'auth/change-course',
async (payload: PayloadType, thunkAPI) => {
return thunkTryCatch(thunkAPI, async () => {
await packsApi.put(payload)
})
}
)
@safronman
safronman / useActions.ts
Last active February 19, 2023 14:45
use action hook
// 🔰 Don't write dispacth in componets. Instead of useAppDispatch use useActionsHook
// 🔗 https://medium.com/@d.maklygin/redux-typescript-reuse-the-type-of-an-action-creators-return-value-91663a48858f
import { useMemo } from 'react'
import { ActionCreatorsMapObject, bindActionCreators } from 'redux'
import { useAppDispatch } from './react-redux-hooks'
type IsValidArg<T> = T extends object ? (keyof T extends never ? false : true) : true
@safronman
safronman / Avatar.tsx
Last active July 13, 2022 09:31
Conver file to base64 and upload
const uploadHandler = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files.length) {
const file = e.target.files[0]
if (file.size < 4000000) {
convertFileToBase64(file, (file64: string) => {
dispatch(updateProfileTC({avatar: file64}))
})
} else {
dispatch(setAppErrorAC('Файл слишком большого размера'))
}
@safronman
safronman / error-utils.ts
Created July 1, 2022 11:34
axios catch TS error
import { setAppErrorAC, SetAppErrorActionType } from '../../app/app-reducer';
import { Dispatch } from 'redux';
import axios, { AxiosError } from 'axios';
import { AppThunk } from '../../app/store';
import { packsAPI } from '../../api/packs-api';
export const errorUtils = (e: Error | AxiosError<{error: string}>, dispatch: Dispatch<SetAppErrorActionType>) => {
const err = e as Error | AxiosError<{ error: string }>
if (axios.isAxiosError(err)) {
const error = err.response?.data ? err.response.data.error : err.message
@safronman
safronman / ReactFC.ts
Last active August 16, 2022 17:25
RFCMemo
import React, { FC } from 'react';
type PropsType = {
//
}
export const Component: FC<PropsType> = React.memo(() => {
return (
<>Component</>
)
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import { AppDispatch, RootState } from '../../app/store'
export const useAppDispatch: () => AppDispatch = useDispatch
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
import { createSlice } from '@reduxjs/toolkit'
export const hostAppSlice = createSlice({
name: 'host-app',
initialState: {
value: false,
},
reducers: {
changeValue: (state) => {
state.value = true
@safronman
safronman / Кастомный хук useFetch .jsx
Last active April 19, 2020 12:26
Кастомный хук useFetch
import {useState, useEffect} from 'react'
import axios from 'axios'
export default url => {
const baseUrl = 'https://conduit.productionready.io/api'
const [isLoading, setIsLoading] = useState(false)
const [response, setResponse] = useState(null)
const [error, setError] = useState(null)
const [options, setOptions] = useState({})
@safronman
safronman / gist:d7eee9519aade5d7235e3897a507a2a9
Last active April 19, 2020 12:25
Кастомный хук useFetxh (использование из компоненты)
import React, {useState, useEffect} from 'react'
import {Link} from 'react-router-dom'
import axios from 'axios'
import useFetch from 'hooks/useFetch'
const Authentication = () => {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [{isLoading, error, response}, doFetch] = useFetch('/users/login')