Skip to content

Commit

Permalink
Add order by title option
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-zatloukal-tfs committed Oct 20, 2021
1 parent 9f17841 commit 5b5efca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snipcommand",
"version": "0.1.4dev001",
"version": "0.1.4dev002",
"description": "A free and open source command snippets manager for organize and copy fast.",
"private": true,
"main": "public/electron.js",
Expand Down
27 changes: 22 additions & 5 deletions src/components/SettingsModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class SettingsModal extends React.Component {
const autoClose = StorageHelpers.preference.get('autoClose') || false;
const lineClamp = StorageHelpers.preference.get('lineClamp') || false;
const showCommandInList = StorageHelpers.preference.get('showCommandInList') || false;
this.setState({dbDirectory, backupDirectory, appTheme, onClickAction, autoClose, lineClamp, showCommandInList});
const sortBy = StorageHelpers.preference.get('sortBy') || '';
this.setState({dbDirectory, backupDirectory, appTheme, onClickAction, autoClose, lineClamp, showCommandInList, sortBy});

this.listBackupFiles();
}
Expand Down Expand Up @@ -140,14 +141,17 @@ class SettingsModal extends React.Component {
}

changeShowCommandInList = showCommandInList => {
const {setCommandList} = this.props;
StorageHelpers.preference.set('showCommandInList', showCommandInList);
this.setState({showCommandInList});
setCommandList();
}

changeSortBy = sortBy => {
StorageHelpers.preference.set('sortBy', sortBy);
this.setState({sortBy});
}

render() {
const {dbDirectory, backupDirectory, backupFiles, appTheme, onClickAction, autoClose, lineClamp, showCommandInList} = this.state;
const {dbDirectory, backupDirectory, backupFiles, appTheme, onClickAction, autoClose, lineClamp, showCommandInList, sortBy} = this.state;
const {show, onClose, selectedTab} = this.props;

return (
Expand Down Expand Up @@ -364,7 +368,20 @@ class SettingsModal extends React.Component {
/>
<div className="info">Limit the contents of a code block displayed in the commands list.</div>
</div>


<div className="settings-section">
<div className="settings-section-title">Sort by:</div>
<Button
text="Created"
styleType={sortBy === '' ? 'success' : 'default'}
onClick={() => this.changeSortBy("")}
/>
<Button
text="Title"
styleType={sortBy === 'title' ? 'success' : 'default'}
onClick={() => this.changeSortBy("title")}
/>
</div>
</div>
<div className={`content${selectedTab === 'update' ? ' active' : ''}`}>
<div className="update-section">
Expand Down
15 changes: 8 additions & 7 deletions src/core/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {App} from "./Constants";
let db;

class Api {
constructor() {
constructor(sortBy) {
const dbFilePath = path.join(StorageHelpers.preference.get('storagePath'), App.dbName);
const adapter = new FileSync(dbFilePath);
this.sortBy = StorageHelpers.preference.get('sortBy');
db = lowdb(adapter);
db.defaults({commands: []}).write();
}
Expand All @@ -23,22 +24,22 @@ class Api {

getCommandById = id => db.get('commands').find({id}).value();

getAllCommands = () => db.get('commands').filter({isTrash: false}).value();
getAllCommands = () => db.get('commands').filter({isTrash: false}).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value();

getAllCommandsInTrash = () => db.get('commands').filter({isTrash: true}).value();
getAllCommandsInTrash = () => db.get('commands').filter({isTrash: true}).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value();

getAllUntaggedCommands = () => db.get('commands').filter({tags: "", isTrash: false} || {
tags: null,
isTrash: false
}).value();
}).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value();

getAllFavouriteCommands = () => db.get('commands').filter({isFavourite: true, isTrash: false}).value();
getAllFavouriteCommands = () => db.get('commands').filter({isFavourite: true, isTrash: false}).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value();

getAllTags = () => db.get('commands').filter({isTrash: false}).map('tags').value();

getCommandsContainsTag = tag => db.get('commands').filter((t => t.tags.indexOf(tag) > -1 && t.isTrash === false)).value();
getCommandsContainsTag = tag => db.get('commands').filter((t => t.tags.indexOf(tag) > -1 && t.isTrash === false)).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value();

queryCommand = query => db.get('commands').filter((t => (t.title.toLowerCase().indexOf(query) > -1 || t.command.toLowerCase().indexOf(query) > -1) && t.isTrash === false)).value();
queryCommand = query => db.get('commands').filter((t => (t.title.toLowerCase().indexOf(query) > -1 || t.command.toLowerCase().indexOf(query) > -1) && t.isTrash === false)).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value();
}

export default Api;

0 comments on commit 5b5efca

Please sign in to comment.