Comments (7)
After the action has been executed, your loader will automatically be re-executed as well. The goal must therefore be to get the search parameter into your loader. Maybe that works with a cookie. It's best to ask here on the Qwik Discord. I still don't know enough about Qwik to answer this.
from modular-forms.
Can you let me know what your solution is? And if you don't find a solution, I might have some ideas for workarounds.
from modular-forms.
Will do, I've had no response yet. I was hoping to do something like this:
export const useRestaurants = (searchPhrase?: string) =>
routeLoader$(() => async () => {
return await prisma.restaurant.findMany({
where: {
name: {
contains: searchPhrase,
},
},
});
});
export const useSearchFormLoader = routeLoader$<InitialValues<SearchForm>>(() => ({
searchPhrase: "",
}));
export const useSearchFormAction = formAction$<SearchForm>((values, { redirect, query }) => {
redirect(302, "?searchPhrase=" + values.searchPhrase);
}, zodForm$(searchSchema));
export default component$(() => {
// get the search phrase from the query string
const query = useLocation().url.searchParams;
const searchPhrase = query.get("searchPhrase");
return (
<Main>
<H1>Discover Northern Ireland's Best Restaurants</H1>
<P>
Find the perfect spot for your next meal in Belfast, Derry, or anywhere in between
with our directory of top-rated restaurants in Northern Ireland. Simply enter your
address or location to browse our listings and discover everything from cozy local
favorites to fine dining experiences. Our comprehensive directory makes it easy to
treat your taste buds to a culinary adventure, with something for every taste and
budget. Start exploring today!
</P>
<SearchForm />
<DivGrid>
<Resource
value={useRestaurants(searchPhrase ?? undefined)()}
onPending={() => <div>Loading restaurants...</div>}
onResolved={(restaurants) => (
<>
{restaurants &&
restaurants.map((restaurant) => (
<RestaurantCard restaurant={restaurant} />
))}
</>
)}
/>
</DivGrid>
</Main>
);
});
But Qwik isn't a fan.. Next option is to just use a server$ function on browser submit after validation. Though that won't actually get the routerLoader$ to update.. Maybe I need to use something else instead of routeLoader$ 🤔
from modular-forms.
Optionally, you can also return data via formAction$
, which you can then retrieve via searchForm.response.data
. However, the originally planned implementation via routeLoader$
would of course be better.
type Restaurant = { … };
export const useFormAction = formAction$<SearchForm, Restaurant[]>((values) => {
return {
data: await prisma.restaurant.findMany({ … }),
};
});
from modular-forms.
export const useSearchFormLoader = routeLoader$<InitialValues<SearchForm>>(() => ({
searchPhrase: "",
}));
export const useSearchFormAction = formAction$<SearchForm>((values, { redirect, query }) => {
return redirect(302, "?searchPhrase=" + encodeURIComponent(values.searchPhrase));
}, zodForm$(searchSchema));
export default component$(() => {
const loc = useLocation();
const searchPhrase = loc.url.searchParams.get("searchPhrase");
const restaurantResource = useResource$<Restaurant[]>(async (ctx) => {
ctx.track(() => searchPhrase);
return await prisma.restaurant.findMany(
searchPhrase
? {
where: {
OR: [
{ name: { contains: searchPhrase } },
{ location: { contains: searchPhrase } },
{ postcode: { contains: searchPhrase } },
],
},
}
: undefined
);
});
return (
<Main>
<H1>Discover Northern Ireland's Best Restaurants</H1>
<P>
Find the perfect spot for your next meal in Belfast, Derry, or anywhere in between
with our directory of top-rated restaurants in Northern Ireland. Simply enter your
address or location to browse our listings and discover everything from cozy local
favorites to fine dining experiences. Our comprehensive directory makes it easy to
treat your taste buds to a culinary adventure, with something for every taste and
budget. Start exploring today!
</P>
<SearchForm />
<DivGrid>
<Resource
value={restaurantResource}
onPending={() => <div>Loading restaurants...</div>}
onResolved={(restaurants) => (
<>
{restaurants &&
restaurants.map((restaurant) => (
<RestaurantCard restaurant={restaurant} />
))}
</>
)}
/>
</DivGrid>
</Main>
);
});
So this is where I've landed so far; problem is the redirect causes a full page reload. I wonder could I access the values returned from the useSeachFormLoader
from modular-forms.
So this is where I've landed so far; problem is the redirect causes a full page reload. I wonder could I access the values returned from the
useSeachFormLoader
Was that a question for me? If so, I need more details.
from modular-forms.
Since it doesn't currently look like Modular Forms can do anything about this "problem", I'm closing the issue. I will reopen it if there are other signs.
from modular-forms.
Related Issues (20)
- define form without defualt values HOT 1
- Next App directory support HOT 2
- sveltekit support? HOT 1
- Qwik: Form not working when schema is declared inside component$ HOT 9
- Qwik: Latest qwik release breaks types HOT 14
- Enhance FieldStore to Return Multiple Errors per Field HOT 2
- Phone Number Input HOT 1
- Allow setting initial form values of a mounted component HOT 2
- Preact: What is the best method to pull values of fields in the form for using in computed() HOT 4
- [Question] Is it possible to have optional fields? HOT 7
- Import error in formAction for qwik HOT 1
- Checkbox works improperly when use modular forms with kobalte HOT 7
- [Question] Is it possible to have custom input without ref? HOT 5
- When useFormStore is in a seperate file, it fails HOT 1
- Select component with number types HOT 3
- Using modular form Fields inside solidjs Dynamic component HOT 1
- Qwik: Vite is trying to externalize packages used in formAction$ HOT 3
- `requestSubmit` is not a function on Safari < 16 HOT 3
- Does this library support React Native? HOT 1
- Internal issue: Cannot read properties of null (reading 'internal') HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modular-forms.