import React, { PropTypes, findDOMNode } from 'react'
import { connect, Provider } from 'react-redux'
import auth from 'boost/auth'
import linkState from 'boost/linkState'
import Select from 'react-select'
import api from 'boost/api'
import ProfileImage from 'boost/components/ProfileImage'
import store from 'boost/store'
const PROFILE = 'PROFILE'
const PREFERENCES = 'PREFERENCES'
const HELP = 'HELP'
const TEAM = 'TEAM'
const MEMBER = 'MEMBER'
const FOLDER = 'FOLDER'
function getUsers (input, cb) {
api.searchUser(input)
.then(function (res) {
let users = res.body
cb(null, {
options: users.map(user => {
return { value: user.name, label: user.name }
}),
complete: false
})
})
.catch(function (err) {
console.error(err)
})
}
class Preferences extends React.Component {
constructor (props) {
super(props)
this.state = {
currentTab: PROFILE,
profile: {
userInfo: {
profileName: props.currentUser.profileName,
email: props.currentUser.email,
alert: null
},
password: {
currentPassword: '',
newPassword: '',
confirmation: '',
error: null
}
}
}
}
handleNavButtonClick (tab) {
return e => {
this.setState({currentTab: tab})
}
}
render () {
let content = this.renderContent()
let tabs = [
{target: PROFILE, label: 'Profile'},
{target: PREFERENCES, label: 'Preferences'},
{target: HELP, label: 'Help & Feedback'},
{target: TEAM, label: 'Team setting'},
{target: MEMBER, label: 'Manage member'},
{target: FOLDER, label: 'Manage folder'}
]
let navButtons = tabs.map(tab => (
))
return (
Setting
{navButtons}
{content}
)
}
renderContent () {
switch (this.state.currentTab) {
case PREFERENCES:
return this.renderPreferences()
case HELP:
return this.renderHelp()
case TEAM:
return this.renderTeamSetting()
case MEMBER:
return this.renderMemberSetting()
case FOLDER:
return this.renderFolderSetting()
case PROFILE:
default:
return this.renderProfile()
}
}
handleProfileSaveButtonClick (e) {
let profileState = this.state.profile
profileState.userInfo.alert = {
type: 'info',
message: 'Sending...'
}
this.setState({profile: profileState}, () => {
let input = {
profileName: profileState.userInfo.profileName,
email: profileState.userInfo.email
}
api.updateUserInfo(input)
.then(res => {
let profileState = this.state.profile
profileState.userInfo.alert = {
type: 'success',
message: 'Successfully done!'
}
this.setState({profile: profileState})
})
.catch(err => {
var message
if (err.status != null) {
message = err.response.body.message
} else if (err.code === 'ECONNREFUSED') {
message = 'Can\'t connect to API server.'
} else throw err
let profileState = this.state.profile
profileState.userInfo.alert = {
type: 'error',
message: message
}
this.setState({profile: profileState})
})
})
}
handlePasswordSaveButton (e) {
let profileState = this.state.profile
if (profileState.password.newPassword !== profileState.password.confirmation) {
profileState.password.alert = {
type: 'error',
message: 'Confirmation doesn\'t match'
}
this.setState({profile: profileState})
return
}
profileState.password.alert = {
type: 'info',
message: 'Sending...'
}
this.setState({profile: profileState}, () => {
let input = {
password: profileState.password.currentPassword,
newPassword: profileState.password.newPassword
}
api.updatePassword(input)
.then(res => {
let profileState = this.state.profile
profileState.password.alert = {
type: 'success',
message: 'Successfully done!'
}
profileState.password.currentPassword = ''
profileState.password.newPassword = ''
profileState.password.confirmation = ''
this.setState({profile: profileState})
})
.catch(err => {
var message
if (err.status != null) {
message = err.response.body.message
} else if (err.code === 'ECONNREFUSED') {
message = 'Can\'t connect to API server.'
} else throw err
let profileState = this.state.profile
profileState.password.alert = {
type: 'error',
message: message
}
profileState.password.currentPassword = ''
profileState.password.newPassword = ''
profileState.password.confirmation = ''
this.setState({profile: profileState}, () => {
if (this.refs.currentPassword != null) findDOMNode(this.refs.currentPassword).focus()
})
})
})
}
renderProfile () {
let profileState = this.state.profile
return (
User Info
{this.state.profile.userInfo.alert != null
? (
{profileState.userInfo.alert.message}
)
: null}
)
}
renderPreferences () {
return (
Hotkey
0 to 9
A to Z
F1 to F24
- Punctuations like
~, !, @, #, $, etc.
Plus
Space
Backspace
Delete
Insert
Return (or Enter as alias)
Up, Down, Left and Right
Home and End
PageUp and PageDown
Escape (or Esc for short)
VolumeUp, VolumeDown and VolumeMute
MediaNextTrack, MediaPreviousTrack, MediaStop and MediaPlayPause
)
}
renderHelp () {
return (
Comming soon
)
}
renderTeamSetting () {
return (
{false
? (
)
: (
Are you sure to delete this team?
)}
)
}
renderMemberSetting () {
let membersEl = [].map(member => {
let isCurrentUser = this.state.currentUser.id === member.id
return (
{`${member.profileName} (${member.name})`}
{member.email}
)
})
return (
)
}
renderFolderSetting () {
return (
)
}
}
Preferences.propTypes = {
currentUser: PropTypes.shape(),
close: PropTypes.func
}
Preferences.prototype.linkState = linkState
function remap (state) {
let currentUser = state.currentUser
return {
currentUser
}
}
let RootComponent = connect(remap)(Preferences)
export default class PreferencesModal extends React.Component {
render () {
return (
{() => }
)
}
}