import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import { CButton, CModal, CModalBody, CModalHeader, CModalTitle, CPopover, CNav, CNavLink, CTabPane, CTabContent, } from '@coreui/react'; import CIcon from '@coreui/icons-react'; import { cilPencil, cilSave, cilX } from '@coreui/icons'; import axiosInstance from 'utils/axiosInstance'; import { useFormFields, useAuth, useToast, DetailedNotesTable } from 'ucentral-libs'; import Form from './Form'; const initialState = { created: { value: '', error: false, editable: false, }, release: { value: false, error: false, editable: false, }, image: { value: '', error: false, editable: true, }, imageDate: { value: '', error: false, editable: true, }, size: { value: '', error: false, editable: true, }, revision: { value: '', error: false, editable: false, }, uri: { value: '', error: false, editable: true, }, description: { value: '', error: false, editable: true, }, notes: { value: [], editable: false, }, }; const EditFirmwareModal = ({ show, toggle, firmwareId, refreshTable }) => { const { t } = useTranslation(); const { currentToken, endpoints } = useAuth(); const { addToast } = useToast(); const [loading, setLoading] = useState(false); const [editing, setEditing] = useState(false); const [index, setIndex] = useState(0); const [firmware, updateWithId, updateWithKey, setFirmware] = useFormFields(initialState); const getFirmware = () => { const options = { headers: { Accept: 'application/json', Authorization: `Bearer ${currentToken}`, }, }; axiosInstance .get(`${endpoints.owfms}/api/v1/firmware/${firmwareId}`, options) .then((response) => { const newFirmware = {}; for (const key of Object.keys(response.data)) { if (key in initialState && key !== 'currentPassword') { newFirmware[key] = { ...initialState[key], value: response.data[key], }; } } setFirmware({ ...initialState, ...newFirmware }); }) .catch(() => {}); }; const toggleEditing = () => { if (editing) { getFirmware(); } setEditing(!editing); }; const toggleModal = () => { toggleEditing(); toggle(); }; const updateFirmware = () => { setLoading(true); const parameters = { id: firmwareId, description: firmware.description.value, }; const newNotes = []; for (let i = 0; i < firmware.notes.value.length; i += 1) { if (firmware.notes.value[i].new) newNotes.push({ note: firmware.notes.value[i].note }); } parameters.notes = newNotes; const options = { headers: { Accept: 'application/json', Authorization: `Bearer ${currentToken}`, }, }; axiosInstance .put(`${endpoints.owfms}/api/v1/firmware/${firmwareId}`, parameters, options) .then(() => { addToast({ title: t('firmware.update_success_title'), body: t('firmware.update_success'), color: 'success', autohide: true, }); refreshTable(); toggle(); }) .catch((e) => { addToast({ title: t('firmware.update_failure_title'), body: t('firmware.update_failure', { error: e.response?.data?.ErrorDescription }), color: 'danger', autohide: true, }); getFirmware(); }) .finally(() => { setLoading(false); }); }; const addNote = (currentNote) => { const newNotes = [...firmware.notes.value]; newNotes.unshift({ note: currentNote, new: true, created: new Date().getTime() / 1000, createdBy: '', }); updateWithKey('notes', { value: newNotes }); }; useEffect(() => { if (show) { getFirmware(); setEditing(false); setIndex(0); } }, [show]); return ( {t('firmware.details_title', { image: firmware.image.value })}
setIndex(0)} > {t('common.main')} setIndex(1)} > {t('configuration.notes')} {index === 0 ? (
) : null} {index === 1 ? ( ) : null} ); }; EditFirmwareModal.propTypes = { firmwareId: PropTypes.string.isRequired, show: PropTypes.bool.isRequired, toggle: PropTypes.func.isRequired, refreshTable: PropTypes.func.isRequired, }; export default React.memo(EditFirmwareModal);