Skip to content

Commit

Permalink
Merge pull request #4 from vercel/nrf-demo-alpha.1
Browse files Browse the repository at this point in the history
Nrf demo alpha.1
  • Loading branch information
malewis5 authored Jan 8, 2025
2 parents 7bde15e + 21ea621 commit 032c50f
Show file tree
Hide file tree
Showing 288 changed files with 14,692 additions and 8,485 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* @bigcommerce/team-catalyst
* @bigcommerce/makeswift
228 changes: 0 additions & 228 deletions .github/workflows/regression-tests.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
'use server';

import { SubmissionResult } from '@conform-to/react';
import { parseWithZod } from '@conform-to/zod';
import { getTranslations } from 'next-intl/server';
import { z, ZodError } from 'zod';

import { schema } from '@/vibes/soul/sections/reset-password-section/schema';
import { client } from '~/client';
import { graphql } from '~/client/graphql';

const ChangePasswordFieldsSchema = z.object({
customerId: z.string(),
customerToken: z.string(),
currentPassword: z.string().min(1),
newPassword: z.string().min(1),
confirmPassword: z.string().min(1),
});

const ChangePasswordSchema = ChangePasswordFieldsSchema.omit({
currentPassword: true,
}).required();

const ChangePasswordMutation = graphql(`
mutation ChangePassword($input: ResetPasswordInput!) {
customer {
Expand All @@ -34,29 +24,26 @@ const ChangePasswordMutation = graphql(`
}
`);

interface ChangePasswordResponse {
status: 'success' | 'error';
message: string;
}

export const changePassword = async (formData: FormData): Promise<ChangePasswordResponse> => {
export async function changePassword(
{ token, customerEntityId }: { token: string; customerEntityId: string },
_prevState: { lastResult: SubmissionResult | null; successMessage?: string },
formData: FormData,
) {
const t = await getTranslations('ChangePassword');
const submission = parseWithZod(formData, { schema });

try {
const parsedData = ChangePasswordSchema.parse({
customerId: formData.get('customer-id'),
customerToken: formData.get('customer-token'),
newPassword: formData.get('new-password'),
confirmPassword: formData.get('confirm-password'),
});
if (submission.status !== 'success') {
return { lastResult: submission.reply({ formErrors: [t('Form.error')] }) };
}

try {
const response = await client.fetch({
document: ChangePasswordMutation,
variables: {
input: {
token: parsedData.customerToken,
customerEntityId: Number(parsedData.customerId),
newPassword: parsedData.newPassword,
token,
customerEntityId: Number(customerEntityId),
newPassword: submission.value.password,
},
},
fetchOptions: {
Expand All @@ -67,23 +54,24 @@ export const changePassword = async (formData: FormData): Promise<ChangePassword
const result = response.data.customer.resetPassword;

if (result.errors.length > 0) {
result.errors.forEach((error) => {
throw new Error(error.message);
});
return {
lastResult: submission.reply({ formErrors: result.errors.map((error) => error.message) }),
};
}

return {
status: 'success',
message: t('confirmChangePassword'),
lastResult: submission.reply(),
successMessage: t('Form.successMessage'),
};
} catch (error: unknown) {
if (error instanceof Error || error instanceof ZodError) {
if (error instanceof Error) {
return {
status: 'error',
message: error.message,
lastResult: submission.reply({ formErrors: [error.message] }),
};
}

return { status: 'error', message: t('Errors.error') };
return {
lastResult: submission.reply({ formErrors: [t('Errors.error')] }),
};
}
};
}
Loading

0 comments on commit 032c50f

Please sign in to comment.