Skip to content

Commit

Permalink
feat: check if callback exist before executeCb function
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Tagliabue committed Jun 28, 2024
1 parent 367506a commit e339b74
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions src/use-formbit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ export default <Values extends InitialValues>({
} = {},
action
) => {
const newUUID = uuidv4()
const newUUID = (function getUUID() {
if (successCallback || errorCallback) {
return uuidv4()
}

return undefined
})()

setWriter((w) => {
const liveValidationPaths = noLiveValidation ? [] : Object.keys(w.liveValidation)
Expand All @@ -126,7 +132,7 @@ export default <Values extends InitialValues>({
const newWriter: Writer<Partial<Values>> = { ...w, form, isDirty: true }

if (paths.length === 0) {
executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)

return newWriter
}
Expand All @@ -143,7 +149,7 @@ export default <Values extends InitialValues>({
if (isEmpty(inner)) {
const neww = { ...newWriter, errors: cleanErrors }

executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)

return neww
}
Expand All @@ -160,7 +166,7 @@ export default <Values extends InitialValues>({

const neww = { ...newWriter, errors, liveValidation }

executeCb(newUUID, errorCallback)
errorCallback && executeCb(newUUID, errorCallback)

return neww
})
Expand All @@ -182,7 +188,13 @@ export default <Values extends InitialValues>({
options = {}
} = {}
) => {
const newUUID = uuidv4()
const newUUID = (function getUUID() {
if (successCallback || errorCallback) {
return uuidv4()
}

return undefined
})()

setWriter((w) => {
const liveValidationPaths = noLiveValidation ? [] : Object.keys(w.liveValidation)
Expand All @@ -196,7 +208,7 @@ export default <Values extends InitialValues>({
const newWriter = { ...w, form, isDirty: true }

if (paths.length === 0) {
executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)

return newWriter
}
Expand All @@ -213,7 +225,7 @@ export default <Values extends InitialValues>({
if (isEmpty(inner)) {
const neww = { ...newWriter, errors: cleanErrors }

executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)

return neww
}
Expand All @@ -230,7 +242,7 @@ export default <Values extends InitialValues>({

const neww = { ...newWriter, errors, liveValidation }

executeCb(newUUID, errorCallback)
errorCallback && executeCb(newUUID, errorCallback)

return neww
})
Expand All @@ -247,7 +259,13 @@ export default <Values extends InitialValues>({
options = {}
} = {}
) => {
const newUUID = uuidv4()
const newUUID = (function getUUID() {
if (successCallback || errorCallback) {
return uuidv4()
}

return undefined
})()

setWriter((w) => {
const liveValidationPaths = noLiveValidation ? [] : Object.keys(w.liveValidation)
Expand All @@ -261,7 +279,7 @@ export default <Values extends InitialValues>({
const newWriter = { ...w, form, isDirty: true }

if (paths.length === 0) {
executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)
return newWriter
}

Expand All @@ -274,7 +292,7 @@ export default <Values extends InitialValues>({

if (isEmpty(inner)) {
const neww = { ...newWriter, errors: cleanErrors }
executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)
return neww
}

Expand All @@ -290,7 +308,7 @@ export default <Values extends InitialValues>({

const neww = { ...newWriter, errors, liveValidation }

executeCb(newUUID, errorCallback)
errorCallback && executeCb(newUUID, errorCallback)

return neww
})
Expand All @@ -306,7 +324,13 @@ export default <Values extends InitialValues>({
options
} = {}
) => {
const newUUID = uuidv4()
const newUUID = (function getUUID() {
if (successCallback || errorCallback) {
return uuidv4()
}

return undefined
})()

setWriter((w) => {
// Teardown errors
Expand All @@ -318,7 +342,7 @@ export default <Values extends InitialValues>({
if (isEmpty(inner)) {
const neww = { ...w, errors: cleanErrors }

executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)

return neww
}
Expand All @@ -335,7 +359,7 @@ export default <Values extends InitialValues>({

const neww = { ...w, errors, liveValidation }

executeCb(newUUID, errorCallback)
errorCallback && executeCb(newUUID, errorCallback)

return neww
})
Expand All @@ -345,7 +369,13 @@ export default <Values extends InitialValues>({
paths,
{ successCallback, errorCallback, options } = {}
) => {
const newUUID = uuidv4()
const newUUID = (function getUUID() {
if (successCallback || errorCallback) {
return uuidv4()
}

return undefined
})()

setWriter((w) => {
// Teardown errors
Expand All @@ -360,7 +390,7 @@ export default <Values extends InitialValues>({
if (isEmpty(inner)) {
const neww = { ...w, errors: cleanErrors }

executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)

return neww
}
Expand All @@ -377,7 +407,7 @@ export default <Values extends InitialValues>({

const neww = { ...w, errors, liveValidation }

executeCb(newUUID, errorCallback)
errorCallback && executeCb(newUUID, errorCallback)

return neww
})
Expand Down Expand Up @@ -414,14 +444,20 @@ export default <Values extends InitialValues>({
errorCallback,
{ isDirty: _, options } = {}
) => {
const newUUID = uuidv4()
const newUUID = (function getUUID() {
if (successCallback || errorCallback) {
return uuidv4()
}

return undefined
})()

setWriter((w) => {
try {
// abortEarly: false as default is to be retrocompatible
schemaRef.current.validateSync(w.form, { abortEarly: false, ...options })

executeCb(newUUID, successCallback)
successCallback && executeCb(newUUID, successCallback)

return { ...w, errors: {} }
} catch (e) {
Expand All @@ -445,7 +481,7 @@ export default <Values extends InitialValues>({

const newWriter = { ...w, errors, liveValidation }

executeCb(newUUID, errorCallback)
errorCallback && executeCb(newUUID, errorCallback)

return newWriter
}
Expand Down

0 comments on commit e339b74

Please sign in to comment.