fix: use review-schema and rtk loading-state
This commit is contained in:
parent
e50187dea1
commit
b405fc315b
@ -4,16 +4,12 @@ import { z } from 'zod';
|
|||||||
import { requestTaylor } from '@/app/api-utlities/clients/taylor.client';
|
import { requestTaylor } from '@/app/api-utlities/clients/taylor.client';
|
||||||
import { createReviewMutation } from '@/app/api-utlities/requests/common';
|
import { createReviewMutation } from '@/app/api-utlities/requests/common';
|
||||||
|
|
||||||
|
import { reviewSchema } from '@/features/review-form/model/review-form.schema';
|
||||||
|
|
||||||
export const POST = async (req: NextRequest) => {
|
export const POST = async (req: NextRequest) => {
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
|
|
||||||
const validatedRequest = z
|
const validatedRequest = reviewSchema.parse(body);
|
||||||
.object({
|
|
||||||
name: z.string(),
|
|
||||||
text: z.string(),
|
|
||||||
rating: z.number().min(0).max(5),
|
|
||||||
})
|
|
||||||
.parse(body);
|
|
||||||
|
|
||||||
await requestTaylor(
|
await requestTaylor(
|
||||||
{ mutation: createReviewMutation },
|
{ mutation: createReviewMutation },
|
||||||
|
|||||||
@ -33,10 +33,9 @@ import { ReviewFormValues, reviewSchema } from '../model/review-form.schema';
|
|||||||
|
|
||||||
export function ReviewForm() {
|
export function ReviewForm() {
|
||||||
const [openReviewFormDialog, setOpenReviewFormDialog] = useState(false);
|
const [openReviewFormDialog, setOpenReviewFormDialog] = useState(false);
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
||||||
const [hoveredStar, setHoveredStar] = useState(0);
|
const [hoveredStar, setHoveredStar] = useState(0);
|
||||||
|
|
||||||
const [createReview] = useCreateReviewMutation();
|
const [createReview, { isLoading: isSubmitting }] = useCreateReviewMutation();
|
||||||
|
|
||||||
const form = useForm<ReviewFormValues>({
|
const form = useForm<ReviewFormValues>({
|
||||||
resolver: zodResolver(reviewSchema),
|
resolver: zodResolver(reviewSchema),
|
||||||
@ -48,8 +47,6 @@ export function ReviewForm() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = async (data: ReviewFormValues) => {
|
const onSubmit = async (data: ReviewFormValues) => {
|
||||||
setIsSubmitting(true);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await createReview(data);
|
await createReview(data);
|
||||||
|
|
||||||
@ -69,8 +66,6 @@ export function ReviewForm() {
|
|||||||
duration: 5000,
|
duration: 5000,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} finally {
|
|
||||||
setIsSubmitting(false);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user