2019-08-28 21:55:56 +05:30
|
|
|
import dedent from 'dedent';
|
2021-08-02 15:39:40 +02:00
|
|
|
import i18next from 'i18next';
|
2018-05-08 00:29:45 +01:00
|
|
|
import { ofType } from 'redux-observable';
|
2021-08-02 15:39:40 +02:00
|
|
|
import { tap, mapTo } from 'rxjs/operators';
|
|
|
|
import envData from '../../../../../config/env.json';
|
2018-05-08 00:29:45 +01:00
|
|
|
import {
|
|
|
|
closeModal,
|
|
|
|
challengeFilesSelector,
|
2021-02-09 05:53:10 +00:00
|
|
|
challengeMetaSelector,
|
|
|
|
projectFormValuesSelector
|
2018-05-08 00:29:45 +01:00
|
|
|
} from '../redux';
|
2021-04-20 22:47:42 +01:00
|
|
|
import { transformEditorLink } from '../utils';
|
2021-08-02 15:39:40 +02:00
|
|
|
import { actionTypes } from './action-types';
|
2021-03-26 00:43:43 +05:30
|
|
|
|
|
|
|
const { forumLocation } = envData;
|
2018-05-08 00:29:45 +01:00
|
|
|
|
|
|
|
function filesToMarkdown(files = {}) {
|
|
|
|
const moreThenOneFile = Object.keys(files).length > 1;
|
|
|
|
return Object.keys(files).reduce((fileString, key) => {
|
|
|
|
const file = files[key];
|
|
|
|
if (!file) {
|
|
|
|
return fileString;
|
|
|
|
}
|
|
|
|
const fileName = moreThenOneFile ? `\\ file: ${file.contents}` : '';
|
|
|
|
const fileType = file.ext;
|
2019-08-28 21:55:56 +05:30
|
|
|
return `${fileString}\`\`\`${fileType}\n${fileName}\n${file.contents}\n\`\`\`\n\n`;
|
2018-05-08 00:29:45 +01:00
|
|
|
}, '\n');
|
|
|
|
}
|
|
|
|
|
2018-09-30 11:37:19 +01:00
|
|
|
function createQuestionEpic(action$, state$, { window }) {
|
2018-05-08 00:29:45 +01:00
|
|
|
return action$.pipe(
|
2021-08-02 15:39:40 +02:00
|
|
|
ofType(actionTypes.createQuestion),
|
2018-05-08 00:29:45 +01:00
|
|
|
tap(() => {
|
2018-09-30 11:37:19 +01:00
|
|
|
const state = state$.value;
|
2018-05-08 00:29:45 +01:00
|
|
|
const files = challengeFilesSelector(state);
|
2021-05-10 08:48:49 -07:00
|
|
|
const { title: challengeTitle, helpCategory } =
|
|
|
|
challengeMetaSelector(state);
|
2018-07-24 18:00:36 +05:30
|
|
|
const {
|
2018-09-30 11:37:19 +01:00
|
|
|
navigator: { userAgent },
|
|
|
|
location: { href }
|
|
|
|
} = window;
|
2021-02-09 05:53:10 +00:00
|
|
|
const projectFormValues = Object.entries(
|
|
|
|
projectFormValuesSelector(state)
|
|
|
|
);
|
2019-08-28 21:55:56 +05:30
|
|
|
const endingText = dedent(
|
2021-05-24 10:59:35 -07:00
|
|
|
`${i18next.t('forum-help.browser-info')}\n\n${i18next.t(
|
|
|
|
'forum-help.user-agent',
|
|
|
|
{ userAgent }
|
|
|
|
)}\n\n${i18next.t(
|
|
|
|
'forum-help.challenge'
|
|
|
|
)} ${challengeTitle}\n\n${i18next.t(
|
|
|
|
'forum-help.challenge-link'
|
|
|
|
)}\n${href}`
|
2019-08-28 21:55:56 +05:30
|
|
|
);
|
|
|
|
|
2021-05-24 10:59:35 -07:00
|
|
|
let textMessage = dedent(`${i18next.t(
|
|
|
|
'forum-help.whats-happening'
|
|
|
|
)}\n${i18next.t('forum-help.describe')}\n\n
|
2021-02-09 05:53:10 +00:00
|
|
|
${
|
|
|
|
projectFormValues.length
|
2021-05-24 10:59:35 -07:00
|
|
|
? `${i18next.t('forum-help.camper-project')}\n`
|
|
|
|
: i18next.t('forum-help.camper-code')
|
2021-02-09 05:53:10 +00:00
|
|
|
}
|
2021-03-11 00:31:46 +05:30
|
|
|
${
|
|
|
|
projectFormValues
|
2021-04-20 22:47:42 +01:00
|
|
|
?.map(([key, val]) => `${key}: ${transformEditorLink(val)}\n`)
|
2021-03-11 00:31:46 +05:30
|
|
|
?.join('') || filesToMarkdown(files)
|
2021-05-24 10:59:35 -07:00
|
|
|
}\n\n
|
|
|
|
${endingText}`);
|
2019-08-28 21:55:56 +05:30
|
|
|
|
|
|
|
const altTextMessage = dedent(
|
2021-05-24 10:59:35 -07:00
|
|
|
`${i18next.t('forum-help.whats-happening')}\n\n\n\n${i18next.t(
|
|
|
|
'forum-help.camper-code'
|
|
|
|
)}\n\n${i18next.t('forum-help.warning')}\n\n${i18next.t(
|
|
|
|
'forum-help.too-long-one'
|
|
|
|
)}\n\n${i18next.t('forum-help.too-long-two')}\n\n${i18next.t(
|
|
|
|
'forum-help.too-long-three'
|
|
|
|
)}\n\n\`\`\`\n${i18next.t('forum-help.add-code-one')}\n${i18next.t(
|
|
|
|
'forum-help.add-code-two'
|
|
|
|
)}\n${i18next.t('forum-help.add-code-three')}\n\n\`\`\`\n${endingText}`
|
2019-08-28 21:55:56 +05:30
|
|
|
);
|
|
|
|
|
2020-10-30 20:10:34 +01:00
|
|
|
const category = window.encodeURIComponent(helpCategory || 'Help');
|
2019-08-28 21:55:56 +05:30
|
|
|
|
|
|
|
const studentCode = window.encodeURIComponent(textMessage);
|
|
|
|
const altStudentCode = window.encodeURIComponent(altTextMessage);
|
|
|
|
|
|
|
|
const baseURI = `${forumLocation}/new-topic?category=${category}&title=&body=`;
|
|
|
|
const defaultURI = `${baseURI}${studentCode}`;
|
|
|
|
const altURI = `${baseURI}${altStudentCode}`;
|
|
|
|
|
|
|
|
window.open(defaultURI.length < 8000 ? defaultURI : altURI, '_blank');
|
2018-05-08 00:29:45 +01:00
|
|
|
}),
|
|
|
|
mapTo(closeModal('help'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default createQuestionEpic;
|