Skip to content

Commit

Permalink
Refactor to remove logic duplication in frontend (SharedDiff module)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbu88 committed Jan 4, 2022
1 parent bce0df6 commit 610a9da
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class MemoryDiffRepository implements SharedDiffRepository {
}

deleteById(id: string): Promise<number> {
delete this.db[id];
return Promise.resolve(1);
}

Expand Down
13 changes: 0 additions & 13 deletions frontend/src/app/SharedDiff.ts

This file was deleted.

67 changes: 64 additions & 3 deletions frontend/src/app/diff-detail/diff-detail.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute, convertToParamMap } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';
import { DiffyService } from '../diffy.service';

import {DiffDetailComponent} from './diff-detail.component';
import { DiffDetailComponent } from './diff-detail.component';

describe('DiffDetailComponent', () => {
let component: DiffDetailComponent;
let fixture: ComponentFixture<DiffDetailComponent>;
const DIFF_ID = "1";
const DIFF_CREATED_DATE = new Date('2022-01-01');
const DIFF_EXPIRES_AT = new Date('2022-01-02');

beforeEach(async(() => {
TestBed.configureTestingModule({declarations: [DiffDetailComponent]}).compileComponents();
TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientModule],
declarations: [DiffDetailComponent],
providers: [
{
provide: ActivatedRoute,
useValue: {
snapshot: { paramMap: { get: () => DIFF_ID } },
},
},
{
provide: DiffyService,
useValue: {
getDiff: () => of({
id: DIFF_ID,
rawDiff: "--",
created: DIFF_CREATED_DATE,
expiresAt: DIFF_EXPIRES_AT,
diff: [],
}),
extendLifetime: () => of({
id: DIFF_ID,
rawDiff: "--",
created: DIFF_CREATED_DATE,
expiresAt: new Date("2022-01-03"),
diff: [],
}),
makePermanent: () => of({
id: DIFF_ID,
rawDiff: "--",
created: DIFF_CREATED_DATE,
expiresAt: new Date("9999-01-01"),
diff: [],
}),
},
}
]

}).compileComponents();
}));

beforeEach(() => {
Expand All @@ -19,4 +65,19 @@ describe('DiffDetailComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('ngOnInit should fetch a diff by id', () => {
expect(component.currentId).toBe(DIFF_ID);
expect(component.sharedDiff.expiresAt).toEqual(DIFF_EXPIRES_AT);
});

it('getExtendLifetimeFn', () => {
component.getExtendLifetimeFn()();
expect(component.sharedDiff.expiresAt).toEqual(new Date("2022-01-03"));
});

it('getMakePermanentDiffFn', () => {
component.getMakePermanentDiffFn()("foo@example.com");
expect(component.sharedDiff.expiresAt).toEqual(new Date("9999-01-01"));
});
});
32 changes: 14 additions & 18 deletions frontend/src/app/diff-detail/diff-detail.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {DOCUMENT} from '@angular/common';
import {Component, Inject, Input, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';

import {AlertService} from '../alert.service';
import {FileTree} from '../diff-detail/tree-functions';
import {DiffyService} from '../diffy.service';
import { makeSharedDiff } from '../SharedDiff';
import { DOCUMENT } from '@angular/common';
import { Component, Inject, Input, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import { AlertService } from '../alert.service';
import { FileTree } from '../diff-detail/tree-functions';
import { DiffyService } from '../diffy.service';
import { SharedDiff } from 'diffy-models';
import {Error} from '../types/Error';
import { Error } from '../types/Error';

const DIFF_MAX_DATE = new Date('9999-01-01');
const MAKE_PERMANENT_THRESHOLD = 5 * 24 * 60 * 60 * 1000 - 1;
Expand Down Expand Up @@ -38,9 +37,8 @@ export class DiffDetailComponent implements OnInit {
this.currentId = id;
this.loading = true;
this.diffyService.getDiff(id).subscribe(
diffy => {
this.sharedDiff = makeSharedDiff(diffy.rawDiff, new Date(diffy.created));
this.sharedDiff.expiresAt = new Date(diffy.expiresAt);
sharedDiff => {
this.sharedDiff = sharedDiff
this.loading = false;
},
error => {
Expand Down Expand Up @@ -105,9 +103,8 @@ export class DiffDetailComponent implements OnInit {
return () => {
this.diffyService.extendLifetime(this.currentId)
.subscribe(
diffy => {
this.sharedDiff = makeSharedDiff(diffy.rawDiff, new Date(diffy.created));
this.sharedDiff.expiresAt = new Date(diffy.expiresAt);
sharedDiff => {
this.sharedDiff = sharedDiff;
},
(error: Error) => {
this.alertService.error(':-( Error while extending diff: ' + error.text, true);
Expand All @@ -119,9 +116,8 @@ export class DiffDetailComponent implements OnInit {
return () => {
this.diffyService.makePermanent(this.currentId)
.subscribe(
diffy => {
this.sharedDiff = makeSharedDiff(diffy.rawDiff, new Date(diffy.created));
this.sharedDiff.expiresAt = new Date(diffy.expiresAt);
sharedDiff => {
this.sharedDiff = sharedDiff;
},
(error: Error) => {
this.alertService.error(':-( Error while making diff permanent: ' + error.text, true);
Expand Down
42 changes: 31 additions & 11 deletions frontend/src/app/diffy.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable, throwError} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {catchError, map} from 'rxjs/operators';
import { SharedDiff } from 'diffy-models';
import {Error} from './types/Error';
import * as Diff2Html from 'diff2html';

// var diff2html = require('diff2html');

@Injectable({providedIn: 'root'})
export class DiffyService {
private diffyUrl = '/api/diff/'; // URL to web api

private storage: {[id: number]: string} = {};
private idCount = 1;

constructor(private http: HttpClient) {}

private handleError<T>(operation = 'operation', result?: T) {
Expand All @@ -33,8 +32,10 @@ export class DiffyService {
}
}

public getDiff(id: string): Observable<any> {
return this.http.get(this.diffyUrl + id).pipe(catchError(this.handleError('getDiff', null)));
public getDiff(id: string): Observable<SharedDiff> {
return this.http.get(this.diffyUrl + id)
.pipe(map(diffyObj => this.makeSharedDiffFromJson(diffyObj)))
.pipe(catchError(this.handleError('getDiff', null)));
}

storeDiff(diffText: string): Observable<any> {
Expand All @@ -51,23 +52,42 @@ export class DiffyService {
.pipe(catchError(this.handleError('deleteDiff', null)));
}

extendLifetime(id: string): Observable<any> {
extendLifetime(id: string): Observable<SharedDiff> {
const httpOptions = {headers: new HttpHeaders({'Content-Type': 'application/json'})};

return this.http.post(this.diffyUrl + 'extend/' + id, httpOptions)
.pipe(map(diffyObj => this.makeSharedDiffFromJson(diffyObj)))
.pipe(catchError(this.handleError('extendLifetimeDiff', null)));
}

makePermanent(id: string): Observable<any> {
makePermanent(id: string): Observable<SharedDiff> {
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'}),
};

return this.http.post(this.diffyUrl + 'makePermanent/' + id, httpOptions,)
.pipe(catchError(this.handleError('extendLifetimeDiff', null)));
return this.http.post(this.diffyUrl + 'makePermanent/' + id, httpOptions)
.pipe(map(diffyObj => this.makeSharedDiffFromJson(diffyObj)))
.pipe(catchError(this.handleError('extendLifetimeDiff', null)));
}

downloadDiff(id: string) {
window.open('/diff_download/' + id);
}
}

private makeSharedDiff(raw_diff: string, date: Date = new Date()): SharedDiff {
let expire_date = new Date();
expire_date.setDate(date.getDate() + 1);
return {
created: date,
expiresAt: expire_date,
diff: Diff2Html.parse(raw_diff),
rawDiff: raw_diff,
};
}

private makeSharedDiffFromJson(diffyObj): SharedDiff {
let sharedDiff = this.makeSharedDiff(diffyObj.rawDiff, new Date(diffyObj.created));
sharedDiff.expiresAt = new Date(diffyObj.expiresAt);
return sharedDiff
}
}

0 comments on commit 610a9da

Please sign in to comment.