Skip to content

Instantly share code, notes, and snippets.

View borispoehland's full-sized avatar

Boris Pöhland borispoehland

View GitHub Profile
fetchCsv() {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", require("path/to/local/file.txt"), false);
rawFile.onreadystatechange = () => {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status === 0) {
var allText = rawFile.responseText;
Papa.parse(allText, {
complete: this.setData, // in this case, set the text as react state
import React, {
useState,
useLayoutEffect
} from 'react';
import ReactDOM from 'react-dom';
const BlinkyRender = () => {
const [value, setValue] = useState(0);
useLayoutEffect(() => {
import { useEffect, useState } from 'react';
interface IProps {
imagesToLoad: string[],
}
const useImagesAreLoaded = ({ imagesToLoad }: IProps): boolean => {
const [loadedTilesCount, setLoadedTilesCount] = useState(0);
useEffect((): void => {
@borispoehland
borispoehland / withLoadingSpinner.js
Last active February 18, 2021 14:15
Sample HOC for React Typescript
import React, { FC, FunctionComponent } from 'react';
import { LoadingSpinner } from '@components/LoadingSpinner';
interface IProps {
isLoading: boolean;
}
// eslint-disable-next-line @typescript-eslint/ban-types, react/display-name
const WithLoadingSpinner = <P extends object>(Component: FunctionComponent<P>): FC<P & IProps> => ({
isLoading,
export const getTopY = (element: HTMLElement, downOffset: number): number => {
return element.getBoundingClientRect().top + window.pageYOffset - downOffset;
};
const UA = req.headers['user-agent'];
const isMobile = Boolean(UA.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i));
{
"version": 2,
"builds": [
{
"src": "src/app.js",
"use": "@vercel/node"
}
],
"routes": [
{
@borispoehland
borispoehland / effects.ts
Last active May 7, 2024 09:48
3 fetch effects for effect-ts. One uses cache: no-store, one cache: force-cache and one uses the default cache / revalidate
import * as S from '@effect/schema/Schema'
import { Context, Effect } from 'effect'
export interface IFetcher {
fetch: typeof fetch
}
export const TFetcher = Context.Tag<IFetcher>('IFetcher')
class FreshFetcher implements IFetcher {
async function getAccessToken() {
return Effect.runPromise(
fetchFresh<{ access_token: string }>(
null,
'https://accounts.spotify.com/api/token',
{
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(
`${process.env.SPOTIFY_CLIENT_ID}:${process.env.SPOTIFY_CLIENT_SECRET}`
const getAccessToken = fetchFresh<{ access_token: string }>(
null,
'https://accounts.spotify.com/api/token',
{
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(
`${process.env.SPOTIFY_CLIENT_ID}:${process.env.SPOTIFY_CLIENT_SECRET}`
).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',