57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
"use server"
|
|
import { ZodError } from "zod"
|
|
|
|
import { bookingWidgetSchema } from "@/components/BookingWidget/schema"
|
|
|
|
import { type State, Status } from "@/types/components/myPages/myProfile/edit"
|
|
|
|
export async function updateBookingWidget(_prevState: State, values: FormData) {
|
|
try {
|
|
const data: Record<string, any> = Object.fromEntries(values.entries())
|
|
|
|
/**
|
|
* ToDo: Update the data parsing and redirect User to respective booking flow page
|
|
*/
|
|
console.info(`Raw Data BW`)
|
|
console.log(data)
|
|
const parsedData = bookingWidgetSchema.safeParse(data)
|
|
if (parsedData.success) {
|
|
console.info(`Success`)
|
|
console.log(parsedData.data)
|
|
return {
|
|
message: "All good!",
|
|
status: Status.success,
|
|
}
|
|
} else {
|
|
console.error("Error parsing BW data")
|
|
console.error(parsedData.error)
|
|
return {
|
|
message: "Invalid data, parse failed!",
|
|
status: Status.error,
|
|
}
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof ZodError) {
|
|
console.error(`ZodError handling profile edit`)
|
|
console.error(error)
|
|
|
|
return {
|
|
errors: error.issues.map((issue) => ({
|
|
message: `Server validation: ${issue.message}`,
|
|
path: issue.path.join("."),
|
|
})),
|
|
message: "Invalid form data",
|
|
status: Status.error,
|
|
}
|
|
}
|
|
|
|
console.error(`EditProfile Server Action Error`)
|
|
console.error(error)
|
|
|
|
return {
|
|
message: "Something went wrong. Please try again.",
|
|
status: Status.error,
|
|
}
|
|
}
|
|
}
|