Skip to content

Commit

Permalink
fix(router): properly parse json response in the FormAction
Browse files Browse the repository at this point in the history
  • Loading branch information
milosdanilov committed Feb 10, 2025
1 parent 16e6ba9 commit 4c703fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
24 changes: 15 additions & 9 deletions apps/analog-app/src/app/pages/cart.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { CurrencyPipe } from '@angular/common';
import { Component, inject } from '@angular/core';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { RouterLinkWithHref } from '@angular/router';
import { FormAction } from '@analogjs/router';

import { CartService } from '../cart.service';

import { type CartSubmitResponse } from './cart.server';

@Component({
selector: 'app-cart',
imports: [RouterLinkWithHref, CurrencyPipe, ReactiveFormsModule],
imports: [RouterLinkWithHref, CurrencyPipe, ReactiveFormsModule, FormAction],
template: `
<h3>Cart</h3>
Expand All @@ -16,14 +19,17 @@ import { CartService } from '../cart.service';
</p>
@for (item of items; track $index) {
<div class="cart-item">
<span>{{ item.name }} </span>
<span>{{ item.price | currency }}</span>
</div>
<div class="cart-item">
<span>{{ item.name }} </span>
<span>{{ item.price | currency }}</span>
</div>
}
<form [formGroup]="checkoutForm" (ngSubmit)="onSubmit()">
<form
method="post"
[formGroup]="checkoutForm"
(onSuccess)="onSuccess($any($event))"
>
<div>
<label for="name"> Name </label>
<input id="name" type="text" formControlName="name" />
Expand All @@ -49,10 +55,10 @@ export default class CartComponent {
address: '',
});

onSubmit(): void {
onSuccess(res: CartSubmitResponse): void {
// Process checkout data here
this.items = this.cartService.clearCart();
console.warn('Your order has been submitted', this.checkoutForm.value);
console.warn(res.message, this.checkoutForm.value);
this.checkoutForm.reset();
}
}
13 changes: 13 additions & 0 deletions apps/analog-app/src/app/pages/cart.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type PageServerAction, json } from '@analogjs/router/server/actions';

export type CartSubmitResponse = {
type: 'success';
message: string;
};

export async function action({ event }: PageServerAction) {
return json<CartSubmitResponse>({
type: 'success',
message: 'Your order has been submitted',
});
}
9 changes: 8 additions & 1 deletion packages/router/src/lib/form-action.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class FormAction {
const redirectUrl = new URL(res.url).pathname;
this.state.emit('redirect');
this.router.navigate([redirectUrl]);
} else if (res.headers.get('Content-type') === 'application/json') {
} else if (this._isJSON(res.headers.get('Content-type'))) {
res.json().then((result) => {
this.onSuccess.emit(result);
this.state.emit('success');
Expand Down Expand Up @@ -95,4 +95,11 @@ export class FormAction {

return `/api/_analog/pages${window.location.pathname}`;
}

private _isJSON(contentType: string | null): boolean {
const mime = contentType ? contentType.split(';') : [];
const essence = mime[0];

return essence === 'application/json';
}
}

0 comments on commit 4c703fb

Please sign in to comment.