diff --git a/src/editors/data/redux/thunkActions/video.js b/src/editors/data/redux/thunkActions/video.js index 9bbe75b36..96539ae0d 100644 --- a/src/editors/data/redux/thunkActions/video.js +++ b/src/editors/data/redux/thunkActions/video.js @@ -107,10 +107,25 @@ export const parseTranscripts = ({ transcriptsData }) => { if (!transcriptsData) { return []; } - const startString = 'language.", "value": '; const cleanedStr = transcriptsData.replace(/"/g, '"'); - const metadataStr = cleanedStr.substring(cleanedStr.indexOf(startString) + startString.length, cleanedStr.indexOf(', "type": "VideoTranslations"')); - return Object.keys(JSON.parse(metadataStr)); + const startString = '"transcripts": '; + const endString = ', "youtube_id_0_75": '; + const transcriptsJson = cleanedStr.substring( + cleanedStr.indexOf(startString) + startString.length, + cleanedStr.indexOf(endString), + ); + // const transcriptsObj = JSON.parse(transcriptsJson); + try { + const transcriptsObj = JSON.parse(transcriptsJson); + return Object.keys(transcriptsObj.value); + } catch (error) { + if (error instanceof SyntaxError) { + console.error('Invalid JSON:', error.message); + } else { + throw error; + } + return []; + } }; // partially copied from frontend-app-learning/src/courseware/course/course-license/CourseLicense.jsx diff --git a/src/editors/data/redux/thunkActions/video.test.js b/src/editors/data/redux/thunkActions/video.test.js index 7f98921e2..4806a0a4d 100644 --- a/src/editors/data/redux/thunkActions/video.test.js +++ b/src/editors/data/redux/thunkActions/video.test.js @@ -247,6 +247,26 @@ describe('video thunkActions', () => { }); }); }); + describe('parseTranscripts', () => { + const testStudioViewDataWithTranscripts = 'de descarga debajo del video.", "value": "", "type": "Generic", "options": []}, "transcripts": {"explicitly_set": false, "default_value": {}, "field_name": "transcripts", "display_name": "Idiomas de transcripci\\u00f3n", "help": "A\\u00f1ada transcripciones en diferentes idiomas. Haga clic a continuaci\\u00f3n para especificar un idioma y subir un archivo .srt de transcripci\\u00f3n para dicho idioma.", "value": {"aa": "non_existent_dummy_file_name", "ab": "non_existent_dummy_file_name", "ba": "non_existent_dummy_file_name", "en": "external video-en.txt"}, "type": "VideoTranslations", "options": [], "custom": true, "languages": [{"label": "Abkhazian", "code": "ab"}], "urlRoot": "/xblock/block-v1:GalileoX+XS_Mate001+3T2022+type@video+block@20bc09f5522d430f8e43c2bc377b348c/handler/studio_transcript/translation"}, "youtube_id_0_75": {'; + const testStudioViewData = 'de descarga debajo del video.", "value": "", "type": "Generic", "options": []}, "transcripts": {"explicitly_set": false, "default_value": {}, "field_name": "transcripts", "display_name": "Idiomas de transcripci\\u00f3n", "help": "A\\u00f1ada transcripciones en diferentes idiomas. Haga clic a continuaci\\u00f3n para especificar un idioma y subir un archivo .srt de transcripci\\u00f3n para dicho idioma.", "value": {}, "type": "VideoTranslations", "options": [], "custom": true, "languages": [{"label": "Abkhazian", "code": "ab"}], "urlRoot": "/xblock/block-v1:GalileoX+XS_Mate001+3T2022+type@video+block@20bc09f5522d430f8e43c2bc377b348c/handler/studio_transcript/translation"}, "youtube_id_0_75": {'; + const testBadStudioViewData = 'tHiSiSaBAdDaTa'; + it('returns an array of languages given a JSON string', () => { + expect(thunkActions.parseTranscripts({ + transcriptsData: testStudioViewDataWithTranscripts, + })).toEqual(['aa', 'ab', 'ba', 'en']); + }); + it('returns an empty array when there are no transcripts', () => { + expect(thunkActions.parseTranscripts({ + transcriptsData: testStudioViewData, + })).toEqual([]); + }); + it('returns an empty array given an unparsable JSON string', () => { + expect(thunkActions.parseTranscripts({ + transcriptsData: testBadStudioViewData, + })).toEqual([]); + }); + }); describe('parseLicense', () => { const emptyLicenseData = null; const noLicense = 'sOMeHTml data-metadata "license" "value"= null, "type"';