"use client" import { useState, useEffect } from "react"; import { zodResolver } from "@hookform/resolvers/zod" import { useFieldArray, useForm } from "react-hook-form" import { z } from "zod" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Checkbox } from "@/components/ui/checkbox" import FetchWrapper from '@/hooks/FetchWrapper'; import { toast } from "sonner" import { Spinner } from '@/components/ui/spinner'; // Update the schema const displayFormSchema = z.object({ views_label: z.string().optional(), display_style: z.array(z.string()).default([]).optional(), post_type: z.array(z.string()).optional(), page_type: z.array(z.string()).default([]).optional(), user_type: z.array(z.string()).optional(), position: z.string().default("after"), }) type DisplayFormValues = z.infer // Update default values const defaultValues: Partial = { views_label: "", display_style: [], post_type: [], page_type: [], position: "after", user_type: [], } export function DisplayForm() { const api = new FetchWrapper(advico_plugin.pluginApiUrl, advico_plugin.nonce); const [settings, setSettings] = useState(null); const [isLoading, setIsLoading] = useState(true); const form = useForm({ resolver: zodResolver(displayFormSchema), defaultValues, mode: "onChange", }) useEffect(() => { document.title = `Display Settings | ${advico_plugin.pluginName}`; api.get(`/settings/display`) .then(data => { const response = data.data; setSettings(response); setIsLoading(false); // Ambil semua nilai selected dari post_types // const selectedPostTypes = Object.entries(response.post_types || {}) // .filter(([_, item]: [string, any]) => item.selected) // .map(([id]) => id); const selectedPostTypes = (response.post_types || []) .filter((item: any) => item.selected) .map((item: any) => item.id); // Ambil semua nilai selected dari page_types const selectedPageTypes = (response.page_types || []) .filter((item: any) => item.selected) .map((item: any) => item.id); const selectedDisplayStyles = (response.display_styles || []) .filter((item: any) => item.selected) .map((item: any) => item.id); const selectedUserTypes = (response.user_types || []) .filter((item: any) => item.selected) .map((item: any) => item.id); // Reset nilai form form.reset({ views_label: response.views_label, display_style: selectedDisplayStyles, post_type: selectedPostTypes, page_type: selectedPageTypes, position: response.position || "after", user_type: selectedUserTypes, }); }) .catch(error => { toast.error('Error Fetching Data', { description: error }); }); }, []); // Remove unused useFieldArray // const { fields, append } = useFieldArray({ // name: "urls", // control: form.control, // }) const [isSubmitting, setIsSubmitting] = useState(false); function onSubmit(data: DisplayFormValues) { setIsSubmitting(true); api.put('/settings/display/update', JSON.stringify(data)) .then(response => { toast.success('Settings updated successfully', { description: 'Your display settings have been saved.' }); setIsSubmitting(false); }) .catch(error => { console.error("Error updating settings:", error); toast.error('Failed to update settings', { description: 'Please try again later.' }); setIsSubmitting(false); }); } if (isLoading) { return (
Loading...
); } return (
( Views Label Enter the label for the post views counter field. )} /> {/* Display Style */} (
Display Style Choose how to display the post views counter
{settings?.display_styles?.map((item) => ( ( { return checked ? field.onChange([...field.value, item.id]) : field.onChange( field.value?.filter( (value) => value !== item.id ) ) }} /> {item.label} )} /> ))}
)} /> {/* Post Type */} (
Post Type Select post types for which the views count will be displayed.
{settings?.post_types.map((item) => ( { const currentValue = field.value || []; if (checked) { field.onChange([...currentValue, item.id]); } else { field.onChange(currentValue.filter((value) => value !== item.id)); } }} /> {item.label} ))}
)} /> {/* Page Type */} (
Page Type Select page types for which the views count will be displayed.
{!isLoading && settings?.page_types?.map((item) => ( ( { const currentValue = field.value || []; return checked ? field.onChange([...currentValue, item.id]) : field.onChange(currentValue.filter((v) => v !== item.id)); }} /> {item.label} )} /> ))}
)} /> {/* Position */} ( Widget Position Select where would you like to display the post views counter. )} /> {/* User Type */} (
User Type Use it to hide the views counter from selected type of visitors.
{settings?.user_types.map((item) => ( { const currentValue = field.value || []; if (checked) { field.onChange([...currentValue, item.id]); } else { field.onChange(currentValue.filter((value) => value !== item.id)); } }} /> {item.label} ))}
)} /> ) }