-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Realworld update #290
Merged
Merged
Realworld update #290
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
54dcbd1
store user info in JWT. yeah, yeah, i know
Rich-Harris f99169b
use prefetching promise if available, reload on query change
Rich-Harris c7efcb7
inject credentials in server-side fetch
Rich-Harris e003e75
start updating realworld example
Rich-Harris 6db72d6
always supply a body when posting
Rich-Harris cc58597
swap tabs
Rich-Harris 3f0fe81
simplify setup
Rich-Harris 6363b2f
simplify settings page
Rich-Harris 6b59ee8
replace/pushState on goto
Rich-Harris 8f49289
handle redirects
Rich-Harris 8d1569f
update lockfile
Rich-Harris 1a088b2
add preloading indicator
Rich-Harris d1d9f26
fix prettier config
Rich-Harris 40010ee
remove double layout
Rich-Harris 1e7e67a
update manifest/logo
Rich-Harris 23ad4b5
remove unwanted segment
Rich-Harris 08180a4
fix settings page
Rich-Harris 5744e5b
fix following logic
Rich-Harris 298518b
various
Rich-Harris fd37ff3
only insist on a body for GET requests
Rich-Harris 096e19b
only attempt 304s for get requests
Rich-Harris 36bea7b
allow empty responses for non-GET requests
Rich-Harris 48d7a38
allow forms to override method with ?_method=delete etc
Rich-Harris bcbb033
fix comment submission/deleting, including progressive enhancement
Rich-Harris 39a6aa0
some changes i apparently made before christmas
Rich-Harris e4f39e8
merge master -> realworld-update
Rich-Harris d4d9b43
update imports
Rich-Harris 20c59c6
minor fixes
Rich-Harris fb58287
fix imports
Rich-Harris a8f277b
Merge branch 'master' into realworld-update
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix comment submission/deleting, including progressive enhancement
- Loading branch information
commit bcbb033c59b3d2cc6213640b1e92c2d780e0eb29
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const noop = () => {}; | ||
|
||
export function ajax(node, { onsubmit = noop, onresponse = noop } = {}) { | ||
const handler = async (event) => { | ||
event.preventDefault(); | ||
const body = node.method === 'post' || node.method === 'put' ? new FormData(node) : null; | ||
|
||
onsubmit(body); | ||
|
||
const response = await fetch(node.action, { | ||
method: node.method, | ||
body, | ||
headers: { | ||
accept: 'application/json' | ||
} | ||
}); | ||
|
||
onresponse(response); | ||
}; | ||
|
||
node.addEventListener('submit', handler); | ||
|
||
return { | ||
destroy() { | ||
node.removeEventListener('submit', handler); | ||
} | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const page_size = 10; | ||
export const placeholder = 'https://static.productionready.io/images/smiley-cyrus.jpg'; |
File renamed without changes.
60 changes: 60 additions & 0 deletions
60
examples/realworld.svelte.dev/src/routes/article/[slug]/_Comment.svelte
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<script> | ||
import { createEventDispatcher } from 'svelte'; | ||
import { ajax } from '$common/actions.js'; | ||
|
||
export let comment; | ||
export let slug; | ||
export let user; | ||
|
||
const dispatch = createEventDispatcher(); | ||
const onresponse = (res) => { | ||
if (res.ok) { | ||
// check the comment was deleted (e.g. we didn't | ||
// double-click and submit twice) | ||
dispatch('deleted'); | ||
} | ||
}; | ||
</script> | ||
|
||
<div class="card"> | ||
<div class="card-block"> | ||
<p class="card-text">{comment.body}</p> | ||
</div> | ||
|
||
<div class="card-footer"> | ||
<a href="/profile/@{comment.author.username}" class="comment-author"> | ||
<img src={comment.author.image} class="comment-author-img" alt={comment.author.username} /> | ||
</a> | ||
|
||
<a | ||
href="/profile/@{comment.author.username}" | ||
class="comment-author" | ||
>{comment.author.username}</a> | ||
|
||
<span class="date-posted"> {new Date(comment.createdAt).toDateString()} </span> | ||
|
||
{#if user && comment.author.username === user.username} | ||
<form | ||
action="/article/{slug}/comments/{comment.id}.json?_method=delete" | ||
method="post" | ||
class="mod-options" | ||
use:ajax={{ onresponse }} | ||
> | ||
<button class="ion-trash-a" /> | ||
</form> | ||
{/if} | ||
</div> | ||
</div> | ||
|
||
<style> | ||
button { | ||
background: none; | ||
border: none; | ||
padding: 0; | ||
margin: 0; | ||
font-size: inherit; | ||
margin-left: 5px; | ||
opacity: 0.6; | ||
cursor: pointer; | ||
} | ||
</style> |
36 changes: 36 additions & 0 deletions
36
examples/realworld.svelte.dev/src/routes/article/[slug]/_CommentContainer.svelte
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<script> | ||
import CommentInput from './_CommentInput.svelte'; | ||
import Comment from './_Comment.svelte'; | ||
|
||
export let comments; | ||
export let slug; | ||
export let user; | ||
</script> | ||
|
||
<div class="col-xs-12 col-md-8 offset-md-2"> | ||
{#if user} | ||
<div> | ||
<CommentInput | ||
{slug} | ||
{user} | ||
on:commented={({ detail }) => (comments = [detail.comment, ...comments])} | ||
/> | ||
</div> | ||
{:else} | ||
<p> | ||
<a href="/login">Sign in</a> | ||
or | ||
<a href="/register">sign up</a> | ||
to add comments on this article. | ||
</p> | ||
{/if} | ||
|
||
{#each comments as comment, i (comment.id)} | ||
<Comment | ||
{comment} | ||
{slug} | ||
{user} | ||
on:deleted={() => (comments = [...comments.slice(0, i), ...comments.slice(i + 1)])} | ||
/> | ||
{/each} | ||
</div> |
52 changes: 52 additions & 0 deletions
52
examples/realworld.svelte.dev/src/routes/article/[slug]/_CommentInput.svelte
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script> | ||
import { createEventDispatcher } from 'svelte'; | ||
import { placeholder } from '$common/constants.js'; | ||
import { ajax } from '$common/actions.js'; | ||
|
||
export let slug; | ||
export let user; | ||
|
||
const dispatch = createEventDispatcher(); | ||
|
||
let body = ''; | ||
let submitting = false; | ||
|
||
const onsubmit = () => { | ||
submitting = true; | ||
}; | ||
|
||
const onresponse = async (res) => { | ||
if (res.ok) { | ||
const comment = await res.json(); | ||
dispatch('commented', { comment }); | ||
body = ''; | ||
} else { | ||
// TODO error handling | ||
} | ||
|
||
submitting = false; | ||
}; | ||
</script> | ||
|
||
<form | ||
action="/article/{slug}/comments.json" | ||
method="post" | ||
class="card comment-form" | ||
use:ajax={{ onsubmit, onresponse }} | ||
> | ||
<div class="card-block"> | ||
<textarea | ||
disabled={submitting} | ||
class="form-control" | ||
name="comment" | ||
placeholder="Write a comment..." | ||
bind:value={body} | ||
rows="3" | ||
/> | ||
</div> | ||
|
||
<div class="card-footer"> | ||
<img src={user.image || placeholder} class="comment-author-img" alt={user.username} /> | ||
<button disabled={submitting} class="btn btn-sm btn-primary" type="submit">Post Comment</button> | ||
</div> | ||
</form> |
14 changes: 14 additions & 0 deletions
14
examples/realworld.svelte.dev/src/routes/article/[slug]/comments/[id].json.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as api from '$common/api.js'; | ||
|
||
export async function del(request, context) { | ||
if (!context.user) { | ||
return { status: 401 }; | ||
} | ||
|
||
const { slug, id } = request.params; | ||
const { status, error } = await api.del(`articles/${slug}/comments/${id}`, context.user.token); | ||
|
||
if (error) { | ||
return { status, body: { error } }; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
examples/realworld.svelte.dev/src/routes/article/[slug]/comments/index.json.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as api from '$common/api.js'; | ||
|
||
export async function get(request, context) { | ||
const { slug } = request.params; | ||
const { comments } = await api.get( | ||
`articles/${slug}/comments`, | ||
context.user && context.user.token | ||
); | ||
|
||
return { | ||
body: comments | ||
}; | ||
} | ||
|
||
export async function post(request, context) { | ||
if (!context.user) { | ||
return { status: 401 }; | ||
} | ||
|
||
const { slug } = request.params; | ||
const body = request.body.get('comment'); | ||
|
||
const { comment } = await api.post( | ||
`articles/${slug}/comments`, | ||
{ comment: { body } }, | ||
context.user.token | ||
); | ||
|
||
// for AJAX requests, return the newly created comment | ||
if (request.headers.accept === 'application/json') { | ||
return { | ||
status: 201, // created | ||
body: comment | ||
}; | ||
} | ||
|
||
// for traditional (no-JS) form submissions, redirect | ||
// back to the article page | ||
console.log(`redirecting to /article/${slug}`); | ||
return { | ||
status: 303, // see other | ||
headers: { | ||
location: `/article/${slug}` | ||
} | ||
}; | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/realworld.svelte.dev/src/routes/article/[slug]/index.json.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as api from '$common/api.js'; | ||
|
||
export async function get(request, context) { | ||
const { slug } = request.params; | ||
const { article } = await api.get(`articles/${slug}`, context.user && context.user.token); | ||
|
||
return { | ||
body: article | ||
}; | ||
} |
This file contains 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
39 changes: 0 additions & 39 deletions
39
examples/realworld.svelte.dev/src/routes/article/_Comment.svelte
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I somewhat wonder if this would be a nice helper to have in SvelteKit. (I might call it
formHandler
instead ofajax
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something like this should be easy to reuse, though I don't know if this is exactly the right thing — it does some slightly opinionated stuff (like setting the
accept
header, so that a no-JS form submission can get a 303 response while an AJAX submission gets the posted data), and we'd need to be more careful about the design if it wasn't just a little helper inside one of the example apps.But yeah, would be nice to have some version of this in the toolkit, though maybe in Svelte itself rather than SvelteKit-specific (sveltejs/rfcs#24)