Skip to content

Instantly share code, notes, and snippets.

View heytulsiprasad's full-sized avatar
⚛️
Overreacting

Tulsi Prasad heytulsiprasad

⚛️
Overreacting
View GitHub Profile
@heytulsiprasad
heytulsiprasad / photos.jsx
Created August 14, 2020 21:22
Using gatsby-images
import React from "react";
import { graphql } from "gatsby";
import Img from "gatsby-image";
function photos({ data }) {
console.log(data.images.nodes);
return (
<div>
<h1>All My Photos</h1>
@heytulsiprasad
heytulsiprasad / Arrow.jsx
Last active August 5, 2020 20:39
A svg turned into react component with added keyframes for continuous animations.
import React from "react";
import styled, { keyframes } from "styled-components";
const slide = keyframes`
from {
transform: translateY(0);
}
to {
transform: translateY(5px);
@heytulsiprasad
heytulsiprasad / .prettierrc
Created July 25, 2020 22:47
Common prettier config file
{
"useTabs": false, // Indent lines with tabs instead of spaces.
"printWidth": 80, // Specify the length of line that the printer will wrap on.
"tabWidth": 2, // Specify the number of spaces per indentation-level.
"singleQuote": false, // Use single quotes instead of double quotes.
/**
* Print trailing commas wherever possible.
* Valid options:
* - "none" - no trailing commas
* - "es5" - trailing commas where valid in ES5 (objects, arrays, etc)
@heytulsiprasad
heytulsiprasad / gist.md
Last active July 25, 2020 19:51
Differentiate vs good and bad code?

Logic

Tip 1

Using else if only when specifically required. Try to see if only else could solve it.

Tip 2

Think of what types the function returns, do they satisfy your needs? Otherwise, coerce them.

@heytulsiprasad
heytulsiprasad / monId.md
Last active July 25, 2020 20:02
Validate mongo id from req.param.id

Check string for mongo id

if (id.match(/^[0-9a-fA-F]{24}$/)) {
  // Yes, it's a valid ObjectId, proceed with `findById` call.
}

Why do I need this?

@heytulsiprasad
heytulsiprasad / Post.js
Last active July 26, 2020 08:18
A comprehensive user profile mongoose model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users",
},
text: {
@heytulsiprasad
heytulsiprasad / register.js
Last active July 24, 2020 17:43
Using validator in express backend
const Validator = require("validator");
const isEmpty = require("./is-empty");
// we cant use isEmpty from validator pkg as it needs its param to be a string
// data is req.body
module.exports = function validateRegisterInput(data) {
let errors = {};
data.name = isEmpty(data.name) ? "" : data.name;
data.email = isEmpty(data.email) ? "" : data.email;
@heytulsiprasad
heytulsiprasad / passport.js
Last active July 23, 2020 18:25
How to create a passport jwt strategy?
// Creating a passport strategy
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const mongoose = require("mongoose");
// Get the user model
const User = mongoose.model("users");
const keys = require("../config/keys");
const opts = {};
@heytulsiprasad
heytulsiprasad / react-local-setup.md
Last active July 23, 2020 07:19
Error I faced while setting up reactjs.org locally.
ERROR #85923  GRAPHQL

There was an error in your GraphQL query:

Cannot query field "allExampleCode" on type "Query".

If you don't expect "allExampleCode" to exist on the type "Query" it is most likely a typo.
However, if you expect "allExampleCode" to exist there are a couple of solutions to common problems:
@heytulsiprasad
heytulsiprasad / copyToClip.js
Last active July 22, 2020 09:23
How to copy to clipboard and paste?
import React, { Component, createRef } from "react";
class Input extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
copyMsg: "Copy to Clipboard",
isCopied: false,
};