Skip to content

Instantly share code, notes, and snippets.

View AugustoCalaca's full-sized avatar

Augusto Calaca AugustoCalaca

View GitHub Profile
@sibelius
sibelius / richTextToRelayRecordProxy.tsx
Created September 17, 2020 22:01
Transform an object to a Relay Record Proxy
export const richTextToRelayRecordProxy = (bodyRichText, store) => {
const bodyRichTextId = 'client:RichText:' + tempID++;
const bodyRichTextRecord = store.create(bodyRichTextId, 'RichText');
bodyRichTextRecord.setValue(bodyRichText.text, 'text');
bodyRichTextRecord.setValue(bodyRichText.html, 'html');
const { contentState } = bodyRichText;
if (contentState) {
const contentStateId = 'client:DraftContentState:' + tempID++;
@sibelius
sibelius / thalesTheorem.ts
Created July 22, 2020 20:51
Thales theorem function and inverted
/*
a = minProgress
b = minResult
c = maxProgress
d = maxResult
x = result
y = progress
y = (-ad + ax + bc - cx) / (b - d)
@sibelius
sibelius / webpack.server.js
Created July 14, 2020 14:59
Webpack config for server
const path = require('path');
const webpack = require('webpack');
const WebpackNodeExternals = require('webpack-node-externals');
const ReloadServerPlugin = require('reload-server-webpack-plugin');
const cwd = process.cwd();
module.exports = {
@sibelius
sibelius / UserEditNameMutation.ts
Created May 18, 2020 14:53
createEditSingleFieldMutation function that generate an edit single field mutation for a given model
const mutation = createEditSingleFieldMutation({
name: 'UserEditName',
model: User,
fieldName: 'title',
fieldType: GraphQLString,
clearCache: UserLoader.clearCache,
notFoundMessage: ({ t }) => t('User not found'),
successMessage: ({ t }) => t('User edited successfully'),
outputFields: {
user: {
@sibelius
sibelius / initEnvironment.tsx
Created May 15, 2020 13:55
initEnvironment to be used for SSR
export const initEnvironment = (records = {}) => {
const network = Network.create(cacheHandler);
const source = new RecordSource(records);
const store = new Store(source, {
// This property tells Relay to not immediately clear its cache when the user
// navigates around the app. Relay will hold onto the specified number of
// query results, allowing the user to return to recently visited pages
// and reusing cached data if its available/fresh.
gcReleaseBufferSize: 10,
@sibelius
sibelius / AutocompleteRelay.tsx
Last active March 23, 2024 10:13
@material-ui Autocomplete lab with react-window + infinite-loader for GraphQL/Relay connections
import React, { useRef, useState } from 'react';
import { Typography } from '@material-ui/core';
import TextField from '@material-ui/core/TextField';
import CircularProgress from '@material-ui/core/CircularProgress';
import Autocomplete, {
AutocompleteChangeDetails,
AutocompleteChangeReason,
AutocompleteProps
} from '@material-ui/lab/Autocomplete';
@sibelius
sibelius / useStateCallback.tsx
Last active July 29, 2020 13:42
useState that properly handles a function value
export const useStateCallback = (callback: Function) => {
const [value, setValue] = useState(() => callback);
const setCallback = useCallback((fn: Function) => {
setValue(() => fn);
}, [setValue]
return [value, setCallback];
}
@sibelius
sibelius / useTransition.tsx
Created April 27, 2020 18:48
useTransition polyfill
import { unstable_withSuspenseConfig, useState, useCallback } from 'react'
// based on https://github.com/facebook/relay/commit/1befdc085bceef5c78f8eb8c2117459ce4b9d7c7#diff-c8dcd2c1e7d048e3b69a8538434cbca4
function useSuspenseTransition(config: {|timeoutMs: number|}) {
const [isPending, setPending] = useState(false);
const startTransition = useCallback(
(callback: () => void) => {
setPending(true);
Scheduler.unstable_next(() => {
@jgcmarins
jgcmarins / babel.config.js
Created April 24, 2020 23:37
Webpack configs for Node.js backends to run both locally and on AWS Lambda
module.exports = {
presets: [
'@babel/preset-react',
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},