Skip to content

Commit

Permalink
fix(FeedbackCreateForm): change feedback api to trpc
Browse files Browse the repository at this point in the history
  • Loading branch information
troff8 authored and Troff8 committed Jul 24, 2023
1 parent 0bde05c commit 5c6a23d
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 71 deletions.
36 changes: 17 additions & 19 deletions src/components/FeedbackCreateForm/FeedbackCreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import * as Sentry from '@sentry/nextjs';
import { errorsProvider } from '../../utils/forms';
import { createFeedbackSchema, CreateFeedback } from '../../schema/feedback';
import { ModalEvent, dispatchModalEvent } from '../../utils/dispatchModal';
import { dispatchErrorNotification } from '../../utils/dispatchNotification';
import { notifyPromise } from '../../utils/notifyPromise';
import { trpc } from '../../utils/trpcClient';

import { tr } from './FeedbackCreateForm.i18n';

const FeedbackCreateForm: React.FC = () => {
const [formBusy, setFormBusy] = useState(false);
const createMutation = trpc.feedback.create.useMutation();

const {
register,
Expand All @@ -25,28 +26,25 @@ const FeedbackCreateForm: React.FC = () => {

const errorsResolver = errorsProvider(errors, isSubmitted);

const onPending = useCallback(async (form: CreateFeedback) => {
setFormBusy(true);
const [res] = await notifyPromise(
fetch('/api/feedback', {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/json',
},
body: JSON.stringify({
const onPending = useCallback(
async (form: CreateFeedback) => {
setFormBusy(true);
const [res] = await notifyPromise(
createMutation.mutateAsync({
title: form.title,
description: form.description,
href: window.location.href,
}),
}),
'sentFeedback',
);
if (res) {
dispatchModalEvent(ModalEvent.FeedbackCreateModal)();
}
setFormBusy(false);
}, []);
'sentFeedback',
);
if (res) {
dispatchModalEvent(ModalEvent.FeedbackCreateModal)();
}

setFormBusy(false);
},
[createMutation],
);

const onError = useCallback((err: typeof errors) => {
Sentry.captureException(err);
Expand Down
46 changes: 0 additions & 46 deletions src/pages/api/feedback.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/schema/criteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export const criteriaSchema = z.object({
required_error: tr('Title is required'),
})
.min(1, {
message: tr('Title must be longer than 1 symbol'),
message: tr
.raw('Title must be longer than {upTo} symbol', {
upTo: 1,
})
.join(''),
}),
weight: z.string().optional(),
goalId: z.string(),
Expand Down
7 changes: 6 additions & 1 deletion src/schema/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ export const createFeedbackSchema = z.object({
invalid_type_error: tr('Title must be a string'),
})
.min(3, {
message: tr('Title must be longer than 3 symbol'),
message: tr
.raw('Title must be longer than {upTo} symbol', {
upTo: 3,
})
.join(''),
}),
description: z.string().optional(),
href: z.string().optional(),
});

export type CreateFeedback = z.infer<typeof createFeedbackSchema>;
6 changes: 5 additions & 1 deletion src/schema/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export const createFilterSchema = z.object({
invalid_type_error: tr('Title must be a string'),
})
.min(1, {
message: tr('Title must be longer than 1 symbol'),
message: tr
.raw('Title must be longer than {upTo} symbol', {
upTo: 1,
})
.join(''),
}),
mode: z.nativeEnum(FilterMode),
params: z.string().min(1),
Expand Down
9 changes: 8 additions & 1 deletion src/utils/declareSsrProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ export function declareSsrProps<T = ExternalPageProps>(
};
}

const ssrHelpers = createServerSideHelpers({ router: trpcRouter, ctx: { session }, transformer: superjson });
const ssrHelpers = createServerSideHelpers({
router: trpcRouter,
ctx: {
session,
headers: req.headers,
},
transformer: superjson,
});

const ssrTime = Date.now();

Expand Down
2 changes: 1 addition & 1 deletion trpc/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { authOptions } from '../src/utils/auth';
export const createContext = async (opts: trpcNext.CreateNextContextOptions) => {
const session = await getServerSession(opts.req, opts.res, authOptions);

return { session };
return { session, headers: opts.req.headers };
};

export type TrpcContext = inferAsyncReturnType<typeof createContext>;
29 changes: 29 additions & 0 deletions trpc/router/feedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { protectedProcedure, router } from '../trpcBackend';
import { createFeedbackSchema } from '../../src/schema/feedback';

export const feedback = router({
create: protectedProcedure
.input(createFeedbackSchema)
.mutation(async ({ ctx, input: { title, description, href } }) => {
if (!process.env.FEEDBACK_URL) {
return;
}
const { name, email, image } = ctx.session.user;
const userAgent = ctx.headers['user-agent'];
await fetch(process.env.FEEDBACK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title,
description,
href,
userAgent,
name,
email,
avatarUrl: image,
}),
});
}),
});
2 changes: 2 additions & 0 deletions trpc/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { goal } from './goal';
import { search } from './search';
import { state } from './state';
import { estimates } from './estimates';
import { feedback } from './feedback';

export const trpcRouter = router({
filter,
Expand All @@ -22,6 +23,7 @@ export const trpcRouter = router({
search,
state,
estimates,
feedback,
});

export type TrpcRouter = typeof trpcRouter;
2 changes: 1 addition & 1 deletion trpc/trpcBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const sessionCheck = t.middleware(({ next, ctx }) => {
}

return next({
ctx: { session },
ctx: { session, headers: ctx.headers },
});
});

Expand Down

0 comments on commit 5c6a23d

Please sign in to comment.