Skip to content

Commit

Permalink
!wip add react hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Jun 1, 2019
1 parent 1832882 commit a442cd4
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 68 deletions.
2 changes: 1 addition & 1 deletion config/rollup.classes.config.js
Expand Up @@ -9,4 +9,4 @@ export default {
sourcemap
},
plugins
}
}
2 changes: 1 addition & 1 deletion config/rollup.config.js
Expand Up @@ -9,4 +9,4 @@ export default {
sourcemap
},
plugins
}
}
2 changes: 1 addition & 1 deletion config/rollup.handlers.config.js
Expand Up @@ -9,4 +9,4 @@ export default {
sourcemap
},
plugins
}
}
10 changes: 7 additions & 3 deletions config/rollup.react.config.js
Expand Up @@ -9,7 +9,9 @@ export default {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
hyperactiv: 'hyperactiv'
hyperactiv: 'hyperactiv',
wretch: 'wretch',
normaliz: 'normaliz'
},
sourcemap: true
},
Expand All @@ -19,6 +21,8 @@ export default {
external: [
'react',
'react-dom',
'hyperactiv'
'hyperactiv',
'wretch',
'normaliz'
]
}
}
2 changes: 1 addition & 1 deletion config/rollup.websocket.config.js
Expand Up @@ -25,4 +25,4 @@ const browserBuild = {
export default [
serverBuild,
browserBuild
]
]
4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -20,7 +20,7 @@
"start": "npm run lint && npm run build && npm run test",
"lint": "eslint ./src ./test",
"lint:fix": "eslint --fix ./src ./test",
"build": "rollup -c config/rollup.config.js && npm run build:handlers && npm run build:react && npm run build:classes && npm run build:websocket",
"build": "npm run clean && rollup -c config/rollup.config.js && npm run build:handlers && npm run build:react && npm run build:classes && npm run build:websocket",
"build:handlers": "rollup -c config/rollup.handlers.config.js",
"build:react": "rollup -c config/rollup.react.config.js",
"build:classes": "rollup -c config/rollup.classes.config.js",
Expand All @@ -46,8 +46,6 @@
"eslint-plugin-jest": "^22.6.4",
"eslint-plugin-react": "^7.13.0",
"jest": "^24.8.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"rimraf": "^2.6.3",
"rollup": "^1.12.3",
"rollup-plugin-terser": "^5.0.0",
Expand Down
98 changes: 40 additions & 58 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/react/hooks/index.js
@@ -0,0 +1,2 @@
export * from './useNormalizedRequest'
export * from './useRequest'
40 changes: 40 additions & 0 deletions src/react/hooks/tools.js
@@ -0,0 +1,40 @@
export const defaultSerialize = (method, url) => `${method}@${url}`
export const unicity = _ => _
export const normalizedOperations = {
write(normalizedData, store) {
Object.entries(normalizedData).forEach(([ entity, entityData ]) => {
if(!store[entity]) {
store[entity] = {}
}

Object.entries(entityData).forEach(([ key, value ]) => {
if(store[entity][key]) {
if(Array.isArray(store[entity][key]) && Array.isArray(value)) {
value.forEach(item => {
store[entity][key].push(item)
})
} else if(typeof store[entity][key] === 'object' && typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => {
store[entity][key][k] = v
})
} else {
store[entity][key] = value
}
} else {
store[entity][key] = value
}
})
})
},
read(mappings, store) {
const storeFragment = {}
Object.entries(mappings).forEach(([ entity, ids ]) => {
if(!storeFragment[entity])
storeFragment[entity] = {}
ids.forEach(key => {
storeFragment[entity][key] = store[entity][key]
})
})
return storeFragment
}
}

0 comments on commit a442cd4

Please sign in to comment.