Skip to content

Commit

Permalink
refactor: CustomerServiceTickets & GlobalNav (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd authored Mar 24, 2021
1 parent 162cd14 commit 47db2c5
Show file tree
Hide file tree
Showing 15 changed files with 1,208 additions and 1,010 deletions.
8 changes: 3 additions & 5 deletions api/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ setTimeout(() => {

AV.Cloud.define('getUserInfo', async (req) => {
const username = req.params.username
if (!username) {
throw new AV.Cloud.Error('The username must be provided', { status: 400 })
if (typeof username !== 'string') {
throw new AV.Cloud.Error('The username must be a string', { status: 400 })
}

const user = await new AV.Query(AV.User)
.equalTo('username', username)
.first({ useMasterKey: true })
if (!user) {
throw new AV.Cloud.Error('Not Found', { status: 404 })
return null
}

return {
...(await getTinyUserInfo(user)),
tags: user.get('tags'),
Expand Down
21 changes: 18 additions & 3 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ const _ = require('lodash')
const moment = require('moment')

exports.TIME_RANGE_MAP = {
today: {
get starts() {
return moment().startOf('day').toDate()
},
get ends() {
return moment().endOf('day').toDate()
},
},
thisMonth: {
starts: moment().startOf('month').toDate(),
ends: moment().endOf('month').toDate(),
Expand Down Expand Up @@ -176,9 +184,16 @@ exports.depthFirstSearchFind = (array, fn) => {
}

exports.getTinyCategoryInfo = (category) => {
return {
objectId: category.id,
name: category.get('name'),
if (typeof category.get === 'function') {
return {
objectId: category.id,
name: category.get('name'),
}
} else {
return {
objectId: category.objectId,
name: category.name,
}
}
}

Expand Down
149 changes: 74 additions & 75 deletions modules/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import i18next from 'i18next'

import './i18n'
import { auth, db } from '../lib/leancloud'
import { AppContext } from './context'
import { isCustomerService } from './common'
import GlobalNav from './GlobalNav'
import css from './App.css'
Expand Down Expand Up @@ -37,6 +38,7 @@ class App extends Component {
constructor(props) {
super(props)
this.state = {
loading: true,
currentUser: auth.currentUser,
isCustomerService: false,
organizations: [],
Expand Down Expand Up @@ -65,28 +67,17 @@ class App extends Component {
componentDidMount() {
this._notificationSystem = this.refs.notificationSystem
const user = this.state.currentUser
if (!user) {
return
if (user) {
return user
.get()
.then((user) => this.refreshGlobalInfo(user))
.catch((err) => this.addNotification(err))
}

return user
.get()
.then((user) => {
return this.refreshGlobalInfo(user)
})
.catch((err) => {
this.refreshGlobalInfo()
this.addNotification(err)
})
return this.refreshGlobalInfo()
}

fetchTagMetadatas() {
return db
.class('TagMetadata')
.find()
.then((tagMetadatas) => {
return tagMetadatas
})
return db.class('TagMetadata').find()
}

refreshTagMetadatas() {
Expand All @@ -98,9 +89,10 @@ class App extends Component {
})
}

refreshGlobalInfo(currentUser) {
async refreshGlobalInfo(currentUser) {
if (!currentUser) {
this.setState({
loading: false,
currentUser: null,
isCustomerService: false,
organizations: [],
Expand All @@ -110,23 +102,26 @@ class App extends Component {
return
}

return Promise.all([
isCustomerService(currentUser),
db.class('Organization').include('memberRole').find(),
this.fetchTagMetadatas(),
]).then(([isCustomerService, organizations, tagMetadatas]) => {
this.setState({ loading: true })
try {
const [isCS, organizations, tagMetadatas] = await Promise.all([
isCustomerService(currentUser),
db.class('Organization').include('memberRole').find(),
this.fetchTagMetadatas(),
])
this.setState({
currentUser,
isCustomerService,
organizations,
tagMetadatas,
isCustomerService: isCS,
})
Raven.setUserContext({
username: currentUser.get('username'),
id: currentUser.id,
})
return
})
} finally {
this.setState({ loading: false })
}
}

onLogin(user) {
Expand Down Expand Up @@ -189,52 +184,58 @@ class App extends Component {
}

return (
<div>
<GlobalNav
currentUser={this.state.currentUser}
isCustomerService={this.state.isCustomerService}
logout={this.logout.bind(this)}
/>
<div className={'container ' + css.main}>
<Switch>
<Route path="/" exact>
<Home {...props} />
</Route>
<Route path="/about" component={About} />
<Route path="/login">
<Login {...props} />
</Route>
<AuthRoute path="/tickets" exact>
<Tickets {...props} />
</AuthRoute>
<AuthRoute path="/tickets/new">
<NewTicket {...props} />
</AuthRoute>
<AuthRoute path="/tickets/:nid">
<Ticket {...props} />
</AuthRoute>
<AuthRoute path="/messages">
<Messages {...props} />
</AuthRoute>
<AuthRoute path="/notifications">
<Notifications {...props} />
</AuthRoute>
<AuthRoute mustCustomerService path="/customerService">
<CustomerService />
</AuthRoute>
<AuthRoute path="/users/:username">
<User {...props} />
</AuthRoute>
<AuthRoute path="/settings">
<Settings {...props} />
</AuthRoute>
<Route path="/error" component={ErrorPage} />
<Route path="*" component={NotFound} />
</Switch>
</div>
<>
{!this.state.loading && (
<AppContext.Provider
value={{
isCustomerService: props.isCustomerService,
tagMetadatas: props.tagMetadatas,
addNotification: props.addNotification,
}}
>
<GlobalNav user={this.state.currentUser?.toJSON()} onLogout={this.logout.bind(this)} />
<div className={'container ' + css.main}>
<Switch>
<Route path="/" exact>
<Home {...props} />
</Route>
<Route path="/about" component={About} />
<Route path="/login">
<Login {...props} />
</Route>
<AuthRoute path="/tickets" exact>
<Tickets {...props} />
</AuthRoute>
<AuthRoute path="/tickets/new">
<NewTicket {...props} />
</AuthRoute>
<AuthRoute path="/tickets/:nid">
<Ticket {...props} />
</AuthRoute>
<AuthRoute path="/messages">
<Messages {...props} />
</AuthRoute>
<AuthRoute path="/notifications">
<Notifications {...props} />
</AuthRoute>
<AuthRoute mustCustomerService path="/customerService">
<CustomerService />
</AuthRoute>
<AuthRoute path="/users/:username">
<User {...props} />
</AuthRoute>
<AuthRoute path="/settings">
<Settings {...props} />
</AuthRoute>
<Route path="/error" component={ErrorPage} />
<Route path="*" component={NotFound} />
</Switch>
</div>
</AppContext.Provider>
)}
<ServerNotification currentUser={this.state.currentUser} />
<NotificationSystem ref="notificationSystem" />
</div>
</>
)
}
}
Expand Down Expand Up @@ -281,9 +282,7 @@ class ServerNotification extends Component {
}

updateLiveQuery() {
if (this.messageLiveQuery) {
this.messageLiveQuery.unsubscribe()
}
this.messageLiveQuery?.unsubscribe()
if (!this.props.currentUser) {
return
}
Expand Down Expand Up @@ -323,7 +322,7 @@ class ServerNotification extends Component {
}

componentWillUnmount() {
return this.messageLiveQuery.unsubscribe()
this.messageLiveQuery?.unsubscribe()
}

notify({ title, body }) {
Expand Down
Loading

0 comments on commit 47db2c5

Please sign in to comment.