Implement progressive enhancement for yeast swapper with state persistence
Some checks failed
CI / update (push) Failing after 9s
Some checks failed
CI / update (push) Failing after 9s
- Add server-side form handling for yeast swapping without JavaScript - Implement toggle-based URL parameter system (y0=1, y1=1) for clean URLs - Add server action to toggle yeast flags and preserve all URL state - Update multiplier forms to preserve yeast toggle states across submissions - Calculate yeast conversions server-side from original recipe data - Fix {{multiplier}} placeholder replacement to handle non-numeric amounts - Enable multiple independent yeast swappers with full state preservation - Maintain perfect progressive enhancement: works with and without JS 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
29
src/routes/rezepte/[name]/+page.server.ts
Normal file
29
src/routes/rezepte/[name]/+page.server.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export const actions = {
|
||||
swapYeast: async ({ request, url }) => {
|
||||
const formData = await request.formData();
|
||||
const yeastId = parseInt(formData.get('yeastId') as string);
|
||||
|
||||
// Build new URL
|
||||
const newUrl = new URL(url.pathname, url.origin);
|
||||
|
||||
// Restore all parameters from the form data (they were submitted as currentParam_*)
|
||||
for (const [key, value] of formData.entries()) {
|
||||
if (key.startsWith('currentParam_')) {
|
||||
const paramName = key.substring('currentParam_'.length);
|
||||
newUrl.searchParams.set(paramName, value as string);
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle the yeast flag - if it exists, remove it; if not, add it
|
||||
const yeastParam = `y${yeastId}`;
|
||||
if (newUrl.searchParams.has(yeastParam)) {
|
||||
newUrl.searchParams.delete(yeastParam);
|
||||
} else {
|
||||
newUrl.searchParams.set(yeastParam, '1');
|
||||
}
|
||||
|
||||
throw redirect(303, newUrl.toString());
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user