event.ts 798 Bytes
import renderApp from "@src/App";
import {
  getElement,
  pendingFetch,
  warningEmptyId,
  fetchByURL,
} from "./functions";
import { randomTransition } from "./page";

export function linkEvent(e: Event) {
  e.preventDefault();

  const pathname: string = (e.target as HTMLAnchorElement).pathname.split(
    "/"
  )[1];

  setTimeout(() => {
    renderApp(pathname);
  }, 1200);

  randomTransition();
}

export async function handleFetchById(url: string, cssSelector: string) {
  const insta_id = (getElement(cssSelector) as HTMLInputElement).value;

  pendingFetch();

  if (!insta_id) {
    warningEmptyId();
    return;
  }

  await fetchByURL(url + insta_id);
}

export const addEventWithElementByType = (element, eventType, handler) => {
  element.addEventListener(eventType, handler);
};