From 04a667eb6f476318ff5761d4f4bd9b886b781205 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Wed, 27 Nov 2019 16:54:37 +0100 Subject: [PATCH] Extract reboot button from reForis. * Add RebootButton tests. * RebootButton code review. * Update translations. --- src/common/RebootButton.js | 77 +++++++++++++++ src/common/__tests__/RebootButton.test.js | 55 +++++++++++ .../__snapshots__/RebootButton.test.js.snap | 94 +++++++++++++++++++ src/forisUrls.js | 4 + src/index.js | 3 + translations/cs/LC_MESSAGES/forisjs.po | 46 ++++++++- translations/da/LC_MESSAGES/forisjs.po | 38 +++++++- translations/de/LC_MESSAGES/forisjs.po | 38 +++++++- translations/el/LC_MESSAGES/forisjs.po | 38 +++++++- translations/en/LC_MESSAGES/forisjs.po | 38 +++++++- translations/es/LC_MESSAGES/forisjs.po | 44 ++++++++- translations/fi/LC_MESSAGES/forisjs.po | 38 +++++++- translations/fo/LC_MESSAGES/forisjs.po | 38 +++++++- translations/forisjs.pot | 26 ++++- translations/fr/LC_MESSAGES/forisjs.po | 38 +++++++- translations/hr/LC_MESSAGES/forisjs.po | 38 +++++++- translations/hu/LC_MESSAGES/forisjs.po | 38 +++++++- translations/it/LC_MESSAGES/forisjs.po | 38 +++++++- translations/ja/LC_MESSAGES/forisjs.po | 38 +++++++- translations/ko/LC_MESSAGES/forisjs.po | 38 +++++++- translations/lt/LC_MESSAGES/forisjs.po | 38 +++++++- translations/nb/LC_MESSAGES/forisjs.po | 38 +++++++- translations/nb_NO/LC_MESSAGES/forisjs.po | 38 +++++++- translations/nl/LC_MESSAGES/forisjs.po | 38 +++++++- translations/pl/LC_MESSAGES/forisjs.po | 38 +++++++- translations/ro/LC_MESSAGES/forisjs.po | 38 +++++++- translations/ru/LC_MESSAGES/forisjs.po | 38 +++++++- translations/sk/LC_MESSAGES/forisjs.po | 38 +++++++- translations/sv/LC_MESSAGES/forisjs.po | 38 +++++++- 29 files changed, 1117 insertions(+), 30 deletions(-) create mode 100644 src/common/RebootButton.js create mode 100644 src/common/__tests__/RebootButton.test.js create mode 100644 src/common/__tests__/__snapshots__/RebootButton.test.js.snap diff --git a/src/common/RebootButton.js b/src/common/RebootButton.js new file mode 100644 index 0000000..3b6c256 --- /dev/null +++ b/src/common/RebootButton.js @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * + * This is free software, licensed under the GNU General Public License v3. + * See /LICENSE for more information. + */ + +import React, { useState, useEffect } from "react"; +import PropTypes from "prop-types"; + +import { useAPIPost } from "api/hooks"; +import { API_STATE } from "api/utils"; +import { ForisURLs } from "forisUrls"; + +import { Button } from "bootstrap/Button"; +import { + Modal, ModalHeader, ModalBody, ModalFooter, +} from "bootstrap/Modal"; +import { useAlert } from "alertContext/AlertContext"; + +RebootButton.propTypes = { + forisFormSize: PropTypes.bool, +}; + +export function RebootButton(props) { + const [triggered, setTriggered] = useState(false); + const [modalShown, setModalShown] = useState(false); + const [triggerRebootStatus, triggerReboot] = useAPIPost(ForisURLs.reboot); + + const [setAlert] = useAlert(); + useEffect(() => { + if (triggerRebootStatus.state === API_STATE.ERROR) { + setAlert(_("Reboot request failed.")); + } + }); + + function rebootHandler() { + setTriggered(true); + triggerReboot(); + setModalShown(false); + } + + return ( + <> + + + + ); +} + +RebootModal.propTypes = { + shown: PropTypes.bool.isRequired, + setShown: PropTypes.func.isRequired, + onReboot: PropTypes.func.isRequired, +}; + +function RebootModal({ shown, setShown, onReboot }) { + return ( + + +

{_("Are you sure you want to restart the router?")}

+ + + + +
+ ); +} diff --git a/src/common/__tests__/RebootButton.test.js b/src/common/__tests__/RebootButton.test.js new file mode 100644 index 0000000..3eb205e --- /dev/null +++ b/src/common/__tests__/RebootButton.test.js @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * + * This is free software, licensed under the GNU General Public License v3. + * See /LICENSE for more information. + */ + +import React from "react"; + +import { fireEvent, getByText, queryByText, render, wait } from "customTestRender"; +import mockAxios from "jest-mock-axios"; +import { mockJSONError } from "testUtils/network"; +import { mockSetAlert } from "testUtils/alertContextMock"; + +import { RebootButton } from "../RebootButton"; + +describe("", () => { + let componentContainer; + beforeEach(() => { + const { container } = render(<> +