Skip to content

Commit

Permalink
Remember last opened / created folder
Browse files Browse the repository at this point in the history
closes #107
  • Loading branch information
meriadec committed Apr 28, 2017
1 parent 52a4271 commit f2248ac
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
9 changes: 8 additions & 1 deletion app/actions/projects.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import fs from 'fs'
import os from 'os'
import path from 'path'
import { replace } from 'react-router-redux'

import {
saveSettings,
cleanBadProjects,
saveLastOpenedFolder,
} from 'actions/settings'

import {
Expand All @@ -18,10 +20,14 @@ import {

import mjml2html from 'helpers/mjml'

const HOME_DIR = os.homedir()

export function addProject (p) {
return async dispatch => {
return async (dispatch, getState) => {
if (!p) {
const state = getState()
p = fileDialog({
defaultPath: state.settings.get('lastOpenedFolder') || HOME_DIR,
properties: [
'openDirectory',
'createDirectory',
Expand All @@ -32,6 +38,7 @@ export function addProject (p) {

await fsAccess(p, fs.constants.R_OK | fs.constants.W_OK)

dispatch(saveLastOpenedFolder(p))
dispatch(openProject(p))
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/actions/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function loadSettings () {
return async dispatch => {
const res = await storageGet('settings')
const settings = defaultsDeep(res, {
lastOpenedFolder: null,
editor: {},
projects: [],
api: {},
Expand Down Expand Up @@ -58,3 +59,11 @@ export function updateSettings (updater) {
dispatch(saveSettings())
}
}

export function saveLastOpenedFolder (path) {
return (dispatch) => {
dispatch(updateSettings(settings => {
return settings.set('lastOpenedFolder', path)
}))
}
}
2 changes: 1 addition & 1 deletion app/components/Application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Application extends Component {
{projects && this.props.children}

{settings && <SettingsModal />}
{settings && <NewProjectModal />}

<NewProjectModal />
<Alerts />
<NotifsPanel />

Expand Down
8 changes: 8 additions & 0 deletions app/components/NewProjectModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const defaultState = {

@connect(state => ({
isOpened: isModalOpened(state, 'newProject'),
lastOpenedFolder: state.settings.get('lastOpenedFolder'),
}), dispatch => ({
closeModal: () => dispatch(closeModal('newProject')),
...bindActionCreators({
Expand All @@ -57,6 +58,11 @@ class NewProjectModal extends Component {
state = defaultState

componentWillReceiveProps (nextProps) {
if (!this.props.isOpened && nextProps.isOpened) {
this.setState({
projectLocation: this.props.lastOpenedFolder || HOME_DIR,
})
}
if (this.props.isOpened && !nextProps.isOpened) {

// prevent flashing the resetted content
Expand All @@ -80,7 +86,9 @@ class NewProjectModal extends Component {
}

handleBrowse = () => {
const { lastOpenedFolder } = this.props
const p = fileDialog({
defaultPath: lastOpenedFolder || undefined,
properties: [
'openDirectory',
'createDirectory',
Expand Down
1 change: 1 addition & 0 deletions app/reducers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default handleActions({

SETTINGS_LOAD_SUCCESS: (state, { payload }) => {
return Map({
lastOpenedFolder: payload.lastOpenedFolder,
projects: List(payload.projects),
editor: Map(payload.editor),
api: Map(payload.api),
Expand Down

0 comments on commit f2248ac

Please sign in to comment.