Skip to content

Instantly share code, notes, and snippets.

View nikasepiskveradze's full-sized avatar

Nika Sepiskveradze nikasepiskveradze

View GitHub Profile
@nikasepiskveradze
nikasepiskveradze / auth-context.js
Created November 20, 2021 21:36
auth provider simple skeleton
import * as React from 'react'
import {FullPageSpinner} from '~/components/lib'
const AuthContext = React.createContext()
function AuthProvider(props) {
// code for pre-loading the user's information if we have their token in
// localStorage goes here
// 🚨 this is the important bit.
@nikasepiskveradze
nikasepiskveradze / react-query-lite.js
Last active March 11, 2024 17:45
Lightweight implementation of React Query
import React from "react";
const context = React.createContext();
export function QueryClientProvider({ children, client }) {
React.useEffect(() => {
const onFocus = () => {
client.queries.forEach((query) => {
query.subscribers.forEach((subscriber) => {
subscriber.fetch();
@nikasepiskveradze
nikasepiskveradze / PrivateRoute.js
Created June 15, 2021 21:45
PrivateRoute Demo
export const PrivateRoute = ({ component: Component, roles, ...rest }) => (
<Route {...rest} render={props => {
const currentUser = authenticationService.currentUserValue;
if (!currentUser) {
// not logged in so redirect to login page with the return url
return <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
}
// check if route is restricted by role
if (roles && roles.indexOf(currentUser.role) === -1) {
@nikasepiskveradze
nikasepiskveradze / http.ts
Created January 8, 2021 17:06
Http client example with TS and RXJs
import {Observable, of} from "rxjs";
import {catchError, map} from "rxjs/operators";
import {ajax} from "rxjs/ajax";
class Http {
public get<T extends Object = Object>(url: string, headers?: any): Observable<T> {
return ajax.get.apply(undefined, [`${url}`, headers]).pipe(catchError(this.catchError), map(this.map));
}
public post<T extends Object = Object>(url: string, body?: any, headers?: any): Observable<T> {
import { useCallback, useState } from 'react';
// Usage
function App() {
// Call the hook which returns, current value and the toggler function
const [isTextChanged, setIsTextChanged] = useToggle();
return (
<button onClick={setIsTextChanged}>{isTextChanged ? 'Toggled' : 'Click to Toggle'}</button>