Skip to content

Commit

Permalink
remove @action decorator and type route templates
Browse files Browse the repository at this point in the history
  • Loading branch information
AmauryD committed Dec 12, 2024
1 parent 67930e0 commit 7f99dca
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 55 deletions.
7 changes: 2 additions & 5 deletions app/components/forms/register.gts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { RegisterChangeset } from 'ember-boilerplate/changesets/register';
import TpkForm from '@triptyk/ember-input-validation/components/tpk-form';
import type validationsRegister from 'ember-boilerplate/validations/register';
import Component from '@glimmer/component';
import { action } from '@ember/object';

export interface FormsRegisterSignature {
Args: {
Expand All @@ -19,13 +18,11 @@ export interface FormsRegisterSignature {
}

export default class FormsRegister extends Component<FormsRegisterSignature> {
@action
setBirthdate(date: unknown) {
setBirthdate = (date: unknown) => {
this.args.changeset.set('birthDate', date as Date);
}

@action
selectCategory(category: unknown) {
selectCategory = (category: unknown) => {
this.args.changeset.set('category', category as string);
}

Expand Down
2 changes: 1 addition & 1 deletion app/routes/404.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Route from '@ember/routing/route';

export default class NotFound extends Route {}
export default class NotFoundRoute extends Route {}
2 changes: 1 addition & 1 deletion app/routes/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { IntlService } from 'ember-intl';
import type SessionService from 'ember-simple-auth/services/session';
import { setupWorker } from 'msw/browser';

export default class Application extends Route {
export default class ApplicationRoute extends Route {
@service declare session: SessionService;
@service declare currentUser: CurrentUserService;
@service declare intl: IntlService;
Expand Down
2 changes: 1 addition & 1 deletion app/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { service } from '@ember/service';

import type SessionService from 'ember-simple-auth/services/session';

export default class Login extends Route {
export default class LoginRoute extends Route {
@service declare session: SessionService;

beforeModel() {
Expand Down
4 changes: 0 additions & 4 deletions app/routes/register.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import Route from '@ember/routing/route';

export interface RegisterRouteParams {}

export type RegisterRouteModel = Resolved<ReturnType<RegisterRoute['model']>>;

export default class RegisterRoute extends Route {
model() {
return {};
Expand Down
9 changes: 4 additions & 5 deletions app/templates/404.gts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import Component from '@glimmer/component';
import { on } from '@ember/modifier';
import { action } from '@ember/object';

import t from 'ember-intl/helpers/t';
import RouteTemplate from 'ember-route-template';
import type { RouteTemplateSignature } from 'ember-boilerplate/utils/route-template';
import type NotFoundRoute from 'ember-boilerplate/routes/404';

export interface NotFoundRouteComponentSignature {}

class NotFoundRouteComponent extends Component<NotFoundRouteComponentSignature> {
@action
comeback() {
class NotFoundRouteComponent extends Component<RouteTemplateSignature<NotFoundRoute>> {
comeback = () => {
window.history.back();
}

Expand Down
4 changes: 3 additions & 1 deletion app/templates/application.gts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import HeadLayout from 'ember-cli-head/components/head-layout';
import RouteTemplate from 'ember-route-template';

import type FlashMessageService from 'ember-cli-flash/services/flash-messages';
import type { RouteTemplateSignature } from 'ember-boilerplate/utils/route-template';
import type ApplicationRoute from 'ember-boilerplate/routes/application';

class ApplicationRouteComponent extends Component {
class ApplicationRouteComponent extends Component<RouteTemplateSignature<ApplicationRoute>> {
@service declare flashMessages: FlashMessageService;

<template>
Expand Down
13 changes: 6 additions & 7 deletions app/templates/forgot-password.gts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';

import { service } from '@ember/service';

import { ForgotPasswordChangeset } from 'ember-boilerplate/changesets/forgot-password';
Expand All @@ -12,26 +12,25 @@ import { tracked } from 'tracked-built-ins';
import type Router from '@ember/routing/router';
import type FlashMessageService from 'ember-cli-flash/services/flash-messages';
import RequestManager from '@ember-data/request';
import type ForgotPasswordRoute from 'ember-boilerplate/routes/forgot-password';
import type { RouteTemplateSignature } from 'ember-boilerplate/utils/route-template';

interface PagesForgotPasswordArgs {}

class PagesForgotPassword extends Component<PagesForgotPasswordArgs> {
class PagesForgotPassword extends Component<RouteTemplateSignature<ForgotPasswordRoute>> {
@service declare router: Router;
@service declare requestManager: RequestManager;
@service declare flashMessages: FlashMessageService;
@tracked changeset: ForgotPasswordChangeset;

validationSchema = forgotPasswordSchema;

constructor(owner: unknown, args: PagesForgotPasswordArgs) {
constructor(owner: unknown, args: RouteTemplateSignature<ForgotPasswordRoute>['Args']) {
super(owner, args);
this.changeset = new ForgotPasswordChangeset({
email: '',
});
}

@action
async sendRecoveryRequest(changeset: ForgotPasswordChangeset) {
sendRecoveryRequest = async (changeset: ForgotPasswordChangeset) => {
try {
await this.requestManager.request({
url: '/auth/forgot-password',
Expand Down
16 changes: 7 additions & 9 deletions app/templates/login.gts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

import { service } from '@ember/service';
import { waitFor } from '@ember/test-waiters';

Expand All @@ -15,10 +15,10 @@ import type RouterService from '@ember/routing/router-service';
import type CurrentUserService from 'ember-boilerplate/services/current-user';
import type FlashMessageService from 'ember-cli-flash/services/flash-messages';
import type SessionService from 'ember-simple-auth/services/session';
import type { RouteTemplateSignature } from 'ember-boilerplate/utils/route-template';
import type LoginRoute from 'ember-boilerplate/routes/login';

interface PagesLoginArgs {}

class PagesLogin extends Component<PagesLoginArgs> {
class PagesLogin extends Component<RouteTemplateSignature<LoginRoute>> {
@service declare flashMessages: FlashMessageService;
@service declare currentUser: CurrentUserService;
@service declare router: RouterService;
Expand All @@ -28,17 +28,15 @@ class PagesLogin extends Component<PagesLoginArgs> {

@tracked changeset: LoginChangeset;

public constructor(owner: unknown, args: PagesLoginArgs) {
public constructor(owner: unknown, args: RouteTemplateSignature<LoginRoute>['Args']) {
super(owner, args);
this.changeset = new LoginChangeset({
email: '',
password: '',
});
}

@action
@waitFor
async login(changeset: LoginChangeset) {
login = waitFor(async (changeset: LoginChangeset) => {
try {
await this.session.authenticate('authenticator:jwt', {
email: changeset.get('email'),
Expand All @@ -49,7 +47,7 @@ class PagesLogin extends Component<PagesLoginArgs> {
} catch (e) {
this.flashMessages.danger('Username or password incorrect');
}
}
});

<template>
<LoginLayout @title={{t "components.templates.login.title"}}>
Expand Down
14 changes: 5 additions & 9 deletions app/templates/register.gts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { service } from '@ember/service';

import { RegisterChangeset } from 'ember-boilerplate/changesets/register';
Expand All @@ -14,20 +13,18 @@ import type RegisterChangesetService from 'ember-boilerplate/services/changesets
import type ErrorHandlerService from 'ember-boilerplate/services/error-handler';
import type FlashMessageService from 'ember-cli-flash/services/flash-messages';
import { array } from '@ember/helper';
import type { RouteTemplateSignature } from 'ember-boilerplate/utils/route-template';
import type RegisterRoute from 'ember-boilerplate/routes/register';

export interface RegisterRouteComponentSignature {
Args: {};
}

class RegisterRouteComponent extends Component<RegisterRouteComponentSignature> {
class RegisterRouteComponent extends Component<RouteTemplateSignature<RegisterRoute>> {
@service declare flashMessages: FlashMessageService;
@service('changesets/register') declare register: RegisterChangesetService;
@service declare errorHandler: ErrorHandlerService;
@tracked declare changeset: RegisterChangeset;

validationSchema = formsRegisterSchema;

constructor(owner: unknown, args: RegisterRouteComponentSignature['Args']) {
constructor(owner: unknown, args: RouteTemplateSignature<RegisterRoute>['Args']) {
super(owner, args);
this.changeset = new RegisterChangeset({
email: '',
Expand All @@ -47,8 +44,7 @@ class RegisterRouteComponent extends Component<RegisterRouteComponentSignature>
});
}

@action
async saveRegister(changeset: RegisterChangeset) {
saveRegister = async (changeset: RegisterChangeset) => {
const user = await this.register.save(changeset);

if (user.isErr) {
Expand Down
19 changes: 7 additions & 12 deletions app/templates/reset-password.gts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';

import { service } from '@ember/service';

import { ResetPasswordChangeset } from 'ember-boilerplate/changesets/reset-password';
Expand All @@ -13,34 +13,29 @@ import { tracked } from 'tracked-built-ins';
import type RouterService from '@ember/routing/router-service';
import type FlashMessageService from 'ember-cli-flash/services/flash-messages';
import type RequestManager from '@ember-data/request';
import type ResetPasswordRoute from 'ember-boilerplate/routes/reset-password';
import type { RouteTemplateSignature } from 'ember-boilerplate/utils/route-template';

interface ResetPasswordRouteComponentSignature {
Args: {
token: string;
};
}

class ResetPasswordRouteComponent extends Component<ResetPasswordRouteComponentSignature> {
class ResetPasswordRouteComponent extends Component<RouteTemplateSignature<ResetPasswordRoute>> {
@service declare requestManager: RequestManager;
@service declare router: RouterService;
@service declare flashMessages: FlashMessageService;
@tracked changeset: ResetPasswordChangeset;

validationSchema = passwordRecoveryValidation;

constructor(owner: unknown, args: ResetPasswordRouteComponentSignature['Args']) {
constructor(owner: unknown, args: RouteTemplateSignature<ResetPasswordRoute>['Args']) {
super(owner, args);
this.changeset = new ResetPasswordChangeset({
password: '',
confirmPassword: '',
});
}

@action
async recoverPassword(changeset: ResetPasswordChangeset) {
recoverPassword = async (changeset: ResetPasswordChangeset) => {
try {
await this.requestManager.request({
url: `auth/set-password/${this.args.token}`,
url: `auth/set-password/${this.args.model.token}`,
body: JSON.stringify({
password: changeset.get('password'),
}),
Expand Down
20 changes: 20 additions & 0 deletions app/utils/route-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Route from '@ember/routing/route';
import type Controller from '@ember/controller';

export type ModelFrom<R extends Route> = Awaited<ReturnType<R['model']>>;

export type RouteTemplateSignature<
R extends Route,
C extends Controller | undefined = undefined,
> = C extends Controller
? {
Args: {
model: ModelFrom<R>;
controller: C;
};
}
: {
Args: {
model: ModelFrom<R>;
};
};

0 comments on commit 7f99dca

Please sign in to comment.