Skip to content

Instantly share code, notes, and snippets.

View akshatmittal61's full-sized avatar
🎃
Pushing Bugs into production

Akshat Mittal akshatmittal61

🎃
Pushing Bugs into production
View GitHub Profile
@akshatmittal61
akshatmittal61 / parser.ts
Created May 29, 2024 09:09
Utility to parse Mongoose response to JS object
export const getObjectFromMongoResponse = <T>(response: any): T | null => {
if (response === null || response === undefined) {
return null;
}
const object = response.toObject ? response.toObject() : response;
// eslint-disable-next-line no-unused-vars
const { _id, __v, createdAt, updatedAt, ...rest } = object;
const data = {
id: (_id ?? object.id).toString(),
@akshatmittal61
akshatmittal61 / device.tsx
Created October 26, 2023 09:34
Custom Hook for Device description
import { useEffect, useState } from "react";
type DeviceType = "mobile" | "tablet" | "desktop";
type ScreenOrientationType = "portrait" | "landscape";
type PlatformType = "client" | "server";
const useDevice = () => {
const [device, setDevice] = useState<DeviceType>("desktop");
const [screenOrientation, setScreenOrientation] =
useState<ScreenOrientationType>("landscape");
@akshatmittal61
akshatmittal61 / FileName.ts
Created July 25, 2023 04:13
Create a new file with similar name with ReGeX
if (cloned) {
if (files.find((file: any) => file.title === newFileTitle)) {
const titleRegex = new RegExp(`${newFileTitle} \\((\\d+)\\)`);
const similarFiles = files.filter((file: any) =>
titleRegex.test(file.title)
);
if (similarFiles.length > 0) {
const lastFile = similarFiles[similarFiles.length - 1];
const lastFileTitle = lastFile.title;
const lastFileTitleMatch = lastFileTitle.match(titleRegex);
@akshatmittal61
akshatmittal61 / README.md
Last active September 18, 2022 03:49
This is a mixin definition for a responsive layout in SASS (SCSS). Import this mixin anywhere in your sass files for responsive layout by replacing redundant media queries.

Usage

To use this mixin in your sass file:

Let's assume that your sass file is on the same directory level as this file,

@import "responsive-mixin"

.container{
@akshatmittal61
akshatmittal61 / PrivateRoute.jsx
Created April 27, 2022 18:33
This is a React Private Route component for enabling auth protection on a react project which is using React Router DOM v6. On accessing the Private Route, if the user is not authneticated then the page will be navigated to the login route.
import React, { useContext } from "react";
import { Navigate } from "react-router-dom";
import GlobalContext from "./Context/GloablContext";
const PrivateRoute = ({ children }) => {
const { isAuthenticated, isLoading } = useContext(GlobalContext);
if (!isLoading) {
if (isAuthenticated) return children;
else return <Navigate to="/login" />;
}