Skip to content

Instantly share code, notes, and snippets.

View Sambalicious's full-sized avatar

Samuel Ayegbusi Sambalicious

View GitHub Profile
@Sambalicious
Sambalicious / helpers.ts
Created December 9, 2023 15:27
DTO Mapper to decouple frontend naming conventions from backend.
export const mapResponseToDTO = <T, U>(
responseDTO: U,
propertyMappings?: Record<string, keyof T>
): T => {
// Create an empty object that will hold the mapped DTO
const mappedDTO: Partial<T> = {};
// Loop through each property in the responseDTO
for (const key in responseDTO) {
// Check if propertyMappings exist and if the current key is in propertyMappings
@Sambalicious
Sambalicious / FileUpload.tsx
Created August 28, 2023 13:42
A reusable file upload component built with react and react-hook-form
import { PropsWithChildren, useRef } from "react";
import { UseFormRegisterReturn } from "react-hook-form";
type FileUploadProps = {
register: UseFormRegisterReturn;
accept?: string;
};
export const FileUpload = (props:PropsWithChildren<FileUploadProps>) => {
const { register, accept, children } = props;
import html2canvas from "html2canvas";
import { jsPDF } from "jspdf";
import { useState } from "react";
interface DownloadPDFProps {
docName: string;
id: string;
width?: number;
height?: number;
}
import { useEffect, useState } from "react";
export const useDebounceValue = <T>(value: T, delay: number) => {
const [debouncedValue, setDebounceValue] = useState<T>(value);
useEffect(() => {
const timer = setTimeout(() => {
setDebounceValue(value);
}, delay);
@Sambalicious
Sambalicious / helper.ts
Created January 14, 2023 05:50
Reusable error handler
export function deleteCookie(name: string) {
if (typeof window !== "undefined") {
const date = new Date();
// Set it expire in -1 days
date.setTime(date.getTime() + -1 * 24 * 60 * 60 * 1000);
// Set it
document.cookie = name + "=; expires=" + date.toUTCString() + "; path=/";
}
}
@Sambalicious
Sambalicious / helper.ts
Created January 14, 2023 05:48
Server side authentication function
export function requireAuthentication<
P extends Record<string, any> = Record<string, any>,
Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData,
>(gssp: GetServerSideProps<P, Q, D>): GetServerSideProps<P, Q, D> {
return async context => {
const { req } = context;
const token = req?.cookies?.b2bToken;
if (!token) {
@Sambalicious
Sambalicious / helper.ts
Created January 14, 2023 05:45
Local storage helper configuration
export const storage = {
get: function (key: string) {
try {
let response = localStorage.getItem(key);
return response ? JSON.parse(response) : null;
} catch (error) {
console.log(error);
}
},
post: <T>(key: string, value: T) => {