feat(SW-160): update profile
This commit is contained in:
committed by
Michael Zetterberg
parent
b6e22d51a5
commit
2337d37f1a
@@ -1,64 +1,100 @@
|
||||
"use server"
|
||||
import { ZodError } from "zod"
|
||||
|
||||
import { z } from "zod"
|
||||
|
||||
import { ApiLang } from "@/constants/languages"
|
||||
import * as api from "@/lib/api"
|
||||
import { protectedServerActionProcedure } from "@/server/trpc"
|
||||
|
||||
import { editProfileSchema } from "@/components/Forms/Edit/Profile/schema"
|
||||
import { countriesMap } from "@/components/TempDesignSystem/Form/Country/countries"
|
||||
import { phoneValidator } from "@/utils/phoneValidator"
|
||||
|
||||
import { type State, Status } from "@/types/components/myPages/myProfile/edit"
|
||||
import { Status } from "@/types/components/myPages/myProfile/edit"
|
||||
|
||||
export async function editProfile(_prevState: State, values: FormData) {
|
||||
try {
|
||||
const data: Record<string, any> = Object.fromEntries(values.entries())
|
||||
|
||||
/**
|
||||
* TODO: Update profile data when endpoint from
|
||||
* API team is ready
|
||||
*/
|
||||
|
||||
console.info(`Raw Data`)
|
||||
console.log(data)
|
||||
data.address = {
|
||||
city: data["address.city"],
|
||||
countryCode: data["address.countryCode"],
|
||||
streetAddress: data["address.streetAddress"],
|
||||
zipCode: data["address.zipCode"],
|
||||
const editProfilePayload = z
|
||||
.object({
|
||||
address: z.object({
|
||||
city: z.string().optional(),
|
||||
countryCode: z.nativeEnum(countriesMap),
|
||||
streetAddress: z.string().optional(),
|
||||
zipCode: z.string().min(1, { message: "Zip code is required" }),
|
||||
}),
|
||||
dateOfBirth: z.string(),
|
||||
email: z.string().email(),
|
||||
language: z.nativeEnum(ApiLang),
|
||||
newPassword: z.string().optional(),
|
||||
password: z.string().optional(),
|
||||
phoneNumber: phoneValidator("Phone is required"),
|
||||
})
|
||||
.transform((data) => {
|
||||
if (!data.password || !data.newPassword) {
|
||||
delete data.password
|
||||
delete data.newPassword
|
||||
}
|
||||
const parsedData = editProfileSchema.safeParse(data)
|
||||
if (parsedData.success) {
|
||||
console.info(`Success`)
|
||||
console.log(parsedData.data)
|
||||
return {
|
||||
message: "All good!",
|
||||
status: Status.success,
|
||||
}
|
||||
} else {
|
||||
console.error("Error parsing edit profile")
|
||||
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 data
|
||||
})
|
||||
|
||||
export const editProfile = protectedServerActionProcedure
|
||||
.input(editProfileSchema)
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const payload = editProfilePayload.safeParse(input)
|
||||
if (!payload.success) {
|
||||
return {
|
||||
errors: error.issues.map((issue) => ({
|
||||
message: `Server validation: ${issue.message}`,
|
||||
path: issue.path.join("."),
|
||||
data: input,
|
||||
issues: payload.error.issues.map((issue) => ({
|
||||
field: issue.path.join("."),
|
||||
message: issue.message,
|
||||
})),
|
||||
message: "Invalid form data",
|
||||
message: "Validation failed.",
|
||||
status: Status.error,
|
||||
}
|
||||
}
|
||||
|
||||
console.error(`EditProfile Server Action Error`)
|
||||
console.error(error)
|
||||
const response = await api.patch(api.endpoints.v1.profile, {
|
||||
body: payload.data,
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
console.info(`Response not ok`)
|
||||
console.error(response)
|
||||
return {
|
||||
data: input,
|
||||
issues: [],
|
||||
message: "Server error",
|
||||
status: Status.error,
|
||||
}
|
||||
}
|
||||
|
||||
const json = await response.json()
|
||||
if (json.errors?.length) {
|
||||
json.errors.forEach((error: any) => {
|
||||
console.info(`API Fail in response`)
|
||||
console.error(error)
|
||||
})
|
||||
}
|
||||
|
||||
const validatedData = editProfileSchema.safeParse(json.data.attributes)
|
||||
if (!validatedData.success) {
|
||||
console.log({ ees: validatedData.error })
|
||||
return {
|
||||
data: input,
|
||||
issues: validatedData.error.issues.map((issue) => ({
|
||||
field: issue.path.join("."),
|
||||
message: issue.message,
|
||||
})),
|
||||
message: "Data is insufficient",
|
||||
status: Status.error,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
message: "Something went wrong. Please try again.",
|
||||
status: Status.error,
|
||||
data: validatedData.data,
|
||||
message: "All good!",
|
||||
status: Status.success,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user