-
-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1408 from analogjs/beta
chore: release 1.9.0
- Loading branch information
Showing
111 changed files
with
1,894 additions
and
987 deletions.
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
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
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
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
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,96 @@ | ||
import { Directive, inject, input, output } from '@angular/core'; | ||
import { ActivatedRoute, Params, Router } from '@angular/router'; | ||
import { injectRouteEndpointURL } from '@analogjs/router'; | ||
|
||
@Directive({ | ||
selector: 'form[action],form[method]', | ||
host: { | ||
'(submit)': `submitted($event)`, | ||
}, | ||
standalone: true, | ||
}) | ||
export class FormAction { | ||
action = input<string>(''); | ||
onSuccess = output<unknown>(); | ||
onError = output<unknown>(); | ||
state = output< | ||
'submitting' | 'error' | 'redirect' | 'success' | 'navigate' | ||
>(); | ||
private router = inject(Router); | ||
private route = inject(ActivatedRoute); | ||
private path = this._getPath(); | ||
|
||
submitted($event: { target: HTMLFormElement } & Event) { | ||
$event.preventDefault(); | ||
|
||
this.state.emit('submitting'); | ||
const body = new FormData($event.target); | ||
|
||
if ($event.target.method.toUpperCase() === 'GET') { | ||
this._handleGet(body, this.path); | ||
} else { | ||
this._handlePost(body, this.path, $event); | ||
} | ||
} | ||
|
||
private _handleGet(body: FormData, path: string) { | ||
const params: Params = {}; | ||
body.forEach((formVal, formKey) => (params[formKey] = formVal)); | ||
|
||
this.state.emit('navigate'); | ||
this.router.navigate([path], { | ||
queryParams: params, | ||
onSameUrlNavigation: 'reload', | ||
}); | ||
} | ||
|
||
private _handlePost( | ||
body: FormData, | ||
path: string, | ||
$event: { target: HTMLFormElement } & Event | ||
) { | ||
fetch(path, { | ||
method: $event.target.method, | ||
body, | ||
}) | ||
.then((res) => { | ||
if (res.ok) { | ||
if (res.redirected) { | ||
const redirectUrl = new URL(res.url).pathname; | ||
this.state.emit('redirect'); | ||
this.router.navigate([redirectUrl]); | ||
} else if (res.headers.get('Content-type') === 'application/json') { | ||
res.json().then((result) => { | ||
this.onSuccess.emit(result); | ||
this.state.emit('success'); | ||
}); | ||
} else { | ||
res.text().then((result) => { | ||
this.onSuccess.emit(result); | ||
this.state.emit('success'); | ||
}); | ||
} | ||
} else { | ||
if (res.headers.get('X-Analog-Errors')) { | ||
res.json().then((errors: unknown) => { | ||
this.onError.emit(errors); | ||
this.state.emit('error'); | ||
}); | ||
} else { | ||
this.state.emit('error'); | ||
} | ||
} | ||
}) | ||
.catch((_) => { | ||
this.state.emit('error'); | ||
}); | ||
} | ||
|
||
private _getPath() { | ||
if (this.route) { | ||
return injectRouteEndpointURL(this.route.snapshot).pathname; | ||
} | ||
|
||
return `/api/_analog/pages${window.location.pathname}`; | ||
} | ||
} |
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 @@ | ||
import { Component, signal } from '@angular/core'; | ||
|
||
import { FormAction } from './form-action.directive'; | ||
|
||
type FormErrors = | ||
| { | ||
email?: string; | ||
} | ||
| undefined; | ||
|
||
@Component({ | ||
selector: 'app-newsletter-page', | ||
standalone: true, | ||
imports: [FormAction], | ||
template: ` | ||
<h3>Newsletter Signup</h3> | ||
@if (!signedUp()) { | ||
<form | ||
method="post" | ||
(onSuccess)="onSuccess()" | ||
(onError)="onError($any($event))" | ||
(onStateChange)="errors.set(undefined)" | ||
> | ||
<div> | ||
<label for="email"> Email </label> | ||
<input type="email" name="email" /> | ||
</div> | ||
<button class="button" type="submit">Submit</button> | ||
</form> | ||
@if( errors()?.email ) { | ||
<p>{{ errors()?.email }}</p> | ||
} } @else { | ||
<div>Thanks for signing up!</div> | ||
} | ||
`, | ||
}) | ||
export default class NewsletterComponent { | ||
signedUp = signal(false); | ||
errors = signal<FormErrors>(undefined); | ||
|
||
onSuccess() { | ||
this.signedUp.set(true); | ||
} | ||
|
||
onError(result?: FormErrors) { | ||
this.errors.set(result); | ||
console.log({ result }); | ||
} | ||
} |
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,30 @@ | ||
import { | ||
type PageServerAction, | ||
redirect, | ||
json, | ||
fail, | ||
} from '@analogjs/router/server/actions'; | ||
import { readFormData } from 'h3'; | ||
|
||
export function load() { | ||
return { | ||
loaded: true, | ||
}; | ||
} | ||
|
||
export async function action({ event }: PageServerAction) { | ||
const body = await readFormData(event); | ||
const email = body.get('email') as string; | ||
|
||
if (!email) { | ||
return fail(422, { email: 'Email is required' }); | ||
} | ||
|
||
if (email.length < 10) { | ||
return redirect('/'); | ||
} | ||
|
||
console.log({ email: body.get('email') }); | ||
|
||
return json({ type: 'success' }); | ||
} |
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,32 @@ | ||
import { Component, computed } from '@angular/core'; | ||
import { toSignal } from '@angular/core/rxjs-interop'; | ||
import { injectLoad } from '@analogjs/router'; | ||
|
||
import { FormAction } from './form-action.directive'; | ||
import type { load } from './search.server'; | ||
|
||
@Component({ | ||
selector: 'app-search-page', | ||
standalone: true, | ||
imports: [FormAction], | ||
template: ` | ||
<h3>Search</h3> | ||
<form method="get"> | ||
<div> | ||
<label for="search"> Search </label> | ||
<input type="text" name="search" [value]="searchTerm()" /> | ||
</div> | ||
<button class="button" type="submit">Submit</button> | ||
</form> | ||
@if(searchTerm()) { | ||
<p>Search Term: {{ searchTerm() }}</p> | ||
} | ||
`, | ||
}) | ||
export default class NewsletterComponent { | ||
loader = toSignal(injectLoad<typeof load>(), { requireSync: true }); | ||
searchTerm = computed(() => this.loader().searchTerm); | ||
} |
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,12 @@ | ||
import type { PageServerLoad } from '@analogjs/router'; | ||
import { getQuery } from 'h3'; | ||
|
||
export async function load({ event }: PageServerLoad) { | ||
const query = getQuery(event); | ||
console.log('loaded search', query['search']); | ||
|
||
return { | ||
loaded: true, | ||
searchTerm: `${query['search']}`, | ||
}; | ||
} |
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
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
Oops, something went wrong.