This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isText = MessageUnion.is.Text; | |
export const isImage = MessageUnion.is.Image; | |
export const fold = MessageUnion.fold; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Message from ‘../path/to/message.ts’; | |
const message: Message = Message.createText({ text: ‘hello world’ }); | |
if (Message.isText(message)) { | |
return <p>This is a text</p>; | |
} else { | |
return <p>This is an image</p>; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function addSignature(signature: string) { | |
return (text: Text) => { | |
const $text = MessageUnion.Text.iso.unwrap(text); // get private type | |
const $newText = { | |
…$text, | |
text: `${$message.text} - ${signature}`, | |
}; | |
return MessageUnion.Text.iso.wrap($newText); // get opaque type | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// text :: (text: Text) => string | |
export const text = MessageUnion.Text.lensFromProp(‘text’).get; | |
// updateText :: (content: string) => (text: Text) => Text | |
export const updateText = MessageUnion.Text.lensFromProp(‘text’).set; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const fetchUsers = () => ({ | |
type: "FETCH_USERS" | |
}); | |
export const fetchUsersFailed = () => ({ | |
type: "FETCH_USERS_FAILED" | |
}); | |
export const fetchUsersSucceeded = (users) => ({ | |
type: "FETCH_USERS_SUCCEEDED", | |
payload: { | |
users |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialState = { loading: true, users: [], error: undefined }; | |
export function reducer(state = initialState, action) { | |
console.log({ type: action.type, action }); | |
switch (action.type) { | |
case "FETCH_USERS_SUCCEEDED": | |
return { | |
...state, | |
loading: false, | |
users: action.payload.users.filter( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fetchUsers, createUser } from "./fake-fetch"; | |
import { | |
fetchUsersSucceeded, | |
fetchUsersFailed, | |
createUserFailed, | |
createUserSucceeded | |
} from "./actionCreators"; | |
export const middleware = ({ dispatch, getState }) => (next) => (action) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getUsers = (state) => | |
state.users.map((user) => ({ | |
...user, | |
picture: user.picture || "https://i.pravatar.cc/300 " | |
})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { useDispatch, useSelector } from "react-redux"; | |
import { getUsers } from "./selectors"; | |
import { createUser } from "./actionCreators"; | |
import Users from "./components/Users"; | |
import UserForm from "./components/UserForm"; | |
export default function App() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as NonEmptyArray from "fp-ts/lib/NonEmptyArray"; | |
import * as User from "./user"; | |
export type State = Loading | Failure | Loaded; | |
export const initialState: Loading = "Loading"; | |
type Loading = "Loading"; | |
type Failure = Error; |