Skip to content

Commit 1a11095

Browse files
committed
User name change and modify style
1 parent 96a8687 commit 1a11095

8 files changed

Lines changed: 132 additions & 21 deletions

File tree

browser/main/HomePage.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { PropTypes} from 'react'
22
import { connect } from 'react-redux'
3-
import { EDIT_MODE, IDLE_MODE, NEW, toggleTutorial } from 'boost/actions'
4-
// import UserNavigator from './HomePage/UserNavigator'
3+
import { EDIT_MODE, IDLE_MODE, toggleTutorial } from 'boost/actions'
54
import ArticleNavigator from './HomePage/ArticleNavigator'
65
import ArticleTopBar from './HomePage/ArticleTopBar'
76
import ArticleList from './HomePage/ArticleList'
@@ -114,13 +113,14 @@ class HomePage extends React.Component {
114113
}
115114

116115
render () {
117-
let { dispatch, status, articles, allArticles, activeArticle, folders, tags, filters } = this.props
116+
let { dispatch, status, user, articles, allArticles, activeArticle, folders, tags, filters } = this.props
118117

119118
return (
120119
<div className='HomePage'>
121120
<ArticleNavigator
122121
ref='nav'
123122
dispatch={dispatch}
123+
user={user}
124124
folders={folders}
125125
status={status}
126126
allArticles={allArticles}
@@ -172,7 +172,7 @@ function buildFilter (key) {
172172
}
173173

174174
function remap (state) {
175-
let { folders, articles, status } = state
175+
let { user, folders, articles, status } = state
176176

177177
if (articles == null) articles = []
178178
articles.sort((a, b) => {
@@ -234,6 +234,7 @@ function remap (state) {
234234
if (activeArticle == null) activeArticle = articles[0]
235235

236236
return {
237+
user,
237238
folders,
238239
status,
239240
allArticles,
@@ -249,11 +250,9 @@ function remap (state) {
249250
}
250251

251252
HomePage.propTypes = {
252-
params: PropTypes.shape({
253-
userId: PropTypes.string
254-
}),
255-
status: PropTypes.shape({
256-
userId: PropTypes.string
253+
status: PropTypes.shape(),
254+
user: PropTypes.shape({
255+
name: PropTypes.string
257256
}),
258257
articles: PropTypes.array,
259258
allArticles: PropTypes.array,

browser/main/HomePage/ArticleNavigator.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import Preferences from 'boost/components/modal/Preferences'
77
import CreateNewFolder from 'boost/components/modal/CreateNewFolder'
88
import keygen from 'boost/keygen'
99

10-
const electron = require('electron')
11-
const remote = electron.remote
12-
let userName = remote.getGlobal('process').env.USER
13-
1410
const BRAND_COLOR = '#18AF90'
1511

1612
const preferenceTutorialElement = (
@@ -109,7 +105,7 @@ export default class ArticleNavigator extends React.Component {
109105
}
110106

111107
render () {
112-
let { status, folders, allArticles } = this.props
108+
let { status, user, folders, allArticles } = this.props
113109
let { targetFolders } = status
114110
if (targetFolders == null) targetFolders = []
115111

@@ -127,7 +123,7 @@ export default class ArticleNavigator extends React.Component {
127123
return (
128124
<div className='ArticleNavigator'>
129125
<div className='userInfo'>
130-
<div className='userProfileName'>{userName}</div>
126+
<div className='userProfileName'>{user.name}</div>
131127
<div className='userName'>localStorage</div>
132128
<button onClick={e => this.handlePreferencesButtonClick(e)} className='settingBtn'>
133129
<i className='fa fa-fw fa-chevron-down'/>

lib/actions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Action types
2+
export const USER_UPDATE = 'USER_UPDATE'
3+
24
export const CLEAR_NEW_ARTICLE = 'CLEAR_NEW_ARTICLE'
35
export const ARTICLE_UPDATE = 'ARTICLE_UPDATE'
46
export const ARTICLE_DESTROY = 'ARTICLE_DESTROY'
@@ -24,13 +26,21 @@ export const EDIT_MODE = 'EDIT_MODE'
2426
// Article status
2527
export const NEW = 'NEW'
2628

29+
export function updateUser (input) {
30+
return {
31+
type: USER_UPDATE,
32+
data: input
33+
}
34+
}
35+
2736
// DB
2837
export function clearNewArticle () {
2938
return {
3039
type: CLEAR_NEW_ARTICLE
3140
}
3241
}
3342

43+
3444
export function updateArticle (article) {
3545
return {
3646
type: ARTICLE_UPDATE,

lib/components/modal/Preference/AppSettingTab.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react'
1+
import React, { PropTypes } from 'react'
22
import linkState from 'boost/linkState'
3+
import { updateUser } from 'boost/actions'
34

45
const electron = require('electron')
56
const ipc = electron.ipcRenderer
@@ -9,9 +10,14 @@ export default class AppSettingTab extends React.Component {
910
constructor (props) {
1011
super(props)
1112
let keymap = remote.getGlobal('keymap')
13+
let userName = props.user != null ? props.user.name : null
1214

1315
this.state = {
1416
toggleFinder: keymap.toggleFinder,
17+
user: {
18+
name: userName,
19+
alert: null
20+
},
1521
alert: null
1622
}
1723
}
@@ -54,6 +60,12 @@ export default class AppSettingTab extends React.Component {
5460
}
5561
}
5662

63+
handleNameSaveButtonClick (e) {
64+
let { dispatch } = this.props
65+
66+
dispatch(updateUser({name: this.state.user.name}))
67+
}
68+
5769
render () {
5870
let alert = this.state.alert
5971
let alertElement = alert != null ? (
@@ -64,6 +76,17 @@ export default class AppSettingTab extends React.Component {
6476

6577
return (
6678
<div className='AppSettingTab content'>
79+
<div className='section'>
80+
<div className='sectionTitle'>User's info</div>
81+
<div className='sectionInput'>
82+
<label>User name</label>
83+
<input valueLink={this.linkState('user.name')} type='text'/>
84+
</div>
85+
<div className='sectionConfirm'>
86+
<button onClick={e => this.handleNameSaveButtonClick(e)}>Save</button>
87+
{alertElement}
88+
</div>
89+
</div>
6790
<div className='section'>
6891
<div className='sectionTitle'>Hotkey</div>
6992
<div className='sectionInput'>
@@ -101,3 +124,6 @@ export default class AppSettingTab extends React.Component {
101124
}
102125

103126
AppSettingTab.prototype.linkState = linkState
127+
AppSettingTab.propTypes = {
128+
dispatch: PropTypes.func
129+
}

lib/components/modal/Preferences.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Preferences extends React.Component {
6262
}
6363

6464
renderContent () {
65-
let { folders, dispatch } = this.props
65+
let { user, folders, dispatch } = this.props
6666

6767
switch (this.state.currentTab) {
6868
case HELP:
@@ -80,22 +80,31 @@ class Preferences extends React.Component {
8080
)
8181
case APP:
8282
default:
83-
return (<AppSettingTab/>)
83+
return (
84+
<AppSettingTab
85+
user={user}
86+
dispatch={dispatch}
87+
/>
88+
)
8489
}
8590
}
8691
}
8792

8893
Preferences.propTypes = {
94+
user: PropTypes.shape({
95+
name: PropTypes.string
96+
}),
8997
folders: PropTypes.array,
9098
dispatch: PropTypes.func
9199
}
92100

93101
Preferences.prototype.linkState = linkState
94102

95103
function remap (state) {
96-
let { folders, status } = state
104+
let { user, folders, status } = state
97105

98106
return {
107+
user,
99108
folders,
100109
status
101110
}

lib/dataStore.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import keygen from 'boost/keygen'
2+
import _ from 'lodash'
3+
24
const electron = require('electron')
35
const remote = electron.remote
46
const jetpack = require('fs-jetpack')
@@ -10,15 +12,66 @@ function getLocalPath () {
1012
return path.join(remote.app.getPath('userData'), 'local.json')
1113
}
1214

15+
function forgeInitialRepositories () {
16+
return [{
17+
key: keygen(),
18+
name: 'local',
19+
type: 'userData',
20+
user: {
21+
name: remote.getGlobal('process').env.USER
22+
}
23+
}]
24+
}
25+
26+
function getRepositories () {
27+
let raw = localStorage.getItem('repositories')
28+
try {
29+
let parsed = JSON.parse(raw)
30+
if (!_.isArray(parsed)) {
31+
throw new Error('repositories data is currupte. re-init data.')
32+
}
33+
return parsed
34+
} catch (e) {
35+
console.log(e)
36+
let newRepos = forgeInitialRepositories()
37+
saveRepositories(newRepos)
38+
return newRepos
39+
}
40+
}
41+
42+
function saveRepositories (repos) {
43+
localStorage.setItem('repositories', JSON.stringify(repos))
44+
}
45+
46+
export function getUser (repoName) {
47+
if (repoName == null) {
48+
return getRepositories()[0]
49+
}
50+
return null
51+
}
52+
53+
export function saveUser (repoName, user) {
54+
let repos = getRepositories()
55+
if (repoName == null) {
56+
Object.assign(repos[0].user, user)
57+
}
58+
saveRepositories(repos)
59+
}
60+
1361
export function init () {
14-
console.log('initialize data store')
62+
// set repositories info
63+
getRepositories()
64+
65+
// set local.json
1566
let data = jetpack.read(getLocalPath(), 'json')
1667

1768
if (data == null) {
69+
// for 0.4.1 -> 0.4.2
1870
if (localStorage.getItem('local') != null) {
1971
data = JSON.parse(localStorage.getItem('local'))
2072
jetpack.write(getLocalPath(), data)
2173
localStorage.removeItem('local')
74+
console.log('update 0.4.1 => 0.4.2')
2275
return
2376
}
2477

@@ -70,6 +123,8 @@ export default (function () {
70123
init()
71124
}
72125
return {
126+
getUser,
127+
saveUser,
73128
init,
74129
getData,
75130
setArticles,

lib/keygen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ var crypto = require('crypto')
22

33
module.exports = function () {
44
var shasum = crypto.createHash('sha1')
5-
shasum.update(((new Date()).getTime()).toString())
5+
shasum.update(((new Date()).getTime() + Math.round(Math.random()*1000)).toString())
66
return shasum.digest('hex')
77
}

lib/reducer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
UNLOCK_STATUS,
1313
TOGGLE_TUTORIAL,
1414

15+
// user
16+
USER_UPDATE,
17+
1518
// Article action type
1619
ARTICLE_UPDATE,
1720
ARTICLE_DESTROY,
@@ -42,10 +45,22 @@ const initialStatus = {
4245
let data = dataStore.getData()
4346
let initialArticles = data.articles
4447
let initialFolders = data.folders
48+
let initialUser = dataStore.getUser().user
4549

4650
let isStatusLocked = false
4751
let isCreatingNew = false
4852

53+
function user (state = initialUser, action) {
54+
switch (action.type) {
55+
case USER_UPDATE:
56+
let updated = Object.assign(state, action.data)
57+
dataStore.saveUser(updated)
58+
return updated
59+
default:
60+
return state
61+
}
62+
}
63+
4964
function folders (state = initialFolders, action) {
5065
state = state.slice()
5166
switch (action.type) {
@@ -250,6 +265,7 @@ function status (state = initialStatus, action) {
250265
}
251266

252267
export default combineReducers({
268+
user,
253269
folders,
254270
articles,
255271
status

0 commit comments

Comments
 (0)