Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat(store): last-changed date and author
Browse files Browse the repository at this point in the history
  • Loading branch information
TheReincarnator authored and Jumace committed Apr 10, 2018
1 parent a3af629 commit 947f271
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 35 deletions.
56 changes: 26 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -103,6 +103,7 @@
"@types/react": "16.3.5",
"@types/react-dom": "16.0.4",
"@types/smoothscroll-polyfill": "0.3.0",
"@types/username": "3.0.0",
"@types/uuid": "3.4.3",
"concurrently": "3.5.1",
"electron": "1.8.4",
Expand Down Expand Up @@ -139,6 +140,7 @@
"styled-components": "3.2.5",
"tslib": "1.9.0",
"typescript": "2.8.1",
"username": "3.0.0",
"uuid": "3.2.1"
},
"homepage": "https://meetalva.github.io/"
Expand Down
4 changes: 3 additions & 1 deletion src/store/command/element-command.ts
Expand Up @@ -82,7 +82,9 @@ export abstract class ElementCommand extends Command {
return false;
}

if (this.element.getPage()) {
const page = this.element.getPage();
if (page) {
page.getProject().touch();
Store.getInstance().setSelectedElement(this.element);
}

Expand Down
51 changes: 47 additions & 4 deletions src/store/project.ts
Expand Up @@ -2,10 +2,13 @@ import { JsonArray, JsonObject } from './json';
import * as MobX from 'mobx';
import { PageRef } from './page/page-ref';
import { Store } from './store';
import * as username from 'username';
import * as Uuid from 'uuid';

export interface ProjectProperties {
id?: string;
lastChangedAuthor?: string;
lastChangedDate?: Date;
name: string;
previewFrame: string;
}
Expand All @@ -24,6 +27,20 @@ export class Project {
*/
@MobX.observable private id: string;

/**
* The last author who edited this project or one of its pages (including elements,
* properties etc.). Updated when calling the touch method.
* @see touch()
*/
@MobX.observable private lastChangedAuthor: string;

/**
* The last change date when this project or one of its pages was edited (including elements,
* properties etc.). Updated when calling the touch method.
* @see touch()
*/
@MobX.observable private lastChangedDate: Date = new Date();

/**
* The human-friendly name of the project.
* In the frontend, to be displayed instead of the ID.
Expand Down Expand Up @@ -51,6 +68,8 @@ export class Project {
public constructor(properties: ProjectProperties) {
this.id = properties.id ? properties.id : Uuid.v4();
this.name = properties.name;
this.lastChangedAuthor = properties.lastChangedAuthor || 'unknown';
this.lastChangedDate = properties.lastChangedDate || new Date();
this.previewFrame = properties.previewFrame;
}

Expand All @@ -60,9 +79,13 @@ export class Project {
* @return A new project object containing the loaded data.
*/
public static fromJsonObject(json: JsonObject): Project {
const lastChangedDate = new Date(json.lastChangedDate as string);

const project: Project = new Project({
id: json.uuid as string,
name: json.name as string,
lastChangedAuthor: json.lastChangedAuthor as string | undefined,
lastChangedDate: isNaN(lastChangedDate.getTime()) ? undefined : lastChangedDate,
previewFrame: json.previewFrame as string
});

Expand All @@ -82,14 +105,22 @@ export class Project {
return this.id;
}

// TODO
/**
* The last author who edited this project or one of its pages (including elements,
* properties etc.). Updated when calling the touch method.
* @see touch()
*/
public getLastChangedAuthor(): string {
return 'Max Mustermann';
return this.lastChangedAuthor;
}

// TODO
/**
* The last change date when this project or one of its pages was edited (including elements,
* properties etc.). Updated when calling the touch method.
* @see touch()
*/
public getLastChangedDate(): Date {
return new Date();
return this.lastChangedDate;
}

/**
Expand Down Expand Up @@ -149,11 +180,23 @@ export class Project {
return {
uuid: this.id,
name: this.name,
lastChangedAuthor: this.lastChangedAuthor,
lastChangedDate: this.lastChangedDate ? this.lastChangedDate.toJSON() : undefined,
previewFrame: this.previewFrame,
pages: pagesJsonObject
};
}

/**
* Updates the last-changed date and author. Call this on any page or project user command.
*/
public touch(): void {
void (async () => {
this.lastChangedAuthor = await username();
this.lastChangedDate = new Date();
})();
}

/**
* Updates the path of each project's page file from the project and page names, trying to use
* normalized versions of those names, and then finding the next unused file name.
Expand Down

0 comments on commit 947f271

Please sign in to comment.