Skip to content

Instantly share code, notes, and snippets.

View DonHuskini's full-sized avatar
🏠
Working from home

Don Huskini DonHuskini

🏠
Working from home
View GitHub Profile
@DonHuskini
DonHuskini / test-swr-data-transformation.tsx
Last active October 16, 2022 13:29
Test SWR data transformations
import * as React from "react";
import useSWR, { SWRConfig } from "swr";
import { ErrorBoundary } from "react-error-boundary";
import axios from "axios";
// grab it with Swagger instead of manually write
interface PokemonDTO {
abilities: {}[];
base_experience: number;
forms: {}[];
@DonHuskini
DonHuskini / errorHandlingWithTS.ts
Last active October 13, 2022 13:11
Handle error with TypeScript
interface Pokemon {
id: number;
name: string;
height: number;
types: { id: number; name: string }[];
}
type GetPokemonSuccessResult = { isOk: true; data: Pokemon; error: null };
type GetPokemonErrorResult = { isOk: false; data: null; error: string };
type GetPokemonResult = GetPokemonSuccessResult | GetPokemonErrorResult;
@DonHuskini
DonHuskini / usePokemonDetails.ts
Created October 12, 2022 02:50
usePokemonDetails custom hook
import { useEffect, useState } from "react";
import { getPokemonDetails, PokemonDetails } from "services/pokemon";
export const usePokemon = (id: PokemonDetails["id"]) => {
const [data, setData] = useState<PokemonDetails>();
const [error, setError] = useState<string>();
useEffect(() => {
(async () => {
const { isOk, data, error } = await getPokemonDetails(id);
@DonHuskini
DonHuskini / typings.ts
Last active October 12, 2022 03:18
How to type Async service function response
// for sake of simplicity, I'm adding all things in this file but
// I recommend spliting them into their respective files as suggested
const url = 'https://pokeapi.co/api/v2'
const getIdFromUrl = (url: string) => {
const splitted = url.split("/");
const id = +splitted[splitted.length - 2];
return id;
};
@DonHuskini
DonHuskini / usePokemon.ts
Created October 11, 2022 18:43
usePokemon hook
import { useEffect, useState } from "react";
import { getPokemon, PokemonDetails } from "services/pokemon";
var Cache: { pokemon?: { data: PokemonDetails[]; storedAt: number } } = {};
export const usePokemon = () => {
const [data, setData] = useState<PokemonDetails[]>();
const [error, setError] = useState<string>();
useEffect(() => {
large desktop 1920
small laptop 1280
large tablet 1024
small tablet 768
mobile 410
small mobile 360
@DonHuskini
DonHuskini / gist:10148dbe6cc4987504a4281fdcf49c44
Created August 11, 2022 16:34
Object keys from array values
const pathIds = ["p1", "p2", "p3", "p4", "p5"] as const
function Fn() {
let data: { [K in typeof pathIds[number]]?: { x: number, y: number } } = {}
data['p1'] = {
x: 10,
y: 20
}
}
1. Add the font on the /public folder. Just .woff2 extension is good enough https://caniuse.com/?search=woff2
1.1. If you have a problem regarding vertical alignment, use https://transfonter.org/ and select "fix vertical metrics"
2. Import the font with CSS @font-face like below:
```css
@font-face {
font-family: 'Bebas Neue';
src: url('/fonts/BebasNeue/BebasNeue-Regular.woff2') format('woff2');
font-weight: 400;
font-display: swap;
}
@DonHuskini
DonHuskini / cool.js
Last active August 5, 2022 02:47
GraphQL with Vanilla JavaScript
const api = axios.create({
baseURL: "xxx/graphql",
method: "POST",
headers: { "Content-Type": "application/json" },
});
const CategoriesQuery = (first: number, after: string) =>
JSON.stringify({
query: /* GraphQL */ `
query Categories($first: Int!, $after: String!) {
@DonHuskini
DonHuskini / mock-user.js
Created July 19, 2022 19:24
User following/followers
class User {
id = undefined;
username = undefined;
following = [];
followers = [];
constructor({ id, username }) {
this.id = id;
this.username = username;
}