Skip to content

Commit 2e281a1

Browse files
committed
feat(gifts): add redeem action and improve redeem UI state
Add a redeem member action to MembershipGiftModel that posts to the redeem endpoint, enabling gift redemption via the API. Update the gifts redeem controller to disable the redeem button while a redemption is in progress to prevent duplicate submissions. Implement actual redeem logic by calling the model's redeem action and redirecting to the catalog on success. Improve error handling by rethrowing caught errors and resetting the redeeming state accordingly.
1 parent 7355456 commit 2e281a1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

app/controllers/gifts/redeem.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class GiftsRedeemController extends Controller {
2424

2525
get redeemButtonIsDisabled() {
2626
// Button is disabled if user already has membership benefits
27-
return this.currentUserCanAccessMembershipBenefits;
27+
return this.currentUserCanAccessMembershipBenefits || this.isRedeemingGift;
2828
}
2929

3030
@action
@@ -42,13 +42,12 @@ export default class GiftsRedeemController extends Controller {
4242
this.isRedeemingGift = true;
4343

4444
try {
45-
// TODO: Implement actual gift redeeming logic
46-
// This would typically involve calling an API endpoint to redeem the gift
47-
// For now, we'll just redirect to the catalog page as a placeholder
45+
await this.model.redeem({});
4846
this.router.transitionTo('catalog');
49-
} catch {
47+
} catch (error) {
5048
// TODO: Handle error appropriately
5149
this.isRedeemingGift = false;
50+
throw error;
5251
}
5352
}
5453
}

app/models/membership-gift.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Model, { attr, belongsTo } from '@ember-data/model';
2+
import { memberAction } from 'ember-api-actions';
23
import type UserModel from 'codecrafters-frontend/models/user';
34

45
export default class MembershipGiftModel extends Model {
@@ -10,4 +11,11 @@ export default class MembershipGiftModel extends Model {
1011
@attr('string') declare giftMessage: string;
1112
@attr('number') declare validityInDays: number;
1213
@attr('string') declare secretToken: string;
14+
15+
declare redeem: (this: MembershipGiftModel, payload: unknown) => Promise<void>;
1316
}
17+
18+
MembershipGiftModel.prototype.redeem = memberAction({
19+
path: 'redeem',
20+
type: 'post',
21+
});

0 commit comments

Comments
 (0)