This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 145
/
onDeploy.js
120 lines (110 loc) · 2.78 KB
/
onDeploy.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
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
// @flow
import * as React from 'react';
import RefId from 'canner-ref-id';
import {Map, List} from 'immutable';
type Props = {
refId: RefId,
keyName: string,
routes: Array<string>,
pattern: string,
onDeploy: (key: string, callback: any => any) => string,
removeOnDeploy: (key: string, id: ?string) => void,
rootValue: any,
};
export default function withOndeploy(Com: React.ComponentType<*>) {
return class ComponentWithOnDeploy extends React.Component<Props> {
key: string;
id: ?string;
constructor(props: Props) {
super(props);
const {pattern, refId, rootValue} = props;
const {key, id} = splitRefId({
refId,
rootValue,
pattern
});
this.key = key;
this.id = id;
}
removeOnDeploy = (arg1: string, callbackId: string) => {
const {removeOnDeploy} = this.props;
if (callbackId) {
return removeOnDeploy(arg1, callbackId);
} else {
return removeOnDeploy(this.key, arg1);
}
}
onDeploy = (arg1: string | Function, callback: Function): string => {
const {onDeploy, refId} = this.props;
if (typeof arg1 === 'string') {
return onDeploy(arg1, callback);
} else {
// first arguments is a function
return onDeploy(this.key, result => {
let restPathArr = refId.getPathArr();
// if (this.id) {
// restPathArr = restPathArr.slice(2);
// } else {
restPathArr = restPathArr.slice(1);
// }
const {paths, value} = getValueAndPaths(result.data, restPathArr);
return {
...result,
// $FlowFixMe
data: result.data.setIn(paths, arg1(value))
}
});
}
}
render() {
return <Com {...this.props}
onDeploy={this.onDeploy}
removeOnDeploy={this.removeOnDeploy}
/>
}
};
}
export function splitRefId({
refId,
rootValue,
pattern
}: {
refId: RefId,
rootValue: any,
pattern: string
}) {
const [key, index] = refId.getPathArr();
let id;
if (pattern.startsWith('array')) {
id = rootValue.getIn([key, index, 'id']);
}
return {
key,
id
}
}
export function getValueAndPaths(value: Map<string, *>, idPathArr: Array<string>) {
return idPathArr.reduce((result: any, key: string) => {
let v = result.value;
let paths = result.paths;
if (Map.isMap(v)) {
if (v.has('edges') && v.has('pageInfo')) {
v = v.getIn(['edges', key, 'node']);
paths = paths.concat(['edges', key, 'node']);
} else {
v = v.get(key);
paths = paths.concat(key);
}
} else if (List.isList(v)) {
v = v.get(key);
paths = paths.concat(key);
}
return {
value: v,
paths
}
}, {
value,
paths: []
});
}