Migrate to a monorepo setup - step 1 * Move web to subfolder /apps/scandic-web * Yarn + transitive deps - Move to yarn - design-system package removed for now since yarn doesn't support the parameter for token (ie project currently broken) - Add missing transitive dependencies as Yarn otherwise prevents these imports - VS Code doesn't pick up TS path aliases unless you open /apps/scandic-web instead of root (will be fixed with monorepo) * Pin framer-motion to temporarily fix typing issue https://github.com/adobe/react-spectrum/issues/7494 * Pin zod to avoid typ error There seems to have been a breaking change in the types returned by zod where error is now returned as undefined instead of missing in the type. We should just handle this but to avoid merge conflicts just pin the dependency for now. * Pin react-intl version Pin version of react-intl to avoid tiny type issue where formatMessage does not accept a generic any more. This will be fixed in a future commit, but to avoid merge conflicts just pin for now. * Pin typescript version Temporarily pin version as newer versions as stricter and results in a type error. Will be fixed in future commit after merge. * Setup workspaces * Add design-system as a monorepo package * Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN * Fix husky for monorepo setup * Update netlify.toml * Add lint script to root package.json * Add stub readme * Fix react-intl formatMessage types * Test netlify.toml in root * Remove root toml * Update netlify.toml publish path * Remove package-lock.json * Update build for branch/preview builds Approved-by: Linus Flood
225 lines
6.4 KiB
TypeScript
225 lines
6.4 KiB
TypeScript
"use server"
|
|
|
|
import { z } from "zod"
|
|
|
|
import { ApiLang } from "@/constants/languages"
|
|
import * as api from "@/lib/api"
|
|
import { getProfile } from "@/lib/trpc/memoizedRequests"
|
|
import { protectedServerActionProcedure } from "@/server/trpc"
|
|
|
|
import { editProfileSchema } from "@/components/Forms/Edit/Profile/schema"
|
|
import { countriesMap } from "@/components/TempDesignSystem/Form/Country/countries"
|
|
import { getIntl } from "@/i18n"
|
|
import { getMembership } from "@/utils/user"
|
|
import { phoneValidator } from "@/utils/zod/phoneValidator"
|
|
|
|
import { Status } from "@/types/components/myPages/myProfile/edit"
|
|
|
|
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
|
|
}
|
|
if (data.phoneNumber) {
|
|
data.phoneNumber = data.phoneNumber.replaceAll(" ", "").trim()
|
|
}
|
|
return data
|
|
})
|
|
|
|
export const editProfile = protectedServerActionProcedure
|
|
.input(editProfileSchema)
|
|
.mutation(async function ({ ctx, input }) {
|
|
const intl = await getIntl()
|
|
const payload = editProfilePayload.safeParse(input)
|
|
if (!payload.success) {
|
|
console.error(
|
|
"editProfile payload validation error",
|
|
JSON.stringify({
|
|
query: input,
|
|
error: payload.error,
|
|
})
|
|
)
|
|
|
|
return {
|
|
data: input,
|
|
issues: payload.error.issues.map((issue) => ({
|
|
field: issue.path.join("."),
|
|
message: issue.message,
|
|
})),
|
|
message: intl.formatMessage({
|
|
id: "An error occured when trying to update profile.",
|
|
}),
|
|
status: Status.error,
|
|
}
|
|
}
|
|
|
|
const profile = await getProfile()
|
|
if (!profile || "error" in profile) {
|
|
console.error(
|
|
"editProfile profile fetch error",
|
|
JSON.stringify({
|
|
query: input,
|
|
error: profile?.error,
|
|
})
|
|
)
|
|
|
|
return {
|
|
data: input,
|
|
issues: [],
|
|
message: intl.formatMessage({
|
|
id: "An error occured when trying to update profile.",
|
|
}),
|
|
status: Status.error,
|
|
}
|
|
}
|
|
|
|
const body: Partial<z.infer<typeof editProfilePayload>> = {}
|
|
Object.keys(payload.data).forEach((key) => {
|
|
const typedKey = key as unknown as keyof z.infer<
|
|
typeof editProfilePayload
|
|
>
|
|
if (typedKey === "password" || typedKey === "newPassword") {
|
|
body[typedKey] = payload.data[typedKey]
|
|
return
|
|
}
|
|
|
|
if (typedKey === "address") {
|
|
if (
|
|
(payload.data.address.city === profile.address.city ||
|
|
(!payload.data.address.city && !profile.address.city)) &&
|
|
(payload.data.address.countryCode === profile.address.countryCode ||
|
|
(!payload.data.address.countryCode &&
|
|
!profile.address.countryCode)) &&
|
|
(payload.data.address.streetAddress ===
|
|
profile.address.streetAddress ||
|
|
(!payload.data.address.streetAddress &&
|
|
!profile.address.streetAddress)) &&
|
|
(payload.data.address.zipCode === profile.address.zipCode ||
|
|
(!payload.data.address.zipCode && !profile.address.zipCode))
|
|
) {
|
|
// untouched - noop
|
|
} else {
|
|
/** API clears all other fields not sent in address payload */
|
|
body.address = payload.data.address
|
|
}
|
|
return
|
|
}
|
|
|
|
// Handle case where dateOfBirth is missing in profile and not updated in form
|
|
if (
|
|
typedKey === "dateOfBirth" &&
|
|
!profile.dateOfBirth &&
|
|
payload.data.dateOfBirth === "1900-01-01"
|
|
) {
|
|
return
|
|
}
|
|
|
|
if (payload.data[typedKey] !== profile[typedKey]) {
|
|
// @ts-ignore
|
|
body[typedKey] = payload.data[typedKey]
|
|
}
|
|
})
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
return {
|
|
data: input,
|
|
message: intl.formatMessage({ id: "Successfully updated profile!" }),
|
|
status: Status.success,
|
|
}
|
|
} else {
|
|
const membership = getMembership(profile.memberships)
|
|
console.log(
|
|
`[edit profile: ${membership?.membershipNumber}] body keys: ${JSON.stringify(Object.keys(body))}`
|
|
)
|
|
}
|
|
|
|
const apiResponse = await api.patch(api.endpoints.v1.Profile.profile, {
|
|
body,
|
|
cache: "no-store",
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
})
|
|
|
|
if (!apiResponse.ok) {
|
|
const text = await apiResponse.text()
|
|
console.error(
|
|
"editProfile api patch error",
|
|
JSON.stringify({
|
|
query: input,
|
|
error: {
|
|
status: apiResponse.status,
|
|
statusText: apiResponse.statusText,
|
|
error: text,
|
|
},
|
|
})
|
|
)
|
|
|
|
return {
|
|
data: input,
|
|
issues: [],
|
|
message: intl.formatMessage({
|
|
id: "An error occured when trying to update profile.",
|
|
}),
|
|
status: Status.error,
|
|
}
|
|
}
|
|
|
|
const json = await apiResponse.json()
|
|
if (json.errors?.length) {
|
|
json.errors.forEach((error: any) => {
|
|
console.warn(
|
|
"editProfile api patch errors (not aborting)",
|
|
JSON.stringify({
|
|
query: input,
|
|
error,
|
|
})
|
|
)
|
|
})
|
|
}
|
|
|
|
const validatedData = editProfileSchema.safeParse(json.data.attributes)
|
|
if (!validatedData.success) {
|
|
console.error(
|
|
"editProfile validation error",
|
|
JSON.stringify({
|
|
query: input,
|
|
error: validatedData.error,
|
|
})
|
|
)
|
|
|
|
return {
|
|
data: input,
|
|
issues: validatedData.error.issues.map((issue) => ({
|
|
field: issue.path.join("."),
|
|
message: issue.message,
|
|
})),
|
|
message: intl.formatMessage({
|
|
id: "An error occured when trying to update profile.",
|
|
}),
|
|
status: Status.error,
|
|
}
|
|
}
|
|
|
|
return {
|
|
data: validatedData.data,
|
|
message: intl.formatMessage({ id: "Successfully updated profile!" }),
|
|
status: Status.success,
|
|
}
|
|
})
|