|
1 | 1 | import { addTemplateHistory } from "actions/history" |
2 | 2 | import { clearErrors, addError } from "actions/errors" |
| 3 | +import { showModal } from "actions/modals" |
| 4 | +import { |
| 5 | + setPendingResourceTemplateSelection, |
| 6 | + clearPendingResourceTemplateSelection, |
| 7 | +} from "actions/resources" |
3 | 8 | import { |
4 | 9 | addResourceFromDataset, |
5 | 10 | addEmptyResource, |
@@ -45,19 +50,39 @@ import { setCurrentComponent } from "actions/index" |
45 | 50 | import { loadRelationships } from "./relationships" |
46 | 51 | import { useKeycloak } from "../KeycloakContext" |
47 | 52 |
|
48 | | - |
49 | 53 | /** |
50 | 54 | * A thunk that loads an existing resource from Sinopia API and adds to state. |
51 | 55 | * @return {[resource, unusedDataset]} if successful |
52 | 56 | */ |
53 | 57 | export const loadResource = |
54 | | - (uri, errorKey, { asNewResource = false, version = null } = {}) => |
| 58 | + ( |
| 59 | + uri, |
| 60 | + errorKey, |
| 61 | + { asNewResource = false, version = null, keycloak = null } = {} |
| 62 | + ) => |
55 | 63 | (dispatch) => { |
56 | 64 | dispatch(clearErrors(errorKey)) |
57 | 65 | return fetchResource(uri, { version }) |
58 | 66 | .then(([dataset, response]) => { |
59 | 67 | if (!dataset) return false |
60 | 68 | const resourceTemplateId = resourceTemplateIdFromDataset(uri, dataset) |
| 69 | + |
| 70 | + // If no resource template ID, store pending data and show modal |
| 71 | + if (!resourceTemplateId) { |
| 72 | + dispatch( |
| 73 | + setPendingResourceTemplateSelection( |
| 74 | + uri, |
| 75 | + dataset, |
| 76 | + response, |
| 77 | + asNewResource, |
| 78 | + errorKey, |
| 79 | + keycloak |
| 80 | + ) |
| 81 | + ) |
| 82 | + dispatch(showModal("ResourceTemplateChoiceModal")) |
| 83 | + return false |
| 84 | + } |
| 85 | + |
61 | 86 | return dispatch( |
62 | 87 | addResourceFromDataset( |
63 | 88 | dataset, |
@@ -105,8 +130,11 @@ export const loadResource = |
105 | 130 | export const loadResourceForEditor = |
106 | 131 | (uri, errorKey, { asNewResource = false } = {}, keycloak) => |
107 | 132 | (dispatch) => |
108 | | - dispatch(loadResource(uri, errorKey, { asNewResource })).then((result) => |
109 | | - dispatch(dispatchResourceForEditor(result, uri, { asNewResource }, keycloak)) |
| 133 | + dispatch(loadResource(uri, errorKey, { asNewResource, keycloak })).then( |
| 134 | + (result) => |
| 135 | + dispatch( |
| 136 | + dispatchResourceForEditor(result, uri, { asNewResource }, keycloak) |
| 137 | + ) |
110 | 138 | ) |
111 | 139 |
|
112 | 140 | export const dispatchResourceForEditor = |
@@ -138,6 +166,60 @@ export const dispatchResourceForEditor = |
138 | 166 | return true |
139 | 167 | } |
140 | 168 |
|
| 169 | +/** |
| 170 | + * A thunk that completes loading a resource after a template has been selected. |
| 171 | + * This is used when a resource doesn't have a template ID and the user selects one via modal. |
| 172 | + */ |
| 173 | +export const completeResourceLoadingWithTemplate = |
| 174 | + (resourceTemplateId) => (dispatch, getState) => { |
| 175 | + const pending = getState().editor.pendingResourceTemplateSelection |
| 176 | + if (!pending) { |
| 177 | + console.error("No pending resource template selection found") |
| 178 | + return Promise.resolve(false) |
| 179 | + } |
| 180 | + |
| 181 | + const { uri, dataset, response, asNewResource, errorKey, keycloak } = |
| 182 | + pending |
| 183 | + |
| 184 | + // Clear pending state |
| 185 | + dispatch(clearPendingResourceTemplateSelection()) |
| 186 | + |
| 187 | + // Load the resource with the selected template |
| 188 | + return dispatch( |
| 189 | + addResourceFromDataset( |
| 190 | + dataset, |
| 191 | + uri, |
| 192 | + resourceTemplateId, |
| 193 | + errorKey, |
| 194 | + asNewResource, |
| 195 | + _.pick(response, ["group", "editGroups"]) |
| 196 | + ) |
| 197 | + ) |
| 198 | + .then(([resource, usedDataset]) => { |
| 199 | + const unusedDataset = dataset.difference(usedDataset) |
| 200 | + dispatch( |
| 201 | + setUnusedRDF( |
| 202 | + resource.key, |
| 203 | + unusedDataset.size > 0 ? unusedDataset.toCanonical() : null |
| 204 | + ) |
| 205 | + ) |
| 206 | + dispatch(loadRelationships(resource.key, uri, errorKey)) |
| 207 | + const result = [response, resource, unusedDataset] |
| 208 | + return dispatch( |
| 209 | + dispatchResourceForEditor(result, uri, { asNewResource }, keycloak) |
| 210 | + ) |
| 211 | + }) |
| 212 | + .catch((err) => { |
| 213 | + if (err.name !== "ResourceTemplateError") { |
| 214 | + console.error(err) |
| 215 | + dispatch( |
| 216 | + addError(errorKey, `Error retrieving ${uri}: ${err.message || err}`) |
| 217 | + ) |
| 218 | + } |
| 219 | + return false |
| 220 | + }) |
| 221 | + } |
| 222 | + |
141 | 223 | export const loadResourceForPreview = |
142 | 224 | (uri, errorKey, { version = null } = {}) => |
143 | 225 | (dispatch) => |
|
0 commit comments