Skip to content

Commit

Permalink
Refactoring templates
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-urbanski committed Jun 24, 2024
1 parent 0658abc commit 8a7abd3
Show file tree
Hide file tree
Showing 34 changed files with 329 additions and 213 deletions.
72 changes: 43 additions & 29 deletions src/generators/AngularGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,32 @@ export default class extends BaseGenerator {
constructor(params) {
super(params);

this.registerTemplates("common/", [
// utils
"utils/config.ts",
"utils/date.ts",
]);

this.registerTemplates("angular/", [
// COMMON COMPONENTS
"app/components/common/delete/delete.component.html",
"app/components/common/delete/delete.component.ts",
"app/components/common/header/header.component.css",
"app/components/common/header/header.component.html",
"app/components/common/header/header.component.ts",
"app/components/common/layout/layout.component.html",
"app/components/common/layout/layout.component.ts",
"app/components/common/sidebar/sidebar.component.css",
"app/components/common/sidebar/sidebar.component.html",
"app/components/common/sidebar/sidebar.component.ts",
"app/components/common/table/table.component.html",
"app/components/common/table/table.component.ts",
"app/components/common/svg/list-svg/list-svg.component.svg",
"app/components/common/svg/list-svg/list-svg.component.ts",
"app/components/common/svg/show-svg/show-svg.component.svg",
"app/components/common/svg/show-svg/show-svg.component.ts",
"app/components/common/svg/edit-svg/edit-svg.component.svg",
"app/components/common/svg/edit-svg/edit-svg.component.ts",
"app/components/common/svg/menu/menu.component.svg",
"app/components/common/svg/menu/menu.component.ts",

// COMPONENTS
"app/components/foo/create/create.component.html",
Expand All @@ -32,24 +46,17 @@ export default class extends BaseGenerator {
"app/components/foo/list/list.component.ts",
"app/components/foo/show/show.component.html",
"app/components/foo/show/show.component.ts",
"app/components/foo/table/table.component.html",
"app/components/foo/table/table.component.ts",
"app/app.component.html",
"app/app.component.ts",

//SVG COMPONENT
"app/components/svg/list-svg/list-svg.component.svg",
"app/components/svg/list-svg/list-svg.component.ts",
"app/components/svg/show-svg/show-svg.component.svg",
"app/components/svg/show-svg/show-svg.component.ts",
"app/components/svg/edit-svg/edit-svg.component.svg",
"app/components/svg/edit-svg/edit-svg.component.ts",
"app/components/svg/menu/menu.component.svg",
"app/components/svg/menu/menu.component.ts",

//INTERFACE
"app/interface/api.ts",

// ROUTER
"app/router/foo.ts",
"app/router/index.ts",
"app/app.routes.ts",

//SERVICE
Expand Down Expand Up @@ -109,53 +116,53 @@ export default class extends BaseGenerator {

//CREATE DIRECTORIES - These directories may already exist
[
`${dir}/assets`,
`${dir}/utils`,
`${dir}/app/components/${lc}/create`,
`${dir}/app/components/${lc}/edit`,
`${dir}/app/components/${lc}/form`,
`${dir}/app/components/${lc}/list`,
`${dir}/app/components/${lc}/show`,
`${dir}/app/components/${lc}/table`,
`${dir}/app/components/common/delete`,
`${dir}/app/components/common/header`,
`${dir}/app/components/common/sidebar`,
`${dir}/app/components/common/table`,
`${dir}/app/components/svg/list-svg`,
`${dir}/app/components/svg/show-svg`,
`${dir}/app/components/svg/edit-svg`,
`${dir}/app/components/svg/menu`,
`${dir}/app/components/common/svg/list-svg`,
`${dir}/app/components/common/svg/show-svg`,
`${dir}/app/components/common/svg/edit-svg`,
`${dir}/app/components/common/svg/menu`,
`${dir}/app/interface`,
`${dir}/app/router`,
`${dir}/app/service`,
`${dir}/app/utils`,
].forEach((dir) => this.createDir(dir, false));

//CREATE FILE
[
"app/components/svg/list-svg/list-svg.component.svg",
"app/components/svg/list-svg/list-svg.component.ts",
"app/components/svg/show-svg/show-svg.component.svg",
"app/components/svg/show-svg/show-svg.component.ts",
"app/components/svg/edit-svg/edit-svg.component.svg",
"app/components/svg/edit-svg/edit-svg.component.ts",
"app/components/svg/menu/menu.component.svg",
"app/components/svg/menu/menu.component.ts",
"app/components/common/svg/list-svg/list-svg.component.svg",
"app/components/common/svg/list-svg/list-svg.component.ts",
"app/components/common/svg/show-svg/show-svg.component.svg",
"app/components/common/svg/show-svg/show-svg.component.ts",
"app/components/common/svg/edit-svg/edit-svg.component.svg",
"app/components/common/svg/edit-svg/edit-svg.component.ts",
"app/components/common/svg/menu/menu.component.svg",
"app/components/common/svg/menu/menu.component.ts",
"app/components/common/delete/delete.component.html",
"app/components/common/delete/delete.component.ts",
"app/components/common/header/header.component.css",
"app/components/common/header/header.component.html",
"app/components/common/header/header.component.ts",
"app/components/common/sidebar/sidebar.component.css",
"app/components/common/sidebar/sidebar.component.html",
"app/components/common/sidebar/sidebar.component.ts",
"app/components/common/table/table.component.html",
"app/components/common/table/table.component.ts",
"app/interface/api.ts",
"app/service/api.service.ts",
"app/app.component.html",
"app/app.component.ts",
"app/app.routes.ts",
].forEach((file) =>
this.createFile(file, `${dir}/${file}`, context, false)
);

// DYNAMIC FILE
[
"app/router/%s.ts",
"app/components/%s/list/list.component.html",
Expand All @@ -169,9 +176,16 @@ export default class extends BaseGenerator {
"app/components/%s/show/show.component.html",
"app/components/%s/show/show.component.ts",
"app/components/%s/show/show.component.html",
"app/components/%s/table/table.component.html",
"app/components/%s/table/table.component.ts",
].forEach((file) =>
this.createFileFromPattern(file, dir, [lc, formFields], context)
);

// CONFIG
this.createConfigFile(`${dir}/app/utils/config.ts`, {
entrypoint: api.entrypoint,
});
}

parseFields(resource) {
Expand Down
4 changes: 2 additions & 2 deletions templates/angular/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component} from '@angular/core';
import {RouterOutlet} from '@angular/router';
import {AsyncPipe} from "@angular/common";
import {HttpClientModule} from "@angular/common/http";
import {Component} from '@angular/core';
import {RouterOutlet} from '@angular/router';
import {LayoutComponent} from "@components/common/layout/layout.component";

@Component({
Expand Down
32 changes: 6 additions & 26 deletions templates/angular/app/app.routes.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import {Routes} from '@angular/router';
import {ListComponent} from "@components/foo/list/list.component";
import {ShowComponent} from "@components/foo/show/show.component";
import {EditComponent} from "@components/foo/edit/edit.component";
import {CreateComponent} from "@components/foo/create/create.component";
import {LayoutComponent} from "@components/common/layout/layout.component";
import { Routes } from "@angular/router";
import { LayoutComponent } from "@components/common/layout/layout.component";
import { allRoutes } from "@router";

export const routes: Routes = [
{
path: '',
path: "",
component: LayoutComponent,
children: [
{
path: '{{lc}}',
component: ListComponent
},
{
path: '{{lc}}/add',
component: CreateComponent
},
{
path: '{{lc}}/:id',
component: ShowComponent,
},
{
path: '{{lc}}/:id/edit',
component: EditComponent
},
]
}
children: allRoutes,
},
];
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {Component, EventEmitter, Input, Output} from '@angular/core';
import {
Component,
EventEmitter,
Input,
Output
} from '@angular/core';

@Component({
selector: 'app-delete',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import {MenuComponent} from "@components/svg/menu/menu.component";
import {MenuComponent} from "@components/common/svg/menu/menu.component";

@Component({
selector: 'app-header',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<app-header></app-header>
<app-header />
<div class="flex">
<div class="w-1/6 h-100">
<app-sidebar></app-sidebar>
<app-sidebar />
</div>
<div class="w-5/6 ">
<router-outlet></router-outlet>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div class="flex justify-center mt-8">
<nav aria-label="Page navigation">
<ul class="flex list-style-none">
<li>
<a [routerLink]="['/books']"
[routerLinkActive]="'bg-gray-200 text-gray-500 pointer-events-none'"
(click)="changeUri(pagination()['hydra:first'])"
class="bg-gray block py-2 px-3 rounded"
aria-label="First page">
<span aria-hidden="true">&lArr;</span>First
</a>
</li>
@if (pagination()['hydra:previous']) {
<li>
<a [routerLink]="['/books']"
[queryParams]="pageParamValue('hydra:previous')"
[routerLinkActive]="'bg-gray-200 text-gray-500 pointer-events-none'"
(click)="changeUri(pagination()['hydra:previous'])"
class="bg-gray block py-2 px-3 rounded"
aria-label="Previous page">
Previous
</a>
</li>
}
@if (pagination()['hydra:next']) {
<li>
<a [routerLink]="['/books']"
[queryParams]="pageParamValue('hydra:next')"
[routerLinkActive]="'bg-gray-200 text-gray-500 pointer-events-none'"
(click)="changeUri(pagination()['hydra:next'])"
class="bg-gray block py-2 px-3 rounded"
aria-label="Next page">
Next
</a>
</li>
}
<li>
<a [routerLink]="['/books']"
[queryParams]="pageParamValue('hydra:last')"
[routerLinkActive]="'bg-gray-200 text-gray-500 pointer-events-none'"
(click)="changeUri(pagination()['hydra:last'])"
class="bg-gray block py-2 px-3 rounded"
aria-label="Last page">
<span aria-hidden="true">&rAarr;</span> Last
</a>
</li>
</ul>
</nav>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Component, EventEmitter, Input, Output, WritableSignal} from '@angular/core';
import {RouterLink, RouterLinkActive} from "@angular/router";
import { Pagination} from "@interface/api";

@Component({
selector: 'app-pagination',
standalone: true,
imports: [
RouterLink,
RouterLinkActive
],
templateUrl: './pagination.component.html',
styleUrl: './pagination.component.css'
})
export class PaginationComponent {
@Input() pagination!:WritableSignal<Pagination>;
@Output() handleChangePage = new EventEmitter()

pageParamValue(page: keyof Pagination) {
const pageParams = this.pagination()[page].split('?page=')
return {
page: pageParams[1]
}
}

changeUri (uri: string) {
this.handleChangePage.emit(uri)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import {RouterLink, RouterLinkActive} from "@angular/router";
import {ListSvgComponent} from "@components/svg/list-svg/list-svg.component";
import {ListSvgComponent} from "@components/common/svg/list-svg/list-svg.component";

@Component({
selector: 'app-sidebar',
Expand All @@ -14,5 +14,4 @@ import {ListSvgComponent} from "@components/svg/list-svg/list-svg.component";
styleUrl: './sidebar.component.css'
})
export class SidebarComponent {

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<div class="bg-blue-100 rounded py-4 px-4 text-blue-700 text-sm">Loading ...</div>
} @else {
<h1 class="text-3xl my-4">Create</h1>
<app-form [fields]="formType" (submit)="onSubmit($event)"/>
<app-form-{{lc}} [item]="item()" (submit)="onSubmit($event)"/>
}
</div>
15 changes: 4 additions & 11 deletions templates/angular/app/components/foo/create/create.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {RouterLink} from "@angular/router";
import {DeleteComponent} from "@components/common/delete/delete.component";
import {FormComponent} from "@components/{{lc}}/form/form.component";
import {ApiItem} from "@interface/api";
import {ApiService} from "@service/api.service";

@Component({
selector: 'app-create',
selector: 'app-create-{{lc}}',
standalone: true,
imports: [
DeleteComponent,
Expand All @@ -21,21 +22,13 @@ import {ApiService} from "@service/api.service";
export class CreateComponent {
private apiService: ApiService = inject(ApiService)
private location: Location = inject(Location)
public item: WritableSignal<ApiItem> = signal({} as ApiItem)
public isLoading: WritableSignal<boolean> = signal(false)

public formType: Array<{ name: string; type: string }> = [
{
name: 'name',
type: 'string',
}
]

onSubmit(data: any) {
return this.apiService
.add('/{{lc}}',
{
...data
}
this.item()
).subscribe(
(item) => {
this.isLoading.set(true)
Expand Down
Loading

0 comments on commit 8a7abd3

Please sign in to comment.