2017-05-15 21:10:00 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { View, Button, TextInput } from 'react-native';
|
|
|
|
import { connect } from 'react-redux'
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Log } from 'lib/log.js'
|
2017-07-15 20:37:17 +02:00
|
|
|
import { ActionButton } from 'lib/components/action-button.js';
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Folder } from 'lib/models/folder.js'
|
2017-07-05 23:29:00 +02:00
|
|
|
import { BaseModel } from 'lib/base-model.js'
|
2017-06-24 20:06:28 +02:00
|
|
|
import { ScreenHeader } from 'lib/components/screen-header.js';
|
2017-07-16 23:17:22 +02:00
|
|
|
import { reg } from 'lib/registry.js';
|
2017-07-13 01:01:15 +02:00
|
|
|
import { NotesScreenUtils } from 'lib/components/screens/notes-utils.js'
|
2017-07-14 20:49:14 +02:00
|
|
|
import { BaseScreenComponent } from 'lib/components/base-screen.js';
|
2017-07-15 18:25:33 +02:00
|
|
|
import { dialogs } from 'lib/dialogs.js';
|
|
|
|
import { _ } from 'lib/locale.js';
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2017-07-14 20:49:14 +02:00
|
|
|
class FolderScreenComponent extends BaseScreenComponent {
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
static navigationOptions(options) {
|
2017-05-16 21:57:09 +02:00
|
|
|
return { header: null };
|
|
|
|
}
|
2017-05-15 21:10:00 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2017-07-15 20:37:17 +02:00
|
|
|
this.state = {
|
|
|
|
folder: Folder.new(),
|
|
|
|
lastSavedFolder: null,
|
|
|
|
};
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2017-05-23 22:39:01 +02:00
|
|
|
if (!this.props.folderId) {
|
2017-07-15 20:37:17 +02:00
|
|
|
const folder = Folder.new();
|
|
|
|
this.setState({
|
|
|
|
folder: folder,
|
|
|
|
lastSavedFolder: Object.assign({}, folder),
|
|
|
|
});
|
2017-05-23 22:39:01 +02:00
|
|
|
} else {
|
|
|
|
Folder.load(this.props.folderId).then((folder) => {
|
2017-07-15 20:37:17 +02:00
|
|
|
this.setState({
|
|
|
|
folder: folder,
|
|
|
|
lastSavedFolder: Object.assign({}, folder),
|
|
|
|
});
|
2017-05-23 22:39:01 +02:00
|
|
|
});
|
|
|
|
}
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 20:37:17 +02:00
|
|
|
isModified() {
|
|
|
|
if (!this.state.folder || !this.state.lastSavedFolder) return false;
|
|
|
|
let diff = BaseModel.diffObjects(this.state.folder, this.state.lastSavedFolder);
|
|
|
|
delete diff.type_;
|
|
|
|
return !!Object.getOwnPropertyNames(diff).length;
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
folderComponent_change(propName, propValue) {
|
2017-05-15 21:10:00 +02:00
|
|
|
this.setState((prevState, props) => {
|
|
|
|
let folder = Object.assign({}, prevState.folder);
|
|
|
|
folder[propName] = propValue;
|
|
|
|
return { folder: folder }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
title_changeText(text) {
|
2017-05-15 21:10:00 +02:00
|
|
|
this.folderComponent_change('title', text);
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:29:00 +02:00
|
|
|
async saveFolderButton_press() {
|
2017-07-15 20:37:17 +02:00
|
|
|
let folder = Object.assign({}, this.state.folder);
|
2017-07-05 23:29:00 +02:00
|
|
|
|
2017-07-15 18:25:33 +02:00
|
|
|
try {
|
2017-07-17 21:19:01 +02:00
|
|
|
folder = await Folder.save(folder, { userSideValidation: true });
|
2017-07-16 23:17:22 +02:00
|
|
|
|
|
|
|
reg.scheduleSync();
|
2017-07-15 18:25:33 +02:00
|
|
|
} catch (error) {
|
2017-07-24 23:29:40 +02:00
|
|
|
dialogs.error(this, _('The notebook could not be saved: %s', error.message));
|
2017-07-15 18:25:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-15 20:37:17 +02:00
|
|
|
this.setState({
|
|
|
|
lastSavedFolder: Object.assign({}, folder),
|
|
|
|
folder: folder,
|
|
|
|
});
|
2017-07-09 00:57:09 +02:00
|
|
|
|
2017-07-15 20:37:17 +02:00
|
|
|
await NotesScreenUtils.openNoteList(folder.id);
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-07-16 12:17:40 +02:00
|
|
|
let saveButtonDisabled = !this.isModified();
|
2017-07-15 20:13:31 +02:00
|
|
|
|
2017-05-15 21:10:00 +02:00
|
|
|
return (
|
2017-07-14 20:49:14 +02:00
|
|
|
<View style={this.styles().screen}>
|
2017-07-16 12:17:40 +02:00
|
|
|
<ScreenHeader
|
|
|
|
navState={this.props.navigation.state}
|
|
|
|
showSaveButton={true}
|
|
|
|
saveButtonDisabled={saveButtonDisabled}
|
|
|
|
onSaveButtonPress={() => this.saveFolderButton_press()}
|
|
|
|
/>
|
2017-07-16 01:30:54 +02:00
|
|
|
<TextInput autoFocus={true} value={this.state.folder.title} onChangeText={(text) => this.title_changeText(text)} />
|
2017-07-15 18:25:33 +02:00
|
|
|
<dialogs.DialogBox ref={dialogbox => { this.dialogbox = dialogbox }}/>
|
2017-05-15 21:10:00 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const FolderScreen = connect(
|
|
|
|
(state) => {
|
|
|
|
return {
|
2017-05-23 22:39:01 +02:00
|
|
|
folderId: state.selectedFolderId,
|
2017-05-15 21:10:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
)(FolderScreenComponent)
|
|
|
|
|
|
|
|
export { FolderScreen };
|