1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.useMutation = useMutation;
|
5 |
|
6 | var _apolloClient = require("apollo-client");
|
7 |
|
8 | var _react = _interopRequireDefault(require("react"));
|
9 |
|
10 | var _ApolloContext = require("./ApolloContext");
|
11 |
|
12 | var _actHack = _interopRequireDefault(require("./internal/actHack"));
|
13 |
|
14 | var _utils = require("./utils");
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
17 |
|
18 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
19 |
|
20 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
21 |
|
22 | var getInitialState = function getInitialState() {
|
23 | return {
|
24 | called: false,
|
25 | data: undefined,
|
26 | error: undefined,
|
27 | hasError: false,
|
28 | loading: false
|
29 | };
|
30 | };
|
31 |
|
32 | function useMutation(mutation, baseOptions) {
|
33 | if (baseOptions === void 0) {
|
34 | baseOptions = {};
|
35 | }
|
36 |
|
37 | var client = (0, _ApolloContext.useApolloClient)(baseOptions.client);
|
38 |
|
39 | var _React$useState = _react["default"].useState(getInitialState),
|
40 | result = _React$useState[0],
|
41 | setResult = _React$useState[1];
|
42 |
|
43 | var _baseOptions = baseOptions,
|
44 | _baseOptions$rethrow = _baseOptions.rethrow,
|
45 | rethrow = _baseOptions$rethrow === void 0 ? true : _baseOptions$rethrow,
|
46 | options = _objectWithoutPropertiesLoose(_baseOptions, ["rethrow"]);
|
47 |
|
48 | var mergeResult = function mergeResult(partialResult) {
|
49 |
|
50 | (0, _actHack["default"])(function () {
|
51 | setResult(function (prev) {
|
52 | return _extends({}, prev, {}, partialResult);
|
53 | });
|
54 | });
|
55 | };
|
56 |
|
57 |
|
58 | _react["default"].useEffect(function () {
|
59 | mergeResult(getInitialState());
|
60 | }, [client]);
|
61 |
|
62 | var _useMutationTracking = useMutationTracking(),
|
63 | generateNewMutationId = _useMutationTracking.generateNewMutationId,
|
64 | isMostRecentMutation = _useMutationTracking.isMostRecentMutation;
|
65 |
|
66 | var onMutationStart = function onMutationStart() {
|
67 | if (!result.loading) {
|
68 | mergeResult({
|
69 | called: true,
|
70 | data: undefined,
|
71 | error: undefined,
|
72 | hasError: false,
|
73 | loading: true
|
74 | });
|
75 | }
|
76 | };
|
77 |
|
78 | var onMutationError = function onMutationError(error, mutationId) {
|
79 | if (isMostRecentMutation(mutationId)) {
|
80 | mergeResult({
|
81 | error: error,
|
82 | hasError: true,
|
83 | loading: false
|
84 | });
|
85 | }
|
86 | };
|
87 |
|
88 | var onMutationCompleted = function onMutationCompleted(response, mutationId) {
|
89 | var data = response.data,
|
90 | errors = response.errors;
|
91 |
|
92 | if (errors && errors.length > 0) {
|
93 | onMutationError(new _apolloClient.ApolloError({
|
94 | graphQLErrors: errors
|
95 | }), mutationId);
|
96 | return;
|
97 | }
|
98 |
|
99 | if (isMostRecentMutation(mutationId)) {
|
100 | mergeResult({
|
101 | data: data,
|
102 | loading: false
|
103 | });
|
104 | }
|
105 | };
|
106 |
|
107 | var runMutation = _react["default"].useCallback(function (mutateOptions) {
|
108 | if (mutateOptions === void 0) {
|
109 | mutateOptions = {};
|
110 | }
|
111 |
|
112 | return new Promise(function (resolve, reject) {
|
113 | onMutationStart();
|
114 | var mutationId = generateNewMutationId();
|
115 |
|
116 |
|
117 | var mutateVariables = options.variables ? _extends({}, mutateOptions.variables, {}, options.variables) : mutateOptions.variables;
|
118 | client.mutate(_extends({
|
119 | mutation: mutation
|
120 | }, options, {}, mutateOptions, {
|
121 | variables: mutateVariables
|
122 | })).then(function (response) {
|
123 | onMutationCompleted(response, mutationId);
|
124 | resolve(response);
|
125 | })["catch"](function (err) {
|
126 | onMutationError(err, mutationId);
|
127 |
|
128 | if (rethrow) {
|
129 | reject(err);
|
130 | return;
|
131 | }
|
132 |
|
133 | resolve({});
|
134 | });
|
135 | });
|
136 | }, [client, mutation, (0, _utils.objToKey)(baseOptions)]);
|
137 |
|
138 | return [runMutation, result];
|
139 | }
|
140 |
|
141 | function useMutationTracking() {
|
142 | var mostRecentMutationId = _react["default"].useRef(0);
|
143 |
|
144 | var generateNewMutationId = function generateNewMutationId() {
|
145 | mostRecentMutationId.current += 1;
|
146 | return mostRecentMutationId.current;
|
147 | };
|
148 |
|
149 | var isMostRecentMutation = function isMostRecentMutation(mutationId) {
|
150 | return mostRecentMutationId.current === mutationId;
|
151 | };
|
152 |
|
153 | return {
|
154 | generateNewMutationId: generateNewMutationId,
|
155 | isMostRecentMutation: isMostRecentMutation
|
156 | };
|
157 | } |
\ | No newline at end of file |