/
SourceContentUpdatesActions.js
195 lines (178 loc) · 7.39 KB
/
SourceContentUpdatesActions.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* eslint-disable require-await */
/* eslint-disable require-await */
import path from 'path-extra';
import ospath from 'ospath';
import sourceContentUpdater from 'tc-source-content-updater';
import {
getTranslate, getContext, getSelectedToolName, getProjectSaveLocation, getProjectBookId,
} from '../selectors';
// helpers
import { getLocalResourceList } from '../helpers/sourceContentUpdatesHelpers';
import { copyGroupDataToProject, updateSourceContentUpdaterManifest } from '../helpers/ResourcesHelpers';
import { getOrigLangforBook } from '../helpers/bibleHelpers';
import * as Bible from '../common/BooksOfTheBible';
import { loadBookTranslations } from './ResourcesActions';
import {
openAlertDialog, closeAlertDialog, openOptionDialog,
} from './AlertModalActions';
import consts from './ActionTypes';
// constants
const SourceContentUpdater = new sourceContentUpdater();
const USER_RESOURCES_PATH = path.join(ospath.home(), 'translationCore/resources');
/**
* Resets the state of the source content updates reducer.
*/
export const resetSourceContentUpdatesReducer = () => ({ type: consts.RESET_LIST_OF_SOURCE_CONTENT_TO_UPDATE });
export const updateSourceContentUpdatesReducer = () => ({ type: consts.SOURCE_CONTENT_UPDATED });
const failedAlertAndRetry = (closeSourceContentDialog, retryCallback, failAlertMessage) => ((dispatch, getState) => {
const translate = getTranslate(getState());
dispatch(
openOptionDialog(
translate(failAlertMessage),
() => dispatch(retryCallback()),
translate('buttons.retry'),
translate('buttons.cancel_button'),
null,
() => {
dispatch(closeAlertDialog());
closeSourceContentDialog();
}
),
);
});
/**
* Gets the list of source content that needs or can be updated.
* @param {function} closeSourceContentDialog - Hacky workaround to close the
* source content dialog in the AppMenu state.
*/
export const getListOfSourceContentToUpdate = async (closeSourceContentDialog) => (async (dispatch, getState) => {
const translate = getTranslate(getState());
dispatch(resetSourceContentUpdatesReducer());
if (navigator.onLine) {
dispatch(openAlertDialog(translate('updates.checking_for_source_content_updates'), true));
const localResourceList = getLocalResourceList();
await SourceContentUpdater.getLatestResources(localResourceList)
.then(resources => {
dispatch(closeAlertDialog());
if (resources.length > 0) {
dispatch({
type: consts.NEW_LIST_OF_SOURCE_CONTENT_TO_UPDATE,
resources,
});
} else {
closeSourceContentDialog();
dispatch(openAlertDialog(translate('updates.source_content_up_to_date')));
}
})
.catch((err) => {
console.error(err, 'Local Resource List:', localResourceList);
dispatch(
failedAlertAndRetry(
closeSourceContentDialog,
() => getListOfSourceContentToUpdate(closeSourceContentDialog),
'updates.failed_checking_for_source_content_updates'
)
);
});
} else {
dispatch(openAlertDialog(translate('no_internet')));
closeSourceContentDialog();
}
});
/**
* Downloads source content updates using the tc-source-content-updater.
* @param {array} languageIdListToDownload - list of language Ids selected to be downloaded.
*/
export const downloadSourceContentUpdates = (languageIdListToDownload) => (async (dispatch, getState) => {
const translate = getTranslate(getState());
const contextId = getContext(getState());
const toolName = getSelectedToolName(getState());
dispatch(resetSourceContentUpdatesReducer());
if (navigator.onLine) {
dispatch(openAlertDialog(translate('updates.downloading_source_content_updates'), true));
await SourceContentUpdater.downloadResources(languageIdListToDownload, USER_RESOURCES_PATH)
.then(async () => {
updateSourceContentUpdaterManifest();
dispatch(updateSourceContentUpdatesReducer());
// if tool is opened then load new bible resources
if (toolName) {
const projectSaveLocation = getProjectSaveLocation(getState());
const bookId = getProjectBookId(getState());
const olForBook = getOrigLangforBook(bookId);
let helpDir = (olForBook && olForBook.languageId) || Bible.NT_ORIG_LANG;
await dispatch(loadBookTranslations(contextId.reference.bookId));
// Tool is opened so we need to update existing group data
copyGroupDataToProject(helpDir, toolName, projectSaveLocation, dispatch);
}
dispatch(openAlertDialog(translate('updates.source_content_updates_successful_download')));
})
.catch((err) => {
console.error(err);
dispatch(
failedAlertAndRetry(
() => dispatch(closeAlertDialog()),
() => downloadSourceContentUpdates(languageIdListToDownload),
'updates.source_content_updates_unsuccessful_download'
)
);
});
} else {
dispatch(openAlertDialog(translate('no_internet')));
}
});
/**
* Downloads a given resource from the door43 catalog via
* the tc-source-content-updater.
* @param {object} resourceDetails - Details about the resource.
* { languageId: 'en', resourceId: 'ult', version: 0.8 }
* @param {string} resourceDetails.languageId The language Id of the resource.
* @param {string} resourceDetails.resourceId The resource Id of the resource.
* @param {number} resourceDetails.version The version of the resource.
*/
export const downloadMissingResource = (resourceDetails) => (async (dispatch, getState) => {
const translate = getTranslate(getState());
if (navigator.onLine) {
dispatch(openAlertDialog(translate('updates.downloading_source_content_updates'), true));
await SourceContentUpdater.downloadAndProcessResource(resourceDetails, USER_RESOURCES_PATH)
.then(async () => {
updateSourceContentUpdaterManifest();
dispatch(updateSourceContentUpdatesReducer());
const successMessage = translate('updates.source_content_updates_successful_download');
dispatch(openAlertDialog(successMessage));
})
.catch((err) => {
console.error(err);
dispatch(
failedAlertAndRetry(
() => dispatch(closeAlertDialog()),
() => downloadMissingResource(resourceDetails),
'updates.source_content_updates_unsuccessful_download'
)
);
});
} else {
dispatch(openAlertDialog(translate('no_internet')));
}
});
/**
* Prompts the user about a missing resource needed by the tool.
* @param {object} resourceDetails - Details about the resource.
* { languageId: 'en', resourceId: 'ult', version: 0.8 }
* @param {string} resourceDetails.languageId The language Id of the resource.
* @param {string} resourceDetails.resourceId The resource Id of the resource.
* @param {number} resourceDetails.version The version of the resource.
*/
export const promptUserAboutMissingResource = (resourceDetails) => (async (dispatch, getState) => {
const translate = getTranslate(getState());
const { resourceId } = resourceDetails;
dispatch(
openOptionDialog(
translate('tools.resource_missing_for_tool', { resourceId: resourceId.toUpperCase() }),
() => dispatch(downloadMissingResource(resourceDetails)),
translate('updates.download'),
translate('buttons.cancel_button'),
null,
() => dispatch(closeAlertDialog()),
),
);
});