Skip to content

Instantly share code, notes, and snippets.

@JordanMarr
Last active February 18, 2022 18:59
Show Gist options
  • Save JordanMarr/ee52a658d025c9d94931052d23077997 to your computer and use it in GitHub Desktop.
Save JordanMarr/ee52a658d025c9d94931052d23077997 to your computer and use it in GitHub Desktop.
Fable React Toastify
// npm install react-toastify
module Toastify
open Fable.Core
open Fable.Core.JsInterop
open Fable.React
type ToastContainerProps =
| AutoClose of int
let inline toastContainer (props : ToastContainerProps list) : ReactElement =
ofImport "ToastContainer" "react-toastify" (keyValueList CaseRules.LowerFirst props) []
let info: url: string -> unit = import "toast.info" "react-toastify"
let success: url: string -> unit = import "toast.success" "react-toastify"
let error: url: string -> unit = import "toast.error" "react-toastify"
let warn: url: string -> unit = import "toast.warn" "react-toastify"
module Main
open Fable.Core.JsInterop
// Import Toastify css
importAll "../node_modules/react-toastify/dist/ReactToastify.min.css"
open Feliz
open Fable.React
open Browser.Dom
// App
ReactDOM.render(App.app, document.getElementById "root")
module App
open Feliz
open Fable.React
open Fable.React.Props
open Toastify
let app = React.functionComponent(fun () ->
div [] [
// Page Layout...
// Configure
Toastify.toastContainer [AutoClose 2000]
]
)
module Projects
open Feliz
open Fable.React
open Entities.Project
open Thoth.Fetch
let page = React.functionComponent(fun () ->
let ctx = React.useContext(Contexts.appContext)
let projects, setProjects = React.useState<ProjectHeader[]>([||])
React.useEffectOnce(fun () ->
Fetch.tryGet<ProjectHeader[]>("/api/Projects", HttpService.withToken ctx.Token)
|> Promise.iter (function
| Ok data ->
setProjects data
Toastify.success "Projects loaded!"
| Error err ->
Toastify.error "Unable to load data."
)
)
div [] [
for p in projects do
div [] [str p.Name]
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment