Skip to content

Instantly share code, notes, and snippets.

View mattpocock's full-sized avatar

Matt Pocock mattpocock

View GitHub Profile
type Params = any
type Payload = any
type Meta = any
declare namespace Api {
function find(
modelName: string,
id: string | number,
params?: Params
): Promise<any>
@mattpocock
mattpocock / forms.md
Last active May 30, 2019 12:25
Autogenerate forms based on JSON API

Auto Generating forms (or an entire admin application?) based on JSON API models

I've been thinking more and more about how we can automate the generation of FE code based on knowledge of the BE model. I feel strongly that if the BE changes, the FE should be able to automatically react.

This should definitely be true when inputting data to mutations.

// AssetInputForm.jsx

// auto-generated JSON from API schema
const file = require('file');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const getSchema = require('./getSchema');
const generateMocks = require('./generateMocks');
const typesDir = path.resolve('../JlrLmsUI/app/utils/fixtures/api');
if (fs.existsSync(typesDir)) {
execSync(`rm -rf ${typesDir}`);
const file = require('file');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const getSchema = require('./getSchema');
const generateMocks = require('./generateMocks');
const typesDir = path.resolve('../JlrLmsUI/app/types/api');
execSync(`rm -rf ${typesDir}`);
/** Account manager becomes a wrapped instance of the api which you can perform actions on */
const accountManager = useResource('AccountManagerResource');
/** 'fetch' calls will first check the cache, then return it to the data prop */
accountManager.fetch({ ...params });
accountManager.fetchAll({ ...params });
/** refetch calls will override the cache */
accountManager.refetch({ ...params });
accountManager.refetchAll({ ...params });

Switching Props based on clients

Getting rid of region tags is a tough problem. It's not tough technically - it can be done in lots of different ways. But working out the best way is tough.

The options

clientSwitchValue

const someValue = clientSwitchValue({
@mattpocock
mattpocock / addons.js
Created June 10, 2019 08:06
Adding storybook to the repo
import 'storybook-addon-styled-component-theme/dist/src/register';
rm -rf packages/ae
cp -r ../one-app-mono/packages/all3 packages/ae
cd packages/ae
git checkout ae/staging
rm -rf .git
cd ../../
echo "Successfully replaced ae"
rm -rf packages/all3
cp -r ../one-app-mono/packages/all3 packages/all3
import React, { useState } from 'react';
import {
getStorybookUI,
configure,
addDecorator,
} from '@storybook/react-native';
import ThemeProvider from '../theme/ThemeProvider';
import './stories/Split.stories.tsx';
import { themesAsObject } from '../test-utils/testAllThemes';
import { View } from 'react-native';
import { makeReducer, makeActionCreators } from './makeReducer';
const createReducer = <
State,
Actions extends { [K in keyof Actions]: (payload?: any) => void }
>({
initialState,
actions,
}: Params<State, Actions>): CreateReducerReturnedObject<State, Actions> => {
const reducer = makeReducer({