Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: adding form clear on reload #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/lib/builderForm/BuilderForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import DescriptionTerm from '$lib/List/DescriptionTerm.svelte';
import ListItem from '$lib/List/ListItem.svelte';
import UnorderedList from '$lib/List/UnorderedList.svelte';
import Recipe from '$lib/recipes/Recipe.svelte';
import { currentBuilderStatus } from '$lib/stores.js';
import { onMount } from 'svelte';

let name: string = $currentBuilderStatus.name;
let author: AuthorInterface = $currentBuilderStatus.author;
Expand Down Expand Up @@ -81,9 +83,36 @@
input.ingredients = ingredientList;
input.instructions = instructionList;
console.log(input);
localStorage.setItem('recipe', JSON.stringify(input));
console.log(`hey did this write to local storage: ${localStorage.getItem('recipe')}`);
updateCurrentBuilderRecipe(input);
};

const onReset = () => {
currentBuilderStatus.set({
name: '',
author: { name: '', reference: '' },
description: '',
totalTime: 0,
keywords: [],
yield: '',
category: '',
cuisine: '',
nutrition: {
calories: ''
},
ingredients: [],
instructions: []
});
name = '';
author = { name: '', reference: '' };
description = '';
keywordList = [];
ingredientList = [];
instructionList = [];
recipeYield = '';
};

// uses "as any" due to some weird ts issue, fix later i guess
const updateCurrentBuilderRecipe = (input: any) => {
currentBuilderStatus.set({
Expand All @@ -104,6 +133,14 @@
console.log(currentBuilderStatus);
goto('/builder/your-recipe');
};

onMount(async () => {
if (localStorage.getItem('recipe')) {
console.log(localStorage.getItem('recipe'));
} else {
onReset();
}
});
</script>

<form class="builderForm" on:submit|preventDefault={onSubmit}>
Expand Down Expand Up @@ -307,7 +344,7 @@
<!-- Submit -->
<section class="formActions">
<Button formType="submit" variant="accent" class="submitButton">Generate Recipe</Button>
<Button formType="reset" class="resetButton">Clear</Button>
<Button formType="reset" class="resetButton" on:reset={onReset}>Clear</Button>
</section>
</form>

Expand Down
9 changes: 7 additions & 2 deletions src/routes/builder/your-recipe.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@
// const result = json;
hasSubmittedRecipe = true;
}

const editRecipe = () => {
console.log('I go edit');
console.log(localStorage.getItem('recipe'));
goto('/builder');
};
</script>

<svelte:head>
<title>your recipe | twotop</title>
<meta name="Builder | twotop" content="twotop builder recipe" />
</svelte:head>

{#if !hasSubmittedRecipe}
<section>
{#if !$isAuthenticated}
Expand All @@ -54,7 +59,7 @@
{#if $isAuthenticated}
<Button variant="accent" on:click={submitRecipe}>Submit</Button>
{/if}
<Button on:click={() => goto('/builder')}>Edit</Button>
<Button on:click={editRecipe}>Edit</Button>
</div>
{:else}
<span> No recipe built, redirecting </span>
Expand Down