Skip to content

Instantly share code, notes, and snippets.

View ctrlplusb's full-sized avatar
📢

Sean Matheson ctrlplusb

📢
View GitHub Profile
@ctrlplusb
ctrlplusb / findManyCursor.test.ts
Last active February 18, 2022 17:22
Utility to provide Relay Cursor Connection Specification support to Prisma Framework
import { Country, Photon } from '@prisma/photon';
import { findManyCursor } from './findManyCursor';
const photon = new Photon();
let data: Country[];
const createCountry = async (id: string) => photon.countries.create({
data: {
id,
const store = createStore<StoreModel>({
todos: {
// ...
add: (state, payload) => {
return {
...state,
items: [
...state.items,
payload
]
import { StoreProvider, createStore, useStore, useActions } from 'easy-peasy';
// 👇 create your store, providing the model
const store = createStore({
todos: {
items: ['Install easy-peasy', 'Build app', 'Profit'],
// 👇 define actions directly on your model
add: (state, payload) => {
// do simple mutation to update state, and we make it an immutable update
state.items.push(payload)
import React from "react";
import { render } from "react-dom";
import { StoreProvider } from "easy-peasy"; // 👈
import store from "./store";
render(
<StoreProvider store={store}>
<App />
</StoreProvider>,
document.querySelector("#root");
export default function Todos() {
const items = useStore(state => state.todos.items);
// 👇
const save = useActions(actions => actions.todos.save);
const [newTodo, setNewTodo] = useState("");
return (
<div>
import { thunk } from "easy-peasy"; // 👈
const store = createStore<StoreModel>({
todos: {
items: ["Install easy-peasy", "Build app", "profit"],
// 👇renamed
saved: (state, payload) => {
state.items.push(payload);
},
// 👇our thunk
import { Action, Thunk } from 'easy-peasy';
type TodosModel = {
items: string[];
saved: Action<TodosModel, string>;
save: Thunk<TodosModel, string>; // 👈
}
import { useState } from "react";
import { useStore, useActions } from "./hooks"; // 👈
export default function Todos() {
// 👇 pull out state from our store
const items = useStore(state => state.todos.items);
// 👇 pull out actions from our store
const add = useActions(actions => actions.todos.add);
import { createTypedHooks } from "easy-peasy";
import { StoreModel } from "./model";
const {
useActions,
useStore,
useDispatch
} = createTypedHooks<StoreModel>();
export { useActions, useDispatch, useStore };
store.dispatch.todos.add("Finish reading this article");