| 86 | } |
| 87 | |
| 88 | async function performMutation(payload, requestedLocation) { |
| 89 | setIsSaving(true); |
| 90 | try { |
| 91 | const response = await fetch( |
| 92 | `${endpoint}?location=${encodeURIComponent( |
| 93 | JSON.stringify(requestedLocation) |
| 94 | )}`, |
| 95 | { |
| 96 | method, |
| 97 | body: JSON.stringify(payload), |
| 98 | headers: { |
| 99 | 'Content-Type': 'application/json', |
| 100 | }, |
| 101 | } |
| 102 | ); |
| 103 | if (!response.ok) { |
| 104 | throw new Error(await response.text()); |
| 105 | } |
| 106 | refresh(response); |
| 107 | } catch (e) { |
| 108 | setDidError(true); |
| 109 | setError(e); |
| 110 | } finally { |
| 111 | setIsSaving(false); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return [isSaving, performMutation]; |
| 116 | } |