#Is it silly to POST to a server action to get redirected to another URL?
3 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
async function handleSearch(formData: FormData) {
"use server";
const values = Object.fromEntries(formData.entries());
const { query, category, maxPrice, zipcode } =
advertisementFilterSchema.parse(values);
const searchParams = new
...(query && { query }),
...(category && { category }),
...(maxPrice && { maxPrice }),
...(zipcode && { zipcode }),
});
redirect(`/?${searchParams.toString()}`);
}
interface FilterBarProps {
defaultValues: FilterValues;
}
export default function FilterBar({ defaultValues }: FilterBarProps) {
return (
<div>
<p>Filter results</p>
<form action={handleSearch}>
<Input
name="query"
type="search"
placeholder="What are you looking for?"
defaultValue={defaultValues.query}
/>
<Input
name="zipcode"
type="text"
placeholder="Zipcode"
defaultValue={defaultValues.zipcode}
/>
<div className="relative">
<select
name="category"
defaultValue={defaultValues.category || "all"}
>
<option value="all">All categories</option>
{categories.map((category) => (
<option key={category} value={category}>
{category}
</option>
))}
</select>
</div>
<Input
name="maxPrice"
type="text"
placeholder="Max price"
defaultValue={defaultValues.maxPrice}
/>
// This uses useFormStatus to show a loading indicator
<FormSubmitButton>
<Search size={20} /> Find
</FormSubmitButton>
</form>
</div>
);
}