Skip to content

Commit

Permalink
Feat/add raw mutation (#132)
Browse files Browse the repository at this point in the history
* adds rawmutation handler

* uses raw version as sot

* unifies return types

* fixes typo

* changeset
  • Loading branch information
MakhBeth authored Jan 30, 2025
1 parent 28c81ff commit e0855e9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-crabs-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-app/vue": minor
---

Added `useAndHandleMutationResult` that returns the Result unwrapped instead of `useAndHandleMutation` simplified version
78 changes: 69 additions & 9 deletions packages/vue/src/makeClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Result } from "@effect-rx/rx/Result"
import * as Sentry from "@sentry/browser"
import { Cause, Effect, Exit, Match, Option, Runtime, S, Struct } from "effect-app"
import type { RequestHandler, RequestHandlerWithInput, TaggedRequestClassAny } from "effect-app/client/clientFor"
Expand Down Expand Up @@ -91,13 +92,14 @@ type WithAction<A> = A & {

// computed() takes a getter function and returns a readonly reactive ref
// object for the returned value from the getter.
type Resp<I, A, E, R> = readonly [
ComputedRef<Res<A, E>>,

type Resp<I, A, E, R, V = ComputedRef<Res<A, E>>> = readonly [
V,
WithAction<(I: I) => Effect<Exit<A, E>, never, R>>
]

type ActResp<A, E, R> = readonly [
ComputedRef<Res<A, E>>,
type ActResp<A, E, R, V = ComputedRef<Res<A, E>>> = readonly [
V,
WithAction<Effect<Exit<A, E>, never, R>>
]

Expand Down Expand Up @@ -344,9 +346,9 @@ export const makeClient = <Locale extends string, R>(

/**
* Pass a function that returns an Effect, e.g from a client action, give it a name.
* Returns a tuple with state ref and execution function which reports success and errors as Toast.
* Returns a tuple with raw Result and execution function which reports success and errors as Toast.
*/
const _useAndHandleMutation: {
const _useAndHandleMutationResult: {
<
I,
E extends ResponseErrors,
Expand All @@ -366,7 +368,7 @@ export const makeClient = <Locale extends string, R>(
self: RequestHandlerWithInput<I, A, E, R, Request>,
action: string,
options?: Opts<A, E, R, I, A2, E2, R2, ESuccess, RSuccess, EError, RError, EDefect, RDefect>
): Resp<I, A2, E2, R2>
): Resp<I, A2, E2, R2, Readonly<Ref<Result<A2, E2>, Result<A2, E2>>>>
<
E extends ResponseErrors,
A,
Expand All @@ -385,7 +387,7 @@ export const makeClient = <Locale extends string, R>(
self: RequestHandler<A, E, R, Request>,
action: string,
options?: Opts<A, E, R, void, A2, E2, R2, ESuccess, RSuccess, EError, RError, EDefect, RDefect>
): ActResp<A2, E2, R2>
): ActResp<A2, E2, R2, Readonly<Ref<Result<A2, E2>, Result<A2, E2>>>>
} = (
self: any,
action: any,
Expand All @@ -407,11 +409,68 @@ export const makeClient = <Locale extends string, R>(
}, options ? dropUndefinedT(options) : undefined)

return tuple(
computed(() => mutationResultToVue(a.value)),
a,
handleRequestWithToast(b as any, action, options)
)
}

/**
* Pass a function that returns an Effect, e.g from a client action, give it a name.
* Returns a tuple with state ref and execution function which reports success and errors as Toast.
*/
const _useAndHandleMutation: {
<
I,
E extends ResponseErrors,
A,
R,
Request extends TaggedRequestClassAny,
A2 = A,
E2 extends ResponseErrors = E,
R2 = R,
ESuccess = never,
RSuccess = never,
EError = never,
RError = never,
EDefect = never,
RDefect = never
>(
self: RequestHandlerWithInput<I, A, E, R, Request>,
action: string,
options?: Opts<A, E, R, I, A2, E2, R2, ESuccess, RSuccess, EError, RError, EDefect, RDefect>
): Resp<I, A2, E2, R2>
<
E extends ResponseErrors,
A,
R,
Request extends TaggedRequestClassAny,
A2 = A,
E2 extends ResponseErrors = E,
R2 = R,
ESuccess = never,
RSuccess = never,
EError = never,
RError = never,
EDefect = never,
RDefect = never
>(
self: RequestHandler<A, E, R, Request>,
action: string,
options?: Opts<A, E, R, void, A2, E2, R2, ESuccess, RSuccess, EError, RError, EDefect, RDefect>
): ActResp<A2, E2, R2>
} = (
self: any,
action: any,
options?: Opts<any, any, any, any, any, any, any, any, any, any, any, any, any>
): any => {
const [a, b] = _useAndHandleMutationResult(self, action, options)

return tuple(
computed(() => mutationResultToVue(a.value)),
b
)
}

/**
* The same as @see useAndHandleMutation, but does not display any toasts by default.
* Messages for success, error and defect toasts can be provided in the Options.
Expand Down Expand Up @@ -659,6 +718,7 @@ export const makeClient = <Locale extends string, R>(
return {
useSafeMutationWithState: _useSafeMutationWithState,
useAndHandleMutation: _useAndHandleMutation,
useAndHandleMutationResult: _useAndHandleMutationResult,
useAndHandleMutationSilently: _useAndHandleMutationSilently,
useAndHandleMutationCustom: _useAndHandleMutationCustom,
makeUseAndHandleMutation: _makeUseAndHandleMutation,
Expand Down

0 comments on commit e0855e9

Please sign in to comment.