Skip to content

Commit

Permalink
fix: toast promise type
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Apr 15, 2022
1 parent 4fc1300 commit 0686232
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
22 changes: 7 additions & 15 deletions packages/machines/toast/src/toast-group.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import { StateMachine as S, subscribe } from "@zag-js/core"
import { normalizeProp, PropTypes, ReactPropTypes } from "@zag-js/types"
import { runIfFn } from "@zag-js/utils"
import { dom } from "./toast.dom"
import {
Toaster,
GroupProps,
GroupMachineContext,
Options,
Placement,
PromiseMessages,
PromiseOptions,
} from "./toast.types"
import { Toaster, GroupProps, GroupMachineContext, Options, Placement, PromiseOptions } from "./toast.types"
import { getGroupPlacementStyle, getToastsByPlacement } from "./toast.utils"

export let toaster: Toaster = {} as any
Expand Down Expand Up @@ -94,17 +86,17 @@ export function groupConnect<T extends PropTypes = ReactPropTypes>(
return group.upsert(options)
},

promise<T>(promise: Promise<T>, msgs: PromiseMessages, opts: PromiseOptions = {}) {
const id = group.loading({ ...opts, ...opts?.loading, type: "loading", title: msgs.loading })
promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared: Options = {}) {
const id = group.loading({ ...shared, ...options.loading })

promise
.then((response) => {
const message = runIfFn(msgs.loading, response)
group.success({ ...opts, ...opts?.success, id, title: message })
const successOptions = runIfFn(options.success, response)
group.success({ ...shared, ...successOptions, id })
})
.catch((error) => {
const message = runIfFn(msgs.error, error)
group.error({ ...opts, ...opts?.error, id, title: message })
const errorOptions = runIfFn(options.error, error)
group.error({ ...shared, ...errorOptions, id })
})

return promise
Expand Down
14 changes: 5 additions & 9 deletions packages/machines/toast/src/toast.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,10 @@ export type GroupMachineContext = SharedContext &

type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value)

export type PromiseMessages<Value = any> = {
loading: Value
success: MaybeFunction<Value, Options>
error: MaybeFunction<Value, Options>
}

export type PromiseOptions = Options & {
[key in "success" | "loading" | "error"]?: Options
export type PromiseOptions<Value> = {
loading: Options
success: MaybeFunction<Options, Value>
error: MaybeFunction<Options, any>
}

export type GroupProps = {
Expand All @@ -160,5 +156,5 @@ export type Toaster = {
loading(options: Options): string | undefined
dismiss(id?: string | undefined): void
remove(id?: string | undefined): void
promise<T>(promise: Promise<T>, msgs: PromiseMessages, opts?: PromiseOptions): Promise<T>
promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: Options): Promise<T>
}

0 comments on commit 0686232

Please sign in to comment.