Renamed DocumentEditState to DocumentEditStore
This commit is contained in:
96
src/scenes/DocumentEdit/DocumentEditStore.js
Normal file
96
src/scenes/DocumentEdit/DocumentEditStore.js
Normal file
@@ -0,0 +1,96 @@
|
||||
import { observable, action, computed, autorun } from 'mobx';
|
||||
import { client } from 'utils/ApiClient';
|
||||
import localforage from 'localforage';
|
||||
import { browserHistory } from 'react-router'
|
||||
|
||||
const DOCUMENT_EDIT_SETTINGS = 'DOCUMENT_EDIT_SETTINGS';
|
||||
|
||||
const parseHeader = (text) => {
|
||||
const firstLine = text.split(/\r?\n/)[0];
|
||||
const match = firstLine.match(/^#+ +(.*)$/);
|
||||
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
}
|
||||
|
||||
const documentEditStore = new class DocumentEditStore {
|
||||
@observable documentId = null;
|
||||
@observable title = 'title';
|
||||
@observable text = 'default state';
|
||||
|
||||
@observable preview;
|
||||
@observable isFetching;
|
||||
@observable isSaving;
|
||||
|
||||
/* Actions */
|
||||
|
||||
@action fetchDocument = async () => {
|
||||
this.isFetching = true;
|
||||
|
||||
try {
|
||||
const data = await client.post('/documents.info', {
|
||||
id: this.documentId,
|
||||
})
|
||||
const { title, text } = data.data;
|
||||
this.title = title;
|
||||
this.text = text;
|
||||
} catch (e) {
|
||||
console.error("Something went wrong");
|
||||
}
|
||||
this.isFetching = false;
|
||||
}
|
||||
|
||||
@action updateDocument = async (nextPath) => {
|
||||
if (this.isSaving) return;
|
||||
|
||||
this.isSaving = true;
|
||||
|
||||
try {
|
||||
const data = await client.post('/documents.update', {
|
||||
id: this.documentId,
|
||||
title: this.title,
|
||||
text: this.text,
|
||||
})
|
||||
browserHistory.push(`/documents/${this.documentId}`);
|
||||
} catch (e) {
|
||||
console.error("Something went wrong");
|
||||
}
|
||||
this.isSaving = false;
|
||||
}
|
||||
|
||||
@action updateText = (text) => {
|
||||
this.text = text;
|
||||
this.title = parseHeader(text);
|
||||
}
|
||||
|
||||
@action updateTitle = (title) => {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@action replaceText = (args) => {
|
||||
this.text = this.text.replace(args.original, args.new);
|
||||
}
|
||||
|
||||
@action togglePreview = () => {
|
||||
this.preview = !this.preview;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
// Rehydrate settings
|
||||
localforage.getItem(DOCUMENT_EDIT_SETTINGS)
|
||||
.then(data => {
|
||||
this.preview = data.preview;
|
||||
});
|
||||
}
|
||||
}();
|
||||
|
||||
// Persist settings to localStorage
|
||||
autorun(() => {
|
||||
localforage.setItem(DOCUMENT_EDIT_SETTINGS, {
|
||||
preview: documentEditStore.preview,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
export default documentEditStore;
|
||||
Reference in New Issue
Block a user