diff --git a/site/src/pages/RoadmapPage/ImportZot4PlanPopup.tsx b/site/src/pages/RoadmapPage/ImportZot4PlanPopup.tsx index a4a31933..7c3aec77 100644 --- a/site/src/pages/RoadmapPage/ImportZot4PlanPopup.tsx +++ b/site/src/pages/RoadmapPage/ImportZot4PlanPopup.tsx @@ -2,7 +2,7 @@ import { FC, useContext, useState } from 'react'; import './ImportZot4PlanPopup.scss'; import { CloudArrowDown, ExclamationTriangle } from 'react-bootstrap-icons'; import { Button, Form, Modal } from 'react-bootstrap'; -import { addRoadmapPlan, setPlanIndex, selectAllPlans } from '../../store/slices/roadmapSlice'; +import { setPlanIndex, selectAllPlans, RoadmapPlan, addRoadmapPlan } from '../../store/slices/roadmapSlice'; import { useAppDispatch, useAppSelector } from '../../store/hooks'; import ThemeContext from '../../style/theme-context'; import trpc from '../../trpc.ts'; @@ -10,7 +10,10 @@ import { expandAllPlanners, makeUniquePlanName } from '../../helpers/planner'; import spawnToast from '../../helpers/toastify'; import helpImage from '../../asset/zot4plan-import-help.png'; -const ImportZot4PlanPopup: FC = () => { +interface ImportZot4PlanPopupProps { + saveRoadmap: (planner?: RoadmapPlan[]) => Promise; +} +const ImportZot4PlanPopup: FC = ({ saveRoadmap }) => { const dispatch = useAppDispatch(); const { darkMode } = useContext(ThemeContext); const [showModal, setShowModal] = useState(false); @@ -45,16 +48,14 @@ const ImportZot4PlanPopup: FC = () => { if (problemCount > 0) { spawnToast('Partially imported "' + schedName + '" (removed ' + problemCount + ' unknown course(s)', true); } - // Update the name to be unique among current planners expandedPlanners[0].name = makeUniquePlanName(expandedPlanners[0].name, allPlanData); - // Add the expanded result as a new planner to the roadmap - const currentPlanDataLength = allPlanData.length; + const updatedPlans = [...allPlanData, expandedPlanners[0]]; dispatch(addRoadmapPlan(expandedPlanners[0])); - dispatch(setPlanIndex(currentPlanDataLength)); + dispatch(setPlanIndex(updatedPlans.length - 1)); + await saveRoadmap(updatedPlans); } catch (err) { // Notify the user spawnToast('The schedule "' + schedName + '" could not be retrieved', true); - return; } }; @@ -63,7 +64,6 @@ const ImportZot4PlanPopup: FC = () => { try { // Use the backend route to try to obtain the formatted schedule await obtainImportedRoadmap(scheduleName, studentYear); - // Success; hide the modal setShowModal(false); } finally { setBusy(false); @@ -127,7 +127,7 @@ const ImportZot4PlanPopup: FC = () => { diff --git a/site/src/pages/RoadmapPage/Planner.tsx b/site/src/pages/RoadmapPage/Planner.tsx index b51c4260..87190ce5 100644 --- a/site/src/pages/RoadmapPage/Planner.tsx +++ b/site/src/pages/RoadmapPage/Planner.tsx @@ -12,6 +12,7 @@ import { selectAllPlans, setAllPlans, defaultPlan, + RoadmapPlan, } from '../../store/slices/roadmapSlice'; import { useFirstRender } from '../../hooks/firstRenderer'; import { SavedRoadmap } from '@peterportal/types'; @@ -51,13 +52,12 @@ const Planner: FC = () => { setShowSyncModal(false); }; - const saveRoadmap = () => { + const saveRoadmap = async (planner?: RoadmapPlan[]) => { const roadmap: SavedRoadmap = { timestamp: new Date().toISOString(), - planners: collapseAllPlanners(allPlanData), + planners: collapseAllPlanners(planner?.length ? planner : allPlanData), transfers: transfers, }; - localStorage.setItem('roadmap', JSON.stringify(roadmap)); // mark changes as saved to bypass alert on page leave @@ -65,7 +65,7 @@ const Planner: FC = () => { // if logged in, save data to account if (isLoggedIn) { - trpc.roadmaps.save + await trpc.roadmaps.save .mutate(roadmap) .then(() => { spawnToast(`Roadmap saved to your account!`); @@ -188,7 +188,7 @@ const Planner: FC = () => { } /> - + ); };