Skip to content

Commit 3a12066

Browse files
init product fn
1 parent 6f24003 commit 3a12066

File tree

16 files changed

+5689
-130
lines changed

16 files changed

+5689
-130
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ testem.log
3838
# System Files
3939
.DS_Store
4040
Thumbs.db
41+
42+
# Local Netlify folder
43+
.netlify

apps/products/functions/api/api.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Handler } from '@netlify/functions';
2+
import fastify, { FastifyInstance, FastifyRequest } from 'fastify';
3+
import awsLambdaFastify from '@fastify/aws-lambda';
4+
import sensible from '@fastify/sensible';
5+
import { products } from '@nx-example/shared/product/data';
6+
import cors from '@fastify/cors';
7+
8+
async function routes(fastify: FastifyInstance) {
9+
fastify.get('/products', async () => {
10+
return products;
11+
});
12+
}
13+
14+
function init() {
15+
const app = fastify();
16+
app.register(sensible);
17+
app.register(cors);
18+
// set the prefix for the netlify functions url
19+
app.register(routes, { prefix: '/.netlify/functions/api' });
20+
return app;
21+
}
22+
23+
// Note: Netlify deploys this function at the endpoint /.netlify/functions/api
24+
export const handler: Handler = awsLambdaFastify(init());

apps/products/netlify.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[build]
2+
# Static files from this folder will be served at the root of the site.
3+
publish = "public"
4+
[functions]
5+
# Directory with serverless functions, including background
6+
# functions, to deploy. This is relative to the base directory
7+
# if one has been set, or the root directory if
8+
# a base hasn’t been set.
9+
directory = "functions/"
10+
11+
# Specifies \`esbuild\` for functions bundling, esbuild is the default for TS
12+
# node_bundler = "esbuild"
13+
14+
[functions."hello*"]
15+
# Apply settings to any functions with a name beginning with "hello"

apps/products/project.json

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"build": {
1414
"executor": "@angular-devkit/build-angular:browser",
1515
"options": {
16-
"aot": true,
16+
"aot": false,
17+
"buildOptimizer": false,
18+
"optimization": false,
19+
"sourceMap": true,
1720
"outputPath": "dist/apps/products",
1821
"index": "apps/products/src/index.html",
1922
"main": "apps/products/src/main.ts",
@@ -92,7 +95,8 @@
9295
"options": {
9396
"lintFilePatterns": [
9497
"apps/products/src/**/*.ts",
95-
"apps/products/src/**/*.html"
98+
"apps/products/src/**/*.html",
99+
"./functions/**/*.ts"
96100
]
97101
},
98102
"outputs": ["{options.outputFile}"]
@@ -114,6 +118,22 @@
114118
}
115119
]
116120
}
121+
},
122+
"serve-functions": {
123+
"command": "npx netlify dev"
124+
},
125+
"deploy-functions": {
126+
"dependsOn": ["lint"],
127+
"command": "npx netlify deploy",
128+
"options": {
129+
"cwd": "apps/products"
130+
},
131+
"configurations": {
132+
"production": {
133+
"command": "npx netlify deploy --prod",
134+
"cwd": "apps/products"
135+
}
136+
}
117137
}
118138
},
119139
"tags": ["type:app", "scope:products"],

apps/products/public/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Netlify Functions</h1>
2+
<p>
3+
The sample function is available at
4+
<a href="/.netlify/functions/hello"><code>/.netlify/functions/hello</code></a
5+
>.
6+
</p>

apps/products/src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { RouterModule } from '@angular/router';
44

55
import { StoreModule } from '@ngrx/store';
66
import { AppComponent } from './app.component';
7+
import { EffectsModule } from '@ngrx/effects';
78

89
@NgModule({
910
declarations: [AppComponent],
@@ -30,6 +31,7 @@ import { AppComponent } from './app.component';
3031
{ initialNavigation: 'enabledBlocking' }
3132
),
3233
StoreModule.forRoot({}),
34+
EffectsModule.forRoot(),
3335
],
3436
providers: [],
3537
bootstrap: [AppComponent],

libs/products/home-page/src/lib/home-page/home-page.component.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22

33
import { select, Store } from '@ngrx/store';
44
import { Observable } from 'rxjs';
55

66
import {
77
getProducts,
88
getProductsState,
9+
LoadProducts,
910
ProductsPartialState,
1011
} from '@nx-example/shared/product/state';
1112
import { Product } from '@nx-example/shared/product/types';
@@ -16,11 +17,16 @@ import '@nx-example/shared/product/ui';
1617
templateUrl: './home-page.component.html',
1718
styleUrls: ['./home-page.component.scss'],
1819
})
19-
export class HomePageComponent {
20+
export class HomePageComponent implements OnInit {
2021
products: Observable<Product[]> = this.store.pipe(
2122
select(getProductsState),
2223
select(getProducts)
2324
);
2425

2526
constructor(private store: Store<ProductsPartialState>) {}
27+
28+
ngOnInit() {
29+
console.log('cmp init');
30+
this.store.dispatch(new LoadProducts());
31+
}
2632
}

libs/products/product-detail-page/src/lib/product-detail-page/product-detail-page.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ <h1>{{ product.name }}</h1>
1111
[value]="product.price"
1212
></nx-example-product-price>
1313
</p>
14+
<p>
15+
<!-- TODO(caleb) make this open the /cart route in the other page -->
16+
<button>Buy</button>
17+
</p>
1418
</div>
1519
</div>
1620
</ng-container>

libs/shared/product/state/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { PRODUCTS_FEATURE_KEY } from './lib/+state/products.reducer';
33

44
export * from './lib/+state/products.reducer';
55
export * from './lib/+state/products.selectors';
6+
export * from './lib/+state/products.actions';
67
export const getProductsState = createFeatureSelector(PRODUCTS_FEATURE_KEY);
78
export * from './lib/shared-product-state.module';
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { Action } from '@ngrx/store';
2+
import { Product } from '@nx-example/shared/product/types';
23

3-
export enum ProductsActionTypes {}
4+
export enum ProductsActionTypes {
5+
LoadProducts = '[Products] Load Products',
6+
LoadProductsSuccess = '[Products] Load Products Success',
7+
}
48

5-
export type ProductsAction = Action;
9+
export class LoadProducts implements Action {
10+
readonly type = ProductsActionTypes.LoadProducts;
11+
}
12+
export class LoadProductsSuccess implements Action {
13+
readonly type = ProductsActionTypes.LoadProductsSuccess;
14+
constructor(public products: Product[]) {}
15+
}
16+
17+
export type ProductsAction = LoadProducts | LoadProductsSuccess;

0 commit comments

Comments
 (0)