From dd8436c17192394647432efdd7cbbf36e57ad774 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Thu, 5 Jul 2018 18:04:12 +0200 Subject: [PATCH] fix release note modal to fetch only when opened and not on mount --- src/components/modals/ReleaseNotes.js | 127 ------------------ .../modals/ReleaseNotes/ReleaseNotesBody.js | 114 ++++++++++++++++ src/components/modals/ReleaseNotes/index.js | 16 +++ 3 files changed, 130 insertions(+), 127 deletions(-) delete mode 100644 src/components/modals/ReleaseNotes.js create mode 100644 src/components/modals/ReleaseNotes/ReleaseNotesBody.js create mode 100644 src/components/modals/ReleaseNotes/index.js diff --git a/src/components/modals/ReleaseNotes.js b/src/components/modals/ReleaseNotes.js deleted file mode 100644 index 60b00923..00000000 --- a/src/components/modals/ReleaseNotes.js +++ /dev/null @@ -1,127 +0,0 @@ -// @flow -import React, { PureComponent } from 'react' -import { translate } from 'react-i18next' -import styled from 'styled-components' -import network from 'api/network' - -import { MODAL_RELEASES_NOTES } from 'config/constants' -import Modal, { ModalBody, ModalTitle, ModalContent, ModalFooter } from 'components/base/Modal' - -import Button from 'components/base/Button' -import Box from 'components/base/Box' -import GrowScroll from 'components/base/GrowScroll' -import Text from 'components/base/Text' -import Spinner from 'components/base/Spinner' -import GradientBox from 'components/GradientBox' -import TrackPage from 'analytics/TrackPage' -import Markdow, { Notes } from 'components/base/Markdown' - -import type { T } from 'types/common' - -type Props = { - t: T, -} - -type State = { - markdown: ?string, -} - -const Title = styled(Text).attrs({ - ff: 'Museo Sans', - fontSize: 5, - color: 'dark', -})`` - -class ReleaseNotes extends PureComponent { - state = { - markdown: null, - } - - loading = false - - fetchNotes = version => { - if (!this.loading) { - this.loading = true - - network({ - method: 'GET', - url: `https://api.github.com/repos/LedgerHQ/ledger-live-desktop/releases/tags/v${version}`, - }) - .then(response => { - const { body } = response.data - - this.setState( - { - markdown: body, - }, - () => { - this.loading = false - }, - ) - }) - .catch(() => { - this.setState( - { - markdown: this.props.t('app:common.error.load'), - }, - () => { - this.loading = false - }, - ) - }) - } - } - - render() { - const { t } = this.props - const renderBody = ({ data, onClose }) => { - const version = data - const { markdown } = this.state - let content - - if (markdown === null) { - this.fetchNotes(version) - - content = ( - - - - ) - } else { - content = ( - - {t('app:releaseNotes.version', { versionNb: version })} - {markdown} - - ) - } - - return ( - - - {t('app:releaseNotes.title')} - - - {content} - - - - - - - - ) - } - - return - } -} - -export default translate()(ReleaseNotes) diff --git a/src/components/modals/ReleaseNotes/ReleaseNotesBody.js b/src/components/modals/ReleaseNotes/ReleaseNotesBody.js new file mode 100644 index 00000000..bcf01258 --- /dev/null +++ b/src/components/modals/ReleaseNotes/ReleaseNotesBody.js @@ -0,0 +1,114 @@ +// @flow +import React, { PureComponent } from 'react' +import { translate } from 'react-i18next' +import styled from 'styled-components' + +import network from 'api/network' + +import Button from 'components/base/Button' +import Box from 'components/base/Box' +import GrowScroll from 'components/base/GrowScroll' +import Text from 'components/base/Text' +import Spinner from 'components/base/Spinner' +import GradientBox from 'components/GradientBox' +import TrackPage from 'analytics/TrackPage' +import Markdow, { Notes } from 'components/base/Markdown' +import { ModalBody, ModalTitle, ModalContent, ModalFooter } from 'components/base/Modal' + +import type { T } from 'types/common' + +type Props = { + version: string, + onClose: () => void, + t: T, +} + +type State = { + markdown: ?string, +} + +const Title = styled(Text).attrs({ + ff: 'Museo Sans', + fontSize: 5, + color: 'dark', +})`` + +class ReleaseNotesBody extends PureComponent { + state = { + markdown: null, + } + + componentDidMount() { + const { version } = this.props + this.fetchNotes(version) + } + + fetchNotes = async (version: string) => { + try { + const { + data: { body }, + } = await network({ + method: 'GET', + url: `https://api.github.com/repos/LedgerHQ/ledger-live-desktop/releases/tags/v${version}`, + }) + if (body) { + this.setState({ markdown: body }) + } else { + this.setState({ markdown: this.props.t('app:common.error.load') }) + } + } catch (error) { + this.setState({ markdown: this.props.t('app:common.error.load') }) + } + } + + renderContent = () => { + const { markdown } = this.state + const { t } = this.props + + const { version } = this.props + + if (markdown) { + return ( + + {t('app:releaseNotes.version', { versionNb: version })} + {markdown} + + ) + } + + return ( + + + + ) + } + + render() { + const { onClose, t } = this.props + + return ( + + + {t('app:releaseNotes.title')} + + + {this.renderContent()} + + + + + + + + ) + } +} + +export default translate()(ReleaseNotesBody) diff --git a/src/components/modals/ReleaseNotes/index.js b/src/components/modals/ReleaseNotes/index.js new file mode 100644 index 00000000..cf4a4b35 --- /dev/null +++ b/src/components/modals/ReleaseNotes/index.js @@ -0,0 +1,16 @@ +// @flow +import React from 'react' + +import { MODAL_RELEASES_NOTES } from 'config/constants' +import Modal from 'components/base/Modal' + +import ReleaseNotesBody from './ReleaseNotesBody' + +const ReleaseNotesModal = () => ( + } + /> +) + +export default ReleaseNotesModal