Skip to content

Commit 19e41d3

Browse files
committed
dev: demo instance
1 parent f5b1099 commit 19e41d3

File tree

10 files changed

+287
-48
lines changed

10 files changed

+287
-48
lines changed

angular.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@
6868
"maximumError": "4kb"
6969
}
7070
]
71-
},
72-
"uk": {
73-
"localize": ["uk"]
7471
}
7572
}
7673
},
@@ -200,6 +197,22 @@
200197
"maximumError": "4kb"
201198
}
202199
]
200+
},
201+
"demo": {
202+
"fileReplacements": [
203+
{
204+
"replace": "projects/ngxe/src/environments/environment.ts",
205+
"with": "projects/ngxe/src/environments/environment.demo.ts"
206+
}
207+
],
208+
"optimization": true,
209+
"outputHashing": "all",
210+
"sourceMap": false,
211+
"namedChunks": false,
212+
"extractLicenses": true,
213+
"vendorChunk": false,
214+
"buildOptimizer": true,
215+
"baseHref": "/"
203216
}
204217
}
205218
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"ng": "ng",
66
"ngxe:start": "ng run ngxe:serve --proxy-config proxy.conf.json",
77
"ngxe:build": "ng run ngxe:build:production",
8+
"ngxe:build:demo": "ng run ngxe:build:demo",
89
"test": "ng run ngxe:test",
910
"demo:start": "ng run demo:serve --port=4201",
1011
"demo:start:uk": "ng run demo:serve:uk",

projects/demo/ngxe.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"output": {
55
"source": "projects/demo/i18n/messages.en.json",
66
"translations": [
7-
{
8-
"locale": "uk",
9-
"path": "projects/demo/i18n/messages.uk.json"
10-
},
117
{
128
"locale": "de",
139
"path": "projects/demo/i18n/messages.de.json"
10+
},
11+
{
12+
"locale": "uk",
13+
"path": "projects/demo/i18n/messages.uk.json"
1414
}
1515
]
1616
}

projects/ngxe/src/app/api.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { HttpClient } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { Observable, of } from 'rxjs';
4+
import { fromPromise } from 'rxjs/internal-compatibility';
5+
import { map } from 'rxjs/operators';
6+
import { Api_Error, Api_GetProject } from '../../../meta/api';
7+
import { environment } from '../environments/environment';
8+
9+
@Injectable({
10+
providedIn: 'root',
11+
})
12+
export class Api {
13+
constructor(
14+
private http: HttpClient,
15+
) {
16+
}
17+
18+
getProject(): Observable<Api_GetProject | Api_Error> {
19+
if (environment.demo) {
20+
return fromPromise(import('../demo.json'))
21+
.pipe(
22+
map(raw => raw as any),
23+
);
24+
} else {
25+
return this.http.get<Api_GetProject | Api_Error>('/api/project');
26+
}
27+
}
28+
29+
postProject(body: any): Observable<boolean> {
30+
if (environment.demo) {
31+
alert('It is a demo instance, so nothing really changed.');
32+
return of(true);
33+
} else {
34+
return this.http.post<boolean>('/api/project', body);
35+
}
36+
}
37+
}

projects/ngxe/src/app/project.spec.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ import { Project } from './project';
33

44
describe('Project', () => {
55
let service: Project;
6-
let http: HttpStub;
6+
let api: ApiStub;
77

88
beforeEach(() => {
9-
http = new HttpStub();
10-
service = new Project(http as any);
9+
api = new ApiStub();
10+
service = new Project(api as any);
1111
});
1212

1313
it('should be created', () => {
1414
expect(service).toBeTruthy();
1515
});
1616

1717
describe('load()', () => {
18-
it('should load data via http', () => {
19-
const spy = spyOn(http, 'get').and.callThrough();
18+
it('should load data via api', () => {
19+
const spy = spyOn(api, 'getProject').and.callThrough();
2020
service.load().subscribe(() => {
2121
});
22-
expect(spy).toHaveBeenCalledWith('/api/project');
22+
expect(spy).toHaveBeenCalled();
2323
});
2424

2525
it('should store data', () => {
2626
service.load().subscribe(() => {
2727
});
28-
expect(service.data).toEqual(http.project as any);
28+
expect(service.data).toEqual(api.project as any);
2929
});
3030

3131
it('should run setCurrent() with first locale', () => {
3232
const spy = spyOn(service, 'setCurrent');
3333
service.load().subscribe(() => {
3434
});
35-
expect(spy).toHaveBeenCalledWith(http.project.output.translations[0].locale);
35+
expect(spy).toHaveBeenCalledWith(api.project.output.translations[0].locale);
3636
});
3737
});
3838

@@ -86,13 +86,13 @@ describe('Project', () => {
8686
});
8787

8888
it('should trim messages', () => {
89-
http.project.input.translations = {
89+
api.project.input.translations = {
9090
'KEY': ' Source',
9191
};
92-
http.project.output.source.translations = {
92+
api.project.output.source.translations = {
9393
'KEY': 'Source ',
9494
};
95-
http.project.output.translations[0].translations = {
95+
api.project.output.translations[0].translations = {
9696
'KEY': 'Target',
9797
};
9898
service.load().subscribe(() => {
@@ -110,16 +110,16 @@ describe('Project', () => {
110110
});
111111

112112
it('should suggest', () => {
113-
http.project.input.translations = {
113+
api.project.input.translations = {
114114
'KEY_1': 'Input Source 1',
115115
'KEY_2': 'Input Source 2',
116116
};
117-
http.project.output.source.translations = {
117+
api.project.output.source.translations = {
118118
'KEY_1': 'Input Source 2',
119119
'KEY_2': 'Input Source 1',
120120
'KEY_3': 'Input Source 2',
121121
};
122-
http.project.output.translations[0].translations = {
122+
api.project.output.translations[0].translations = {
123123
'KEY_1': 'Target 1',
124124
'KEY_2': 'Target 2',
125125
'KEY_3': 'Target 3',
@@ -170,13 +170,12 @@ describe('Project', () => {
170170

171171
describe('save()', () => {
172172
it('should compile body and send via http', () => {
173-
const spy = spyOn(http, 'post').and.callThrough();
173+
const spy = spyOn(api, 'postProject').and.callThrough();
174174
service.load().subscribe(() => {
175175
});
176176
service.save().subscribe(() => {
177177
});
178178
expect(spy).toHaveBeenCalledWith(
179-
'/api/project',
180179
{
181180
input: {
182181
locale: 'INP_LOC',
@@ -193,34 +192,33 @@ describe('Project', () => {
193192
});
194193

195194
it('should sort messages by ID', () => {
196-
http.project.input.translations = {
195+
api.project.input.translations = {
197196
'KEY_3': 'Key3',
198197
'KEY_1': 'Key1',
199198
'A_KEY': 'AKey',
200199
'Z_KEY': 'ZKey',
201200
'KEY_2': 'Key2',
202201
};
203-
http.project.output.source.translations = {
202+
api.project.output.source.translations = {
204203
'KEY_3': 'Key3',
205204
'KEY_1': 'Key1',
206205
'A_KEY': 'AKey',
207206
'Z_KEY': 'ZKey',
208207
'KEY_2': 'Key2',
209208
};
210-
http.project.output.translations[0].translations = {
209+
api.project.output.translations[0].translations = {
211210
'KEY_3': 'Key3',
212211
'KEY_1': 'Key1',
213212
'A_KEY': 'AKey',
214213
'Z_KEY': 'ZKey',
215214
'KEY_2': 'Key2',
216215
};
217-
const spy = spyOn(http, 'post').and.callThrough();
216+
const spy = spyOn(api, 'postProject').and.callThrough();
218217
service.load().subscribe(() => {
219218
});
220219
service.save().subscribe(() => {
221220
});
222221
expect(spy).toHaveBeenCalledWith(
223-
'/api/project',
224222
{
225223
input: {
226224
locale: 'INP_LOC',
@@ -237,23 +235,22 @@ describe('Project', () => {
237235
});
238236

239237
it('should remove non existed messages in source from target', () => {
240-
http.project.input.translations = {
238+
api.project.input.translations = {
241239
'KEY': 'Source',
242240
};
243-
http.project.output.source.translations = {
241+
api.project.output.source.translations = {
244242
'KEY': 'Source',
245243
};
246-
http.project.output.translations[0].translations = {
244+
api.project.output.translations[0].translations = {
247245
'KEY': 'Target',
248246
'UNEX_KEY': 'Unex',
249247
};
250-
const spy = spyOn(http, 'post').and.callThrough();
248+
const spy = spyOn(api, 'postProject').and.callThrough();
251249
service.load().subscribe(() => {
252250
});
253251
service.save().subscribe(() => {
254252
});
255253
expect(spy).toHaveBeenCalledWith(
256-
'/api/project',
257254
{
258255
input: {
259256
locale: 'INP_LOC',
@@ -271,7 +268,7 @@ describe('Project', () => {
271268
});
272269
});
273270

274-
class HttpStub {
271+
class ApiStub {
275272
project: any = {
276273
success: true,
277274
config: 'CONFIG',
@@ -304,11 +301,11 @@ class HttpStub {
304301
},
305302
};
306303

307-
get(url: string) {
304+
getProject() {
308305
return of(this.project);
309306
}
310307

311-
post(url: string, body: any) {
308+
postProject(body: any) {
312309
return of(true);
313310
}
314311
}

projects/ngxe/src/app/project.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { HttpClient } from '@angular/common/http';
21
import { Injectable } from '@angular/core';
32
import { tap } from 'rxjs/operators';
4-
import { Api_Error, Api_GetProject } from '../../../meta/api';
3+
import { Api_GetProject } from '../../../meta/api';
54
import { JsonFile, JsonFileTranslations } from '../../../meta/formats';
5+
import { Api } from './api';
66
import { TableRow, TableRowType, TableStats } from './meta';
77

88
const typesWeight: { [key in TableRowType]: number } = {
@@ -25,13 +25,13 @@ export class Project {
2525
stats: TableStats = {};
2626

2727
constructor(
28-
private http: HttpClient,
28+
private api: Api,
2929
) {
3030
}
3131

3232
load() {
33-
return this.http
34-
.get<Api_GetProject | Api_Error>('/api/project')
33+
return this.api
34+
.getProject()
3535
.pipe(
3636
tap(res => {
3737
if (!res.success) {
@@ -104,7 +104,7 @@ export class Project {
104104
})),
105105
},
106106
};
107-
return this.http.post('/api/project', body);
107+
return this.api.postProject(body);
108108
}
109109

110110
private compileTable({inputSource, outputSource, translation}: {

0 commit comments

Comments
 (0)