Skip to content

Instantly share code, notes, and snippets.

View mustafadalga's full-sized avatar

Mustafa Dalga mustafadalga

View GitHub Profile
@mustafadalga
mustafadalga / watch.vue-2.js
Last active April 12, 2024 16:48
How the Vue.js watch feature works in the hood
function watch(obj, prop, cb) {
const callback = arguments.length == 3 ? cb : prop;
if (arguments.length == 3) {
if (typeof obj[prop] === 'object') {
return watch(obj[prop], callback);
}
return defineProperty(obj, prop, callback);
@mustafadalga
mustafadalga / api.post.list.route.ts
Created September 11, 2023 11:30
Sending Cookie to API Route in Next.js 13:In a Next.js 13 project, you can send a cookie to an API route by including it in the headers of your Axios request in your server-side function. In the API route, you can access this cookie using request.cookies.get('cookieName').
import { NextRequest, NextResponse } from "next/server";
import { AxiosInstance } from "axios";
import { CookieKeys } from "@/_enums";
import createAuthenticatedApiServiceV2 from "@/_services/createAuthenticatedApiServiceV2";
import handleAxiosError from "@/_utilities/handleAxiosError";
export async function GET(request: NextRequest) {
try {
const jwt: string | undefined = request.cookies.get(CookieKeys.Jwt)?.value;
@mustafadalga
mustafadalga / App.tsx
Created April 1, 2024 20:35
Modal Transition Effect in React.js
import { lazy, Suspense, useEffect } from "react";
import useModalStore from "@/hooks/useModalStore.ts";
const Modal = lazy(() => import("@/components/Modal.tsx"))
function Loading() {
return <h2>🌀 Loading...</h2>;
}
function App() {
@mustafadalga
mustafadalga / memo.ts
Created March 31, 2024 16:10
Custom Memoization Function in JavaScript
/**
* This Gist demonstrates a simple yet effective implementation of a memoization function in JavaScript.
* Memoization is an optimization technique used to speed up computer programs by storing the results of expensive function
* calls and returning the cached result when the same inputs occur again.
*/
const memoMap = new Map();
const complexFunction = (a1: number[], a2: number[]): number => {
// Simulate heavy calculations
@mustafadalga
mustafadalga / App.vue
Last active March 17, 2024 09:56
Modal Fade In / Fade Out Animation with Transition in vue.js 3
<template>
<div class="app">
<button @click="store.mutations.open()">Open Modal </button>
<Modal v-if="store.state.isModalOpen"/>
</div>
</template>
<script setup>
import store from "./store";
import Modal from "./Modal.vue";
@mustafadalga
mustafadalga / App.vue
Created March 17, 2024 09:55
Modal Slide In / Up Animation with Transition in vue.js 3
<template>
<div class="app">
<button @click="store.mutations.open()">Open Modal </button>
<Modal v-if="store.state.isModalOpen"/>
</div>
</template>
<script setup>
import store from "./store";
import Modal from "./Modal.vue";
@mustafadalga
mustafadalga / store.ts
Created January 8, 2024 10:36
Unit Testing Vue.js Composables: Verifying Vuex Store Interaction and Reactive Behavior in useCheckUserPosts
import { createStore } from "vuex";
import type { Post } from "@/types";
export interface ApisLoadState {
isCalled: boolean;
isLoaded: boolean;
}
export interface ApiLoadState extends ApisLoadState {
api: string;
@mustafadalga
mustafadalga / amplitude.d.ts
Last active December 17, 2023 14:35
Amplitude Type Declaration
declare global {
interface AmplitudeInstance {
logEvent(eventName: string, eventProperties?: object): void;
identify(identifyObject: object): void;
setUserId(userId: string | null): void;
init(apiKey: string, userId?: string, options?: object, callback?: () => void): void;
}
interface Amplitude {
getInstance(): AmplitudeInstance;
@mustafadalga
mustafadalga / useFetchPosts.test.ts
Created November 18, 2023 11:17
Unit Testing React Hooks with MSW, Vitest, and Testing Library: Fetching and Updating Posts
import { expect, describe, it, beforeAll, afterEach, afterAll, beforeEach } from "vitest";
import { QueryClientProvider } from "react-query";
import { ReactNode } from "react";
import { http, HttpResponse } from "msw";
import { useCommonStore } from "@/_store/useCommonStore";
import { act, renderHook, waitFor } from "@testing-library/react";
import { createTestServer, queryClient } from "@/__tests__/utilities";
import useFetchCustomVariables from "@/_hooks/useFetchCustomVariables";
const successResponse = [
@mustafadalga
mustafadalga / useDeepCompareMemoize.test.ts
Created November 18, 2023 09:24
Unit Tests for useDeepCompareMemoize: Testing Deep Comparison React Hook with Vitest and Testing Library
import { describe, it, expect } from "vitest";
import { renderHook } from "@testing-library/react";
import useDeepCompareMemoize from "./useDeepCompareMemoize";
describe("useDeepCompareMemoize", () => {
it('should return a memoized version of the input value based on deep comparison', () => {
const initialData = { user: { name: 'John', age: 25, city: 'New York' } };
const newData = { user: { name: 'Alice', age: 30, city: 'San Francisco' } };
const identicalData = { user: { name: 'John', age: 25, city: 'New York' } };