Skip to content

Commit

Permalink
Tim/autosave roadmap import (#573)
Browse files Browse the repository at this point in the history
* fix: abort signals prevent cancelled requests from continuing

* Update site/src/component/SearchModule/SearchModule.tsx

Co-authored-by: Awesome-E <[email protected]>

* refactor:shortened

* feat: save

* fix: saves roadmap after importzing z4p

* removed unneeded commit

* fix: redux race condition

---------

Co-authored-by: Awesome-E <[email protected]>
  • Loading branch information
timobraz and Awesome-E authored Jan 31, 2025
1 parent c0cc7b6 commit 221ad2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions site/src/pages/RoadmapPage/ImportZot4PlanPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ 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';
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<void>;
}
const ImportZot4PlanPopup: FC<ImportZot4PlanPopupProps> = ({ saveRoadmap }) => {
const dispatch = useAppDispatch();
const { darkMode } = useContext(ThemeContext);
const [showModal, setShowModal] = useState(false);
Expand Down Expand Up @@ -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;
}
};

Expand All @@ -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);
Expand Down Expand Up @@ -127,7 +127,7 @@ const ImportZot4PlanPopup: FC = () => {
</Form.Group>
</Form>
<Button variant="primary" disabled={busy || scheduleName.length < 8} onClick={handleImport}>
{busy ? 'Importing...' : 'Import'}
{busy ? 'Importing...' : 'Import and Save'}
</Button>
</Modal.Body>
</Modal>
Expand Down
10 changes: 5 additions & 5 deletions site/src/pages/RoadmapPage/Planner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
selectAllPlans,
setAllPlans,
defaultPlan,
RoadmapPlan,
} from '../../store/slices/roadmapSlice';
import { useFirstRender } from '../../hooks/firstRenderer';
import { SavedRoadmap } from '@peterportal/types';
Expand Down Expand Up @@ -51,21 +52,20 @@ 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
dispatch(setUnsavedChanges(false));

// 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!`);
Expand Down Expand Up @@ -188,7 +188,7 @@ const Planner: FC = () => {
}
/>
<ImportTranscriptPopup />
<ImportZot4PlanPopup />
<ImportZot4PlanPopup saveRoadmap={saveRoadmap} />
</div>
);
};
Expand Down

0 comments on commit 221ad2a

Please sign in to comment.