Created with svelte.technology/repl
-
-
Save natec425/ccf4cc4ff12b9eea72641e51b93b1eee to your computer and use it in GitHub Desktop.
New Student Form - MagnoliaJS 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import NewStudentForm from './NewStudentForm.svelte' | |
import CurrentStudentsList from './CurrentStudentsList.svelte' | |
import TimezoneGroups from './TimezoneGroups.svelte' | |
</script> | |
<NewStudentForm /> | |
<CurrentStudentsList /> | |
<TimezoneGroups /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { students } from './students.js' | |
</script> | |
<h2>Current Students</h2> | |
{#each $students as student} | |
<p>{student.name} - {student.timezone}</p> | |
{:else} | |
<p>No Students!</p> | |
{/each} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"svelte": true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form on:submit={addNewStudent}> | |
<input bind:value={name}> | |
<input bind:value={timezone}> | |
<button>New Student!</button> | |
</form> | |
<script> | |
import { students } from './students.js' | |
let name = '' | |
let timezone = '' | |
function addNewStudent(event) { | |
event.preventDefault() | |
students.update(currentStudents => | |
[...currentStudents, { name, timezone } ] | |
) | |
event.target.reset() | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { writable } from 'svelte/store' | |
export const students = writable([]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { derived } from 'svelte/store' | |
import { students } from './students.js' | |
export const timezoneGroups = derived( | |
students, | |
students => groupByTimezone(students), | |
[] | |
) | |
function groupByTimezone(students) { | |
const groups = {} | |
for (const {name, timezone} of students) { | |
if (groups[timezone]) { | |
groups[timezone].push(name) | |
} else { | |
groups[timezone] = [name] | |
} | |
} | |
const result = Object.entries(groups) | |
.map(([timezone, students]) => | |
({timezone, students})) | |
console.log(result) | |
return result | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { timezoneGroups } from './timezoneGroups.js' | |
</script> | |
<h2>Timezone Groups</h2> | |
{#each $timezoneGroups as group} | |
<h3>{group.timezone}</h3> | |
<ul> | |
{#each group.students as student} | |
<li>{student}</li> | |
{/each} | |
</ul> | |
{/each} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment