refactor(SW-360): improve error handling logic
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
"use server"
|
"use server"
|
||||||
|
|
||||||
|
import { redirect } from "next/navigation"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
@@ -45,7 +46,7 @@ export const registerUser = profileServiceServerActionProcedure
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
return false
|
return { success: false, error: "Validation error" }
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -57,7 +58,7 @@ export const registerUser = profileServiceServerActionProcedure
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!apiResponse.ok) {
|
if (!apiResponse.ok) {
|
||||||
const text = apiResponse.text()
|
const text = await apiResponse.text()
|
||||||
console.error(
|
console.error(
|
||||||
"registerUser api error",
|
"registerUser api error",
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@@ -69,14 +70,16 @@ export const registerUser = profileServiceServerActionProcedure
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
return false
|
return { success: false, error: "API error" }
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = await apiResponse.json()
|
const json = await apiResponse.json()
|
||||||
console.log("json", json)
|
console.log("json", json)
|
||||||
|
|
||||||
return true
|
// TODO: Redirect to actual success page.
|
||||||
|
redirect("/signup/success")
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return false
|
console.error("Unexpected error", error)
|
||||||
|
return { success: false, error: "Unexpected error" }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -52,17 +52,11 @@ export default function Form({ link, subtitle, title }: RegisterFormProps) {
|
|||||||
const zipCode = intl.formatMessage({ id: "Zip code" })
|
const zipCode = intl.formatMessage({ id: "Zip code" })
|
||||||
|
|
||||||
async function handleSubmit(data: RegisterSchema) {
|
async function handleSubmit(data: RegisterSchema) {
|
||||||
const isSuccessResponse = await registerUser(data)
|
const result = await registerUser(data)
|
||||||
if (!isSuccessResponse) {
|
if (!result.success) {
|
||||||
toast.error("Something went wrong!")
|
toast.error(intl.formatMessage({ id: "Something went wrong!" }))
|
||||||
} else {
|
|
||||||
// TODO: Toast should be removed and we should show a different page
|
|
||||||
toast.success("Form submitted successfully")
|
|
||||||
// should we navigate to sub route like /signup/success ?
|
|
||||||
// router.push("/")
|
|
||||||
|
|
||||||
methods.reset()
|
|
||||||
}
|
}
|
||||||
|
// No need to handle success case here, as the redirect happens server-side.
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ export const contentPageQueryRouter = router({
|
|||||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||||
const { lang, uid } = ctx
|
const { lang, uid } = ctx
|
||||||
|
|
||||||
|
// TODO: Add logic to: check if Authed & Content Page URL is a "signup page", if so, redirect to home page.
|
||||||
|
|
||||||
const contentPageRefsData = await fetchContentPageRefs(lang, uid)
|
const contentPageRefsData = await fetchContentPageRefs(lang, uid)
|
||||||
const contentPageRefs = validateContentPageRefs(
|
const contentPageRefs = validateContentPageRefs(
|
||||||
contentPageRefsData,
|
contentPageRefsData,
|
||||||
|
|||||||
Reference in New Issue
Block a user