Passed
Push — master ( 48350e...a50f9c )
by Tom
02:35
created

src/components/ButtonMenu/index.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 130
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 100
mnd 2
bc 2
fnc 0
dl 0
loc 130
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React from 'react'
2
import PropTypes from 'prop-types'
3
import { withTranslation } from 'react-i18next'
4
import { connect } from 'react-redux'
5
import settings from 'Settings'
6
import { downloadStringAsFile, downloadBlobAsFile } from '../../utils/download'
7
import { changeLangAsync, importJson } from '../../actions'
8
import Button from './Button'
9
// import SwitchBar from './SwitchBar'
10
import buttonStyle from './index.scss'
11
import UploadButton from './UploadButton'
12
13
const pdfUrls = {}
14
15
Promise.all(
16
  settings.langs.map((lang) => import(`../../../pdf/${lang.code}.pdf`)),
17
).then((importedLangPdfs) => {
18
  settings.langs.forEach((lang, index) => {
19
    pdfUrls[lang.code] = importedLangPdfs[index].default
20
  })
21
})
22
23
const ButtonMenu = ({ currentLang, userData, projectData, dispatch, t }) => {
24
  const print = () => {
25
    window.print()
26
  }
27
28
  const handleGotoGithub = () => {
29
    window.open('https://github.com/tomchen/react-printable-cv')
30
  }
31
32
  const handleLangChange = (lang) => {
33
    dispatch(changeLangAsync(lang))
34
  }
35
36
  const handleExportJson = () => {
37
    downloadStringAsFile(
38
      'data.json',
39
      JSON.stringify({ cv: userData, project_list: projectData }, null, 2),
40
    )
41
  }
42
43
  const handleImportJson = (e) => {
44
    const reader = new FileReader()
45
    reader.onload = (evt) => {
46
      dispatch(importJson(JSON.parse(evt.target.result)))
47
    }
48
    reader.readAsText(e.target.files[0])
49
  }
50
51
  const pdf = () => {
52
    const currentPdfUrl = pdfUrls[currentLang]
53
    if (/^data:.+/.test(currentPdfUrl)) {
54
      // is base64
55
      fetch(currentPdfUrl)
56
        .then((res) => res.blob())
57
        .then((blob) => {
58
          downloadBlobAsFile(`cv.${currentLang}.pdf`, blob)
59
        })
60
    } else {
61
      window.open(currentPdfUrl)
62
    }
63
  }
64
65
  // const handleEditModeChange = (switchOn) => {
66
67
  // }
68
69
  return (
70
    <div className={buttonStyle.menu}>
71
      {settings.langs.map(({ code, name }) => (
72
        <Button
73
          key={code}
74
          disabled={currentLang === code}
75
          text={name}
76
          title={currentLang !== code ? t('Change language') : undefined}
77
          onClick={() => {
78
            handleLangChange(code)
79
          }}
80
        />
81
      ))}
82
      <Button
83
        text={t('PDF')}
84
        title={t('Download PDF file for the current language')}
85
        onClick={pdf}
86
        color="secondary"
87
      />
88
      <Button
89
        text={t('Print (🧪)')}
90
        title={t(
91
          'Experimental. Almost perfect in Chrome. Firefox user should set print scale to 100% instead of "Shrink To Fit". Other browsers may have problems. Instead, you may click "PDF" then print the PDF file.',
92
        )}
93
        onClick={print}
94
        color="default"
95
      />
96
      {/* <SwitchBar text={t('Edit mode')} onChange={handleEditModeChange} /> */}
97
      {settings.export_json_button && <Button
98
        text={t('Export JSON')}
99
        title={t('Export .json file of the data')}
100
        onClick={handleExportJson}
101
        color="default"
102
      />}
103
      {settings.import_json_button && <UploadButton
104
        text={t('Import JSON')}
105
        title={t('Import .json file and replace the data')}
106
        onChange={handleImportJson}
107
        color="default"
108
        component="span"
109
      />}
110
      {settings.github_button && <Button text={t('GitHub')} onClick={handleGotoGithub} color="default" />}
111
    </div>
112
  )
113
}
114
115
ButtonMenu.propTypes = {
116
  currentLang: PropTypes.string,
117
  userData: PropTypes.objectOf(PropTypes.any).isRequired,
118
  projectData: PropTypes.objectOf(PropTypes.any).isRequired,
119
  dispatch: PropTypes.func.isRequired,
120
  t: PropTypes.func.isRequired,
121
}
122
123
const mapStateToProps = (state) => ({
124
  currentLang: state.lang,
125
  userData: state.userData,
126
  projectData: state.projectData,
127
})
128
129
export default connect(mapStateToProps)(withTranslation()(ButtonMenu))
130