Skip to content

Commit

Permalink
Add an active review switch at the ReviewsTable and improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMejia77 committed Aug 23, 2024
1 parent 7f09462 commit 618d48b
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 34 deletions.
177 changes: 177 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-switch": "^1.1.0",
"@types/react-star-ratings": "^2.3.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
Expand Down
4 changes: 3 additions & 1 deletion src/actions/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { Review } from "@/types";

export const getReviews = async (): Promise<Review[]> => {
try {
const response = await fetch(`${REVIEWS_API}/reviews`);
const response = await fetch(`${REVIEWS_API}/reviews`, {
cache: "no-store"
});
const { data } = await response.json();
return data ?? [];
} catch (_) {
Expand Down
44 changes: 25 additions & 19 deletions src/app/(admin)/atc24$rw/admin/components/ReviewForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Textarea } from "@/components/ui/textarea";
import { useDropzone } from "react-dropzone";
import Image from "next/image";
import { REVIEWS_API } from "@/lib/constants";
import { createFormData } from "@/lib/utils";

interface ReviewFormProps {
review?: Review;
Expand Down Expand Up @@ -52,21 +53,18 @@ export const ReviewForm = ({ review }: ReviewFormProps) => {
rating: review?.rating || "5",
user: review?.user || "",
active: review?.active || "false",
date: review?.date || new Date().toISOString(),
},
});

const handleCreate = (
values: z.infer<typeof reviewSchema>,
token: string
) => {
const formData = new FormData();
formData.append("review", values.review);
formData.append("rating", String(values.rating));
formData.append("user", values.user);
formData.append("date", new Date().toISOString());
formData.append("file", acceptedFiles[0]);
formData.append("active", JSON.parse(values.active || "false") ? "true" : "false");

const formData: FormData = createFormData({
...values,
file: acceptedFiles[0],
});
startTransition(() => {
createReview(formData, token)
.then((data) => {
Expand All @@ -84,16 +82,12 @@ export const ReviewForm = ({ review }: ReviewFormProps) => {
};

const handleUpdate = (review: Review, token: string) => {
const formData = new FormData();
formData.append("id", review.id);
formData.append("review", review.review);
formData.append("rating", String(review.rating));
formData.append("user", review.user);
formData.append("date", new Date().toISOString());
formData.append("file", acceptedFiles[0] || new File([], "null", {
type: "image/png",
}));
formData.append("active", review.active ? "true" : "false");
const formData: FormData = createFormData({
...review,
file: acceptedFiles[0] || new File([], "null", {
type: "image/png",
})
});
startTransition(() => {
updateReview(formData, token)
.then((data) => {
Expand Down Expand Up @@ -142,6 +136,7 @@ export const ReviewForm = ({ review }: ReviewFormProps) => {
{...field}
placeholder="Texto de la reseña"
className="max-w-full h-[150px] resize-none"
maxLength={600}
/>
</FormControl>
<FormMessage />
Expand Down Expand Up @@ -218,7 +213,7 @@ export const ReviewForm = ({ review }: ReviewFormProps) => {
type="checkbox"
id="show"
className="w-5 h-5"
checked={JSON.parse(field.value || 'false') ? true : false}
checked={JSON.parse(field.value)}
/>
<label
htmlFor="show"
Expand All @@ -231,6 +226,17 @@ export const ReviewForm = ({ review }: ReviewFormProps) => {
</FormItem>
)}
/>
<FormField
control={form.control}
name="date"
render={({ field }) => (
<FormItem className="hidden">
<FormControl>
<Input type="hidden" {...field} />
</FormControl>
</FormItem>
)}
/>
<Button
className="w-full bg-primary-lm hover:bg-red-600 dark:text-white"
disabled={isPending}
Expand Down
Loading

0 comments on commit 618d48b

Please sign in to comment.