Skip to content

Commit

Permalink
Added POST to create container
Browse files Browse the repository at this point in the history
  • Loading branch information
bjmckenz committed Mar 13, 2024
1 parent d599c3d commit d6e69d5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<p>Step 5: Use a "form" to collect user data and make an API request</p>
<button on:click={() => {goto('/fetchContainer')}}>Fetch A Container</button>

<p>Step 6: See how to collect data and make a POST request</p>
<button on:click={() => {goto('/createContainer')}}>Create A Container</button>


<style>
.note {
Expand Down
49 changes: 49 additions & 0 deletions src/routes/createContainer/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<script>
import Button, { Label } from '@smui/button';
import Textfield from '@smui/textfield';
import HelperText from '@smui/textfield/helper-text';
let newContainerNumber = '';
let newDateContainerShipped = '';
let newContainerSize = '';
let newNameOfShip = '';
const createContainer = async (newContainer) => {
const res = await fetch('http://localhost:3000/container', {
method: 'POST',
body: JSON.stringify(newContainer),
headers: {
'content-type': 'application/json'
}
});
const data = await res.json();
return data.containers[0];
};
</script>

<h1>Create a Container</h1>

<Textfield variant="filled" bind:value={newContainerNumber} label="Container Number">
<HelperText slot="helper">May include letters</HelperText>
</Textfield><br />
<Textfield variant="filled" bind:value={newContainerSize} label="Size">
<HelperText slot="helper">Tons</HelperText>
</Textfield>
<br />
<Textfield variant="filled" bind:value={newDateContainerShipped} label="Date Shipped"></Textfield>
<br /><Textfield variant="filled" bind:value={newNameOfShip} label="Name of Ship"></Textfield>
<br />
<Button
variant="raised"
on:click={async () => {
const newContainer = {
containerNumber: newContainerNumber,
dateContainerShipped: newDateContainerShipped,
containerSize: newContainerSize,
nameOfShip: newNameOfShip
};
const container = await createContainer(newContainer);
console.log(container['containerNumber']);
}}
><Label>Add Container</Label></Button
>
2 changes: 1 addition & 1 deletion src/routes/fromapi/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{#each data.containers as container}
<div>
<span class="id">{container.id}</span>
<span class="id">{container.containerNumber}</span>
<span class="name">{container.nameOfShip}</span>
</div>
{/each}
Expand Down

0 comments on commit d6e69d5

Please sign in to comment.