Skip to content

Instantly share code, notes, and snippets.

View oleh-zaporozhets's full-sized avatar

Oleh Zaporozhets oleh-zaporozhets

View GitHub Profile
class MainApi extends HttpClient {
private static classInstance?: MainApi;
private constructor() {
super('https://api.awesome-site.com');
}
public static getInstance() {
if (!this.classInstance) {
this.classInstance = new MainApi();
class MainApi extends HttpClient {
private static classInstance?: MainApi;
private constructor() {
super('https://api.awesome-site.com');
}
...
}
class MainApi extends HttpClient {
private constructor() {
super('https://api.awesome-site.com');
}
...
}
import { useState } from 'react';
interface Status {
pending: boolean;
resolve: boolean;
reject: boolean;
}
interface ReturnStatus<T> extends Status {
data: T | null;
import { useEffect } from 'react';
export default () => {
useEffect(() => {
const initialOverflow = window.getComputedStyle(document.body).overflow;
document.body.style.overflow = 'hidden';
return () => {
document.body.style.overflow = initialOverflow;
import { useEffect } from 'react';
type PassedElement = Element | Window;
type UseEventListener = (h: EventListener, e: string, el?: PassedElement) => void;
const useEventListener: UseEventListener = (handler, event, element = window) => {
useEffect(() => {
const isSupported = element && element.addEventListener;
import { useState } from 'react';
type HandleToggle = (value: boolean | React.MouseEvent) => void;
export default (defaultValue = false): [boolean, HandleToggle] => {
const [isOn, toggle] = useState<boolean>(defaultValue);
const handleToggle: HandleToggle = (value) => {
const toggleState = typeof value === 'boolean' ? value : !isOn;
import { useState } from 'react';
export type ChangeEvent = React.ChangeEvent<HTMLInputElement>;
export type OnChange = (event: ChangeEvent | string) => void;
export default (defaultValue = ''): [string, OnChange] => {
const [value, setValue] = useState<string>(defaultValue);
const onChange: OnChange = (data) => {
const value = typeof data === 'string' ? data : data.target.value;
import { AxiosRequestConfig } from 'axios';
import { CreateUserBody } from './types';
class MainApiProtected extends HttpClient {
public constructor() {
super('https://api.awesome-site.com');
this._initializeRequestInterceptor();
}
import HttpClient from './http-client';
import { User } from './types';
class MainApi extends HttpClient {
public constructor() {
super('https://api.awesome-site.com');
}
public getUsers = () => this.instance.get<User[]>('/users');