diff --git a/onboarding/src/Components/FormsEdit/FormsEditPage.js b/onboarding/src/Components/FormsEdit/FormsEditPage.js index ae4e7507..0b027e61 100644 --- a/onboarding/src/Components/FormsEdit/FormsEditPage.js +++ b/onboarding/src/Components/FormsEdit/FormsEditPage.js @@ -10,6 +10,7 @@ import FormsEdit, { fetchFormData } from "../hooks/FormsEdit"; import { singleCombo } from "../hooks/Packages"; import { onDragEnd } from "../utils"; import ModalWarning from "../ModalWarning"; +import SaveInfo from "../SaveInfo"; import "../../static/css/FormsEdit.scss"; function FormsEditPage() { @@ -19,15 +20,16 @@ function FormsEditPage() { const [sections, setSections] = useState([]); const [loading, setLoading] = useState(true); const [errorMessage, setErrorMessage] = useState(""); - const [update, setUpdate] = useState(true); - // const [saved, setSaved] = useState(false); + const [isAutosave, setIsAutosave] = useState(false); + const [saveOnDemand, setSaveOnDemand] = useState(false); const [packageTitle, setPackageTitle] = useState(""); const [showSaveModal, setShowSaveModal] = useState(false); const [editMode, setEditMode] = useState(true); + const [sectionsCopy, setSectionsCopy] = useState([]); const { data:formData } = fetchFormData(formId); - const { sections:sortedSections, loading:isLoading, errorMessage:error } = FormsEdit(formId, update); - + const { sections:sortedSections, loading:isLoading, errorMessage:error } = FormsEdit(formId); + // Set title of package in navigation bar let title = singleCombo(formData?.package)?.title; if(location.state?.packageTitle && !packageTitle) setPackageTitle(location.state.packageTitle); @@ -39,38 +41,83 @@ function FormsEditPage() { useEffect(() => { setSections(sortedSections); + // Shallow copy an Array of Objects + setSectionsCopy(JSON.parse(JSON.stringify(sortedSections))); updateMaxOrder(sortedSections?.length); setLoading(isLoading); setErrorMessage(error); }, [sortedSections, isLoading, error]); + const showAutosaveInfo = () => { + // Show info "Zapisano zmiany" for 3 sec. when the changes were saved + if(saveOnDemand !== true) { + setIsAutosave(true); + const timer = setTimeout(() => { + setIsAutosave(false); + }, 3000); + + return () => { + clearTimeout(timer); + } + }; + } + useEffect(() => { + // Compare two Arrays of Objects + if(sections && sectionsCopy && JSON.stringify(Object.values(sections)) == JSON.stringify(Object.values(sectionsCopy))) return; + if(sections && !sections.some(section => section.title === "") && saveOnDemand !== true) { + setErrorMessage(""); + + // Save changes after 10 sec. form last change + const saveTimeout = setTimeout( + () => { + if(saveOnDemand !== true) { + FormSectionsAPI.saveAll(sections, updateUnsetAsNew) + .catch((error) => setErrorMessage(error.message)) + .then((result) => { + // Shallow copy an Array of Objects + setSectionsCopy(JSON.parse(JSON.stringify(result))); + setSections(result); + }) + .finally(() => { + showAutosaveInfo(); + }); + } + }, + 10000 + ); + return () => clearTimeout(saveTimeout); + } + }, [sections]); + const hideModal = () => { setShowSaveModal(false); + setSaveOnDemand(false); }; - + const updateUnsetAsNew = function(newSections){ if(typeof newSections === 'undefined' || newSections === null) - return; - + return; + let newSections2 = newSections.map( (section) => { if(section.hasOwnProperty('isNew') ) - section.isNew = false; - + section.isNew = false; + return section; }); - //setSections(newSections2); + setSectionsCopy(JSON.parse(JSON.stringify(newSections))); setSections( newSections2.sort((section1, section2) => section1.order - section2.order) ); } const handleSave = (e) => { e.preventDefault(); + setIsAutosave(false); + setSaveOnDemand(true); FormSectionsAPI.saveAll(sections, updateUnsetAsNew) .catch((error) => setErrorMessage(error.message)) - .then(() => { - setUpdate(true); + .then((result) => { setShowSaveModal(true); - // updateUnsetAsNew(result); + updateUnsetAsNew(result); }); }; @@ -128,7 +175,10 @@ function FormsEditPage() { - {showSaveModal && ( + {isAutosave && ( + + )} + {showSaveModal && saveOnDemand && ( )} - {/* {saved ? ( -
-
- Zapisano zmiany -
-
- ) : ( - <> - )} */} ); } diff --git a/onboarding/src/Components/hooks/FormsEdit.js b/onboarding/src/Components/hooks/FormsEdit.js index e3a37397..675d70d8 100644 --- a/onboarding/src/Components/hooks/FormsEdit.js +++ b/onboarding/src/Components/hooks/FormsEdit.js @@ -6,8 +6,8 @@ import useFetch from "./useFetch.js"; /** * Get page sections; */ -function FormsEdit(formId, update){ - const [sections, setSections] = useState(null); +function FormsEdit(formId){ + const [sections, setSections] = useState([]); const [loading, setLoading] = useState(true); const [errorMessage, setErrorMessage] = useState(""); @@ -32,7 +32,7 @@ function FormsEdit(formId, update){ .finally(() => setLoading(false)); return () => abortCont.abort(); - }, [update]); + }, []); return { sections, loading, errorMessage }; }