Skip to content

Instantly share code, notes, and snippets.

View ejangi's full-sized avatar
😎
Totally addicted to Docker.

James ejangi

😎
Totally addicted to Docker.
View GitHub Profile
.fluid-type {
font-size: clamp(1rem, 4cqi, 3rem);
}
// Using a reducer allows you to control field validation etc
// without having a million useState(); calls and logic spanning across multiple functions.
import { useReducer } from "react";
function EditCalendarEvent() {
const [event, updateEvent] = useReducer(
(prev, next) => {
const newEvent = { ...prev, ...next };
<script runat="server">
// You can POST to this endpoint in order to quickly subscribe someone to a Data Extension.
// The request body expects a JSON object with a property labelled "de" that uses the Marketing Cloud
// Data Extension ID (e.g. 6ded84cc-9fbf-4ec5-83e3-06cc82fb380a)
// The rest of the JSON object is just key/value pairs for each field name and field value.
//
// Example:
// POST /subscribe
// {"de":"6ded84cc-9fbf-4ec5-83e3-06cc82fb380a","FirstName": "Joe","LastName":"Bloggs","Email":"[email protected]"}
// 1) Use the Cloudlflare Pages/Github integration to build everytime there is a push to the main branch.
// 2) In github settings for the repo, create a new webhook that only triggers on "Check runs".
@ejangi
ejangi / react-complex-forms.js
Created June 25, 2021 22:42
How to deal with large form state in a React app.
import * as React from "react";
import { render } from "react-dom";
import produce from "immer";
import { set, has } from "lodash";
import "./styles.css";
function enhancedReducer(state, updateArg) {
// check if the type of update argument is a callback function
if (updateArg.constructor === Function) {
@ejangi
ejangi / modal.js
Created June 24, 2021 23:38
An example of exposing component methods to parent component in React and Next JS
import { useState, useRef, useEffect, forwardRef, useImperativeHandle } from 'react';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
/*
USAGE:
@ejangi
ejangi / useUserContext.js
Created June 24, 2021 03:03
Next JS UserContext and UserProvider example of using React useContext
import { createContext, useState } from 'react';
//create a context, with createContext api
export const userContext = createContext();
const UserProvider = (props) => {
// this state will be shared with all components
const [loggedin, setLoggined] = useState(false);
return (
@ejangi
ejangi / _document.js
Created June 24, 2021 02:58
Next JS server side styles rendering
import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets } from '@material-ui/core/styles';
import theme from '../styles/theme';
// This makes sure that Material UI renders correctly on the server side as well
// See https://dev.to/felixmohr/setting-up-a-blog-with-next-js-react-material-ui-and-typescript-2m6k
export default class MyDocument extends Document {
render() {
@ejangi
ejangi / react-state-class.js
Last active June 19, 2021 05:08
No need to work with useState() when you have a react component class.
export default class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
render() {
return (
@ejangi
ejangi / bigquery-lag-window-partition.sql
Created June 16, 2021 23:08
Use the LAG() function to include details about the previous record in a series.
SELECT
ocr.ContactId,
ocr.OpportunityCloseDate,
ocr.OpportunityAmount,
LAG(ocr.OpportunityCloseDate, 1) OVER (PARTITION BY ocr.ContactId ORDER BY ocr.OpportunityCloseDate ASC) AS `Prev`
FROM
`OpportunityContactRoles` AS ocr
WHERE
ocr.OpportunityAmount > 0
ORDER BY