Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: Add persisted store feature #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@material-ui/styles": "4.11.4",
"classnames": "2.3.1",
"cozy-bar": "7.19.1",
"cozy-client": "^32.5.0",
"cozy-client": "33.0.0",
"cozy-device-helper": "^2.2.1",
"cozy-doctypes": "1.85.0",
"cozy-editor-core": "134.0.3",
Expand All @@ -79,6 +79,7 @@
"react-dom": "18.2.0",
"react-intl": "2.9.0",
"react-router-dom": "6.3.0",
"redux-persist": "^6.0.0",
"rxjs": "5.5.12",
"styled-components": "3.4.10",
"url-search-params-polyfill": "7.0.1",
Expand Down
21 changes: 21 additions & 0 deletions src/targets/browser/configureStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** global __DEVELOPMENT__ **/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed now

import { createStore, combineReducers } from 'redux'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web
import { persistStore, persistReducer } from 'redux-persist'

const configureStore = client => {
const persistConfig = {
key: 'root',
storage
}
const rootReducer = combineReducers({
cozy: client.reducer()
})
const persistedReducer = persistReducer(persistConfig, rootReducer)
const store = createStore(persistedReducer)
const persistor = persistStore(store)

return { store, persistor }
}

export default configureStore
47 changes: 28 additions & 19 deletions src/targets/browser/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
createGenerateClassName
} from '@material-ui/core/styles'
import { fr, en } from '@atlaskit/editor-core/i18n'
import { PersistGate } from 'redux-persist/integration/react'

import MuiCozyTheme from 'cozy-ui/transpiled/react/MuiCozyTheme'
import CozyClient, { CozyProvider } from 'cozy-client'
Expand All @@ -28,6 +29,7 @@ import {
getDataOrDefault,
getPublicSharecode
} from 'lib/initFromDom'
import configureStore from './configureStore'

const manifest = require('../../../manifest.webapp')

Expand Down Expand Up @@ -55,7 +57,7 @@ const generateClassName = createGenerateClassName({
disableGlobal: true
})

const renderApp = function(appLocale, client, isPublic) {
const renderApp = function(appLocale, client, isPublic, persistor) {
const App = require('components/app').default

const container = document.querySelector('[role=application]')
Expand All @@ -73,20 +75,22 @@ const renderApp = function(appLocale, client, isPublic) {
messages={locales[appLocale].atlaskit}
>
<CozyProvider client={client}>
<MuiCozyTheme>
<IsPublicContext.Provider value={isPublic}>
{!isPublic && (
<SharingProvider
doctype="io.cozy.files"
documentType="Notes"
previewPath="/preview/"
>
<App isPublic={isPublic} />
</SharingProvider>
)}
{isPublic && <App isPublic={isPublic} />}
</IsPublicContext.Provider>
</MuiCozyTheme>
<PersistGate loading={null} persistor={persistor}>
<MuiCozyTheme>
<IsPublicContext.Provider value={isPublic}>
{!isPublic && (
<SharingProvider
doctype="io.cozy.files"
documentType="Notes"
previewPath="/preview/"
>
<App isPublic={isPublic} />
</SharingProvider>
)}
{isPublic && <App isPublic={isPublic} />}
</IsPublicContext.Provider>
</MuiCozyTheme>
</PersistGate>
</CozyProvider>
</IntlProvider>
</StylesProvider>
Expand Down Expand Up @@ -131,12 +135,17 @@ export const initApp = () => {
appMetadata: {
slug: appSlug,
version: appVersion
}
},
store: false,
backgroundFetching: true
})

if (!Document.cozyClient) {
Document.registerClient(client)
}
client.registerPlugin(RealtimePlugin)
const { store, persistor } = configureStore(client)
client.setStore(store)

if (!isPublic) {
// initialize the bar, common of all applications, it allows
Expand All @@ -155,13 +164,13 @@ export const initApp = () => {
isPublic: isPublic
})
}
return { appLocale, client, isPublic }
return { appLocale, client, isPublic, persistor }
}

const memoizedInit = memoize(initApp)
const init = () => {
const { appLocale, client, isPublic } = memoizedInit()
renderApp(appLocale, client, isPublic)
const { appLocale, client, isPublic, persistor } = memoizedInit()
renderApp(appLocale, client, isPublic, persistor)
}
document.addEventListener('DOMContentLoaded', init)

Expand Down
41 changes: 23 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7309,19 +7309,16 @@ [email protected]:
lodash "^4.17.20"
node-jose "^1.1.4"

cozy-client@^27.14.4:
version "27.15.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-27.15.0.tgz#7ec2e4ad9a8fdfd9f3998583461d1e34c340c725"
integrity sha512-s4Vqc0FPEnsuD2gqpvzAyItAVXGERVyqAV/H1N1tOGN0rt27HxYycO6aBQz9uHPfoLhzZ/vw0xjrSUsC2tIoEA==
cozy-client@33.0.0:
version "33.0.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-33.0.0.tgz#0711096ce281c9ebfe017a95475ceacf7879dd2e"
integrity sha512-RXzjoii+3/ri99EJTJF+ogEvYKW7qQ2VPcmMS5q1UCBOqziXGnDxND/DbHPX6wZaOynSBfJ/iyBIqhlmEi469w==
dependencies:
"@cozy/minilog" "1.0.0"
"@types/jest" "^26.0.20"
"@types/lodash" "^4.14.170"
btoa "^1.2.1"
cozy-device-helper "^1.12.0"
cozy-flags "2.7.1"
cozy-logger "^1.6.0"
cozy-stack-client "^27.15.0"
cozy-stack-client "^33.0.0"
json-stable-stringify "^1.0.1"
lodash "^4.17.13"
microee "^0.0.6"
Expand All @@ -7336,16 +7333,19 @@ cozy-client@^27.14.4:
sift "^6.0.0"
url-search-params-polyfill "^8.0.0"

cozy-client@^32.5.0:
version "32.5.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-32.5.0.tgz#ae92bc8e0a29325aaa6bbe1d516e8da63eb3fa09"
integrity sha512-a+6eM9y1pmO8Hzjg7EThBqM0MlQoSI41l7/QL6XQgUAZG9zJanb+uOqDE52eY1b00SrwYZURZgESI3/NSG59EA==
cozy-client@^27.14.4:
version "27.15.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-27.15.0.tgz#7ec2e4ad9a8fdfd9f3998583461d1e34c340c725"
integrity sha512-s4Vqc0FPEnsuD2gqpvzAyItAVXGERVyqAV/H1N1tOGN0rt27HxYycO6aBQz9uHPfoLhzZ/vw0xjrSUsC2tIoEA==
dependencies:
"@cozy/minilog" "1.0.0"
"@types/jest" "^26.0.20"
"@types/lodash" "^4.14.170"
btoa "^1.2.1"
cozy-stack-client "^32.3.3"
cozy-device-helper "^1.12.0"
cozy-flags "2.7.1"
cozy-logger "^1.6.0"
cozy-stack-client "^27.15.0"
json-stable-stringify "^1.0.1"
lodash "^4.17.13"
microee "^0.0.6"
Expand Down Expand Up @@ -7707,10 +7707,10 @@ cozy-stack-client@^27.15.0:
mime "^2.4.0"
qs "^6.7.0"

cozy-stack-client@^32.3.3:
version "32.3.3"
resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-32.3.3.tgz#4afa61b60f2c57a1862d2d81799f0f1bcdce6b15"
integrity sha512-LsoRT4J+S4uQcXU1U4gFqWZ3pjFK5NZoFNYdFlo8sNIiUj3gN0UIKHyDV64xo4422WTwX9YEuSjlOL0W7hH/dg==
cozy-stack-client@^33.0.0:
version "33.0.0"
resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-33.0.0.tgz#cd65a0aa82e773653b6e03392508f30792fea2b2"
integrity sha512-SqEhtJXuSDsCeC2goHibozX3c2HzoV/vfnp2J10XORMFcvzfx7pfYuwH5XQrRMtAzhERqqn7UNPjPzb/Yo6ljw==
dependencies:
detect-node "^2.0.4"
mime "^2.4.0"
Expand Down Expand Up @@ -15408,7 +15408,7 @@ raf-schd@^4.0.2:
resolved "https://registry.yarnpkg.com/raf-schd/-/raf-schd-4.0.3.tgz#5d6c34ef46f8b2a0e880a8fcdb743efc5bfdbc1a"
integrity sha512-tQkJl2GRWh83ui2DiPTJz9wEiMN20syf+5oKfB03yYP7ioZcJwsIK8FjrtLwH1m7C7e+Tt2yYBlrOpdT+dyeIQ==

raf@^3.4.0:
raf@^3.4.0, raf@^3.4.1:
version "3.4.1"
resolved "https://registry.yarnpkg.com/raf/-/raf-3.4.1.tgz#0742e99a4a6552f445d73e3ee0328af0ff1ede39"
integrity sha512-Sq4CW4QhwOHE8ucn6J34MqtZCeWFP2aQSmrlroYgqAV1PjStIhJXxYuTgUIfkEk7zTLjmIjLmU5q+fbD1NnOJA==
Expand Down Expand Up @@ -16075,6 +16075,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-5.10.0.tgz#5d8d802c5571e55924efc1c3a9b23575283be62b"
integrity sha512-sSJAzNq7zka3qVHKce1hbvqf0Vf5DuTVm7dr4GtsqQVOexnrvbV47RWFiPxQ8fscnyiuWyD2O92DOxPl0tGCRg==

redux-persist@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-6.0.0.tgz#b4d2972f9859597c130d40d4b146fecdab51b3a8"
integrity sha512-71LLMbUq2r02ng2We9S215LtPu3fY0KgaGE0k8WRgl6RkqxtGfl7HUozz1Dftwsb0D/5mZ8dwAaPbtnzfvbEwQ==

[email protected], redux-thunk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"
Expand Down