feat: SW-65 Working on booking widget wrapper component
This commit is contained in:
56
actions/updateBookingWidget.ts
Normal file
56
actions/updateBookingWidget.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
"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
|
||||
*/
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user