Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 3x 6x 1x 1x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 8x 8x 7x 7x 7x 8x 8x 8x 5x 8x 8x 1x 7x 8x 8x 33x 13x 20x 13x 7x 7x 4x 2x 2x 2x 3x 13x | // @flow import * as React from 'react'; import {generateAction} from '../action'; import {createEmptyData} from 'canner-helpers'; import RefId from 'canner-ref-id'; import type {HOCProps} from './types'; import {update, merge, isArray, isPlainObject} from 'lodash'; type changeQueue = Array<{RefId: RefId | {firstRefId: RefId, secondRefId: RefId}, type: any, value: any}>; export default function withRequest(Com: React.ComponentType<*>) { // this hoc will update data; return class ComponentWithRequest extends React.Component<HOCProps> { onChange = (refId: RefId | {firstRefId: RefId, secondRefId: RefId} | changeQueue, type: any, delta: any, config: any): Promise<*> => { let id; if (isArray(refId)) { // changeQueue const changeQueue = refId; // $FlowFixMe return Promise.all(changeQueue.map(args => { const {refId, type, value} = args; return this.onChange(refId, type, value); })); } else Eif (refId instanceof RefId) { id = refId.toString(); } else { id = { // $FlowFixMe: refId should be object here firstId: refId.firstRefId.toString(), // $FlowFixMe: refId should be object here secondId: refId.secondRefId.toString() }; } const {schema, rootValue, request} = this.props; const itemSchema = findSchemaByRefId(schema, refId); const {relation, items, pattern} = itemSchema; const action = createAction({ relation, id, type, value: delta, config, rootValue: {...rootValue}, items, pattern }); Iif (!action) { throw new Error('invalid change'); } Iif (action.type === 'NOOP') { return Promise.resolve(); } // $FlowFixMe return request(action); } render() { return <Com {...this.props} onChange={this.onChange} />; } }; } export function createAction({ relation, id, type, value, config, rootValue, items, }: { relation: any, id: {firstId: string, secondId: string} | string, type: 'create' | 'update' | 'delete' | 'swap', value: any, config: any, rootValue: any, value: any, items: any }) { Eif (type === 'create') { if (!config) { const emptyData = createEmptyData(items); Eif (isPlainObject(emptyData)) { value = merge(emptyData, value); } else { value = emptyData; } } Eif (typeof id === 'string' && id.split('/').length === 1) { update(value, 'id', id => id || randomId()); } } return generateAction({ id, updateType: type, value, rootValue, relation }); } function randomId() { return Math.random().toString(36).substr(2, 12); } export function findSchemaByRefId(schema: Object, refId: any) { let paths = []; if (isPlainObject(refId)) { paths = refId.firstRefId.getPathArr(); } else { paths = refId.getPathArr(); } let pattern = ''; return loop(schema, paths, pattern); } export function loop(schema: Object, paths: Array<string>, pattern: string): any { if (paths.length === 0) { return { ...schema, pattern: removeFirstSlash(pattern) }; } if (!schema.type) { // in items of object return loop(schema[paths[0]], paths.slice(1), `${pattern}/${schema[paths[0]].type}`); } Iif (schema.type === 'json') { return {}; } if (schema.type === 'array') { if (paths.length === 1) { // paths = [index], so just return return loop(schema, [], pattern); } else Eif (schema.items.type === 'object') { // path[0] is index, so we take the paths[1] return loop(schema.items.items[paths[1]], paths.slice(2), `${pattern}/${schema.items.items[paths[1]].type}`); } } return loop(schema.items[paths[0]], paths.slice(1), `${pattern}/${schema.items[paths[0]].type}`); } function removeFirstSlash(pattern: string) { return pattern.split('/').slice(1).join('/'); } |