This repository was archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
86 lines (71 loc) · 2.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { Component, useEffect } from 'react'
import * as R from 'ramda'
import { compose, graphql } from 'react-apollo'
import { NetworkStatus } from 'apollo-client'
import { connect } from 'react-redux'
import moment from 'moment'
import { useTranslation } from 'react-i18next'
import App from 'components/App'
import { togglePopover } from 'actions'
import AppQuery from './query.gql'
// TODO refactor it into AppPage as a functional component // todo: change name
const RemoteLocale = ({ children, currentUser, languages }) => {
const { i18n } = useTranslation()
useEffect(() => {
const languageId = R.pipe(R.prop('preference'), R.prop('languageId'))(currentUser)
if (!R.isNil(languageId)) {
const chosenLanguage = R.find(R.propEq('id', languageId))(languages)
const languageCode = R.prop('languageCode', chosenLanguage)
i18n.changeLanguage(languageCode, () => {
// TODO: Handle callback (error/success)
})
moment.locale(languageCode)
}
}, [currentUser])
return <>{children}</>
}
class AppPage extends Component {
constructor(props) {
super(props)
}
render() {
const {
data: { networkStatus, currentUser, offices, languages },
togglePopover,
popover,
children,
} = this.props
return (
<RemoteLocale currentUser={currentUser} languages={languages}>
<App
languages={languages}
loading={networkStatus === NetworkStatus.loading}
currentUser={currentUser}
offices={offices}
userPopover={popover && popover.type === 'user' ? popover : null}
toggleUserPopover={R.partial(togglePopover, ['user', {}])}
>
{children}
</App>
</RemoteLocale>
)
}
}
const mapStateToProps = (state, { children }) => {
const { popover } = state.model
return {
popover,
children,
}
}
const withData = compose(
graphql(AppQuery, {
options: {
fetchPolicy: 'cache-and-network',
},
})
)
const withActions = connect(mapStateToProps, {
togglePopover,
})
export default withActions(withData(AppPage))