Skip to content

Commit

Permalink
Changed subscription schema to receive server errors, show them using…
Browse files Browse the repository at this point in the history
… toast, update query to use missing parameters, re run query even if the same parameters are used
  • Loading branch information
dngomez committed Aug 27, 2024
1 parent 486618d commit cf7e33a
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 54 deletions.
18 changes: 16 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { useSubscription } from "@apollo/client";
import { subscriptionQueueSchedule } from "./components/GlobalState/scheduleSubscription";
import { useContext, useEffect, useRef } from "react";
import { GlobalStateContext } from "./components/GlobalState/GlobalState";
import { Toast } from "primereact/toast";

function App() {
const { setNightPlans, setPlansSummary, setLoadingPlan, uuid } =
useContext(GlobalStateContext);

const toast = useRef<Toast>(null);

const { data: scheduleData, loading: subscriptionLoading } = useSubscription(
subscriptionQueueSchedule,
{
Expand All @@ -18,15 +21,26 @@ function App() {

useEffect(() => {
if (!subscriptionLoading) {
setNightPlans(scheduleData?.queueSchedule?.nightPlans?.nightTimeline);
setPlansSummary(scheduleData?.queueSchedule?.plansSummary);
if (scheduleData.queueSchedule.__typename === "NewNightPlans") {
setNightPlans(scheduleData.queueSchedule.nightPlans.nightTimeline);
setPlansSummary(scheduleData.queueSchedule.plansSummary);
} else if (scheduleData.queueSchedule.__typename === "NightPlansError") {
toast.current?.show({
severity: "error",
summary: "Error",
detail: scheduleData.queueSchedule.error,
});
setNightPlans([]);
setPlansSummary({} as any);
}
setLoadingPlan(false);
}
}, [scheduleData, subscriptionLoading]);

return (
<Layout>
<Outlet />
<Toast ref={toast} />
</Layout>
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/apollo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ const wsLink = new GraphQLWsLink(
import.meta.env.VITE_API_URL ??
"https://gpp-schedule-staging.herokuapp.com/graphql"
}`,

keepAlive: 10000,
retryAttempts: Infinity,
shouldRetry: () => true,
on: {
connected: () => {
console.log("Socket successfully connected");
},
error: (error) => {
console.log("Socket error", error);
},
closed: () => {
console.log("Socket closed");
},
},
})
);

Expand Down
72 changes: 67 additions & 5 deletions src/components/ControlPanel/ControlPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useRef, useContext } from "react";
import { useState, useRef, useContext, useEffect } from "react";
import { GlobalStateContext } from "../GlobalState/GlobalState";
import "./ControlPanel.scss";

Expand Down Expand Up @@ -34,9 +34,13 @@ export default function ControlPanel() {
];

const [numNight, setNumNight] = useState<number>(1);
// const { isReady, txMessage } = useContext(WebsocketContext);
const [schedule, { loading, error, data: scheduleData }] =
useLazyQuery(scheduleQuery);
const [validInputs, setValidInputs] = useState(false);
const [schedule, { loading, error, data: scheduleData }] = useLazyQuery(
scheduleQuery,
{
fetchPolicy: "no-cache",
}
);

const {
thesis,
Expand All @@ -51,6 +55,60 @@ export default function ControlPanel() {
uuid,
} = useContext(GlobalStateContext);

function validateInputs() {
let valid = true;
if (numNight < 1) {
toast.current?.show({
severity: "error",
summary: "Error",
detail: "Number of nights must be at least 1",
life: 3000,
});
valid = false;
}

let dateRange = 1;
if (datesState) {
if (datesState[0] && datesState[1]) {
dateRange =
1 +
Math.floor(
Math.abs(datesState[1].getTime() - datesState[0].getTime()) /
1000 /
60 /
60 /
24
);
}
}

if (dateRange < 2 && !semesterVisibility) {
toast.current?.show({
severity: "error",
summary: "Error",
detail: "A date range should be selected or use semester visibility",
life: 3000,
});
valid = false;
}

if (numNight > dateRange && !semesterVisibility) {
toast.current?.show({
severity: "error",
summary: "Error",
detail: "Number of nights cannot be greater than date range",
life: 3000,
});
valid = false;
}
return valid;
}

useEffect(() => {
if (validateInputs()) setValidInputs(true);
else setValidInputs(false);
}, [numNight, datesState, semesterVisibility]);

const customBase64Uploader = async (event: FileUploadHandlerEvent) => {
// convert file to base64 encoded
const file = event.files[0];
Expand Down Expand Up @@ -83,7 +141,8 @@ export default function ControlPanel() {
datesState !== null &&
datesState.length >= 2 &&
Array.isArray(datesState) &&
numNight
numNight &&
validInputs
);

const onRunClick = () => {
Expand All @@ -99,6 +158,9 @@ export default function ControlPanel() {
thesisFactor: thesis,
semesterVisibility: semesterVisibility,
power: power,
whaPower: whaPower,
metPower: metPower,
visPower: visPower,
},
});
};
Expand Down
6 changes: 6 additions & 0 deletions src/components/ControlPanel/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const scheduleQuery = graphql(`
$semesterVisibility: Boolean!
$thesisFactor: Float
$power: Int
$metPower: Float
$whaPower: Float
$visPower: Float
) {
testSubQuery(
scheduleId: $scheduleId
Expand All @@ -20,6 +23,9 @@ export const scheduleQuery = graphql(`
endTime: $endTime
thesisFactor: $thesisFactor
power: $power
metPower: $metPower
whaPower: $whaPower
visPower: $visPower
semesterVisibility: $semesterVisibility
numNightsToSchedule: $numNightsToSchedule
}
Expand Down
84 changes: 45 additions & 39 deletions src/components/GlobalState/scheduleSubscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,61 @@ import { graphql } from "../../gql";
export const subscriptionQueueSchedule = graphql(`
subscription queueSchedule($scheduleId: String!) {
queueSchedule(scheduleId: $scheduleId) {
nightPlans {
nightTimeline {
nightIndex
timeEntriesBySite {
site
mornTwilight
eveTwilight
timeEntries {
startTimeSlots
event
plan {
startTime
nightConditions {
iq
cc
}
visits {
obsId
endTime
altitude
atomEndIdx
atomStartIdx
__typename
... on NewNightPlans {
nightPlans {
nightTimeline {
nightIndex
timeEntriesBySite {
site
mornTwilight
eveTwilight
timeEntries {
startTimeSlots
event
plan {
startTime
instrument
fpu
disperser
filters
score
obsClass
completion
peakScore
requiredConditions {
nightConditions {
iq
cc
}
}
nightStats {
timeLoss
planScore
nToos
completionFraction
programCompletion
visits {
obsId
endTime
altitude
atomEndIdx
atomStartIdx
startTime
instrument
fpu
disperser
filters
score
obsClass
completion
peakScore
requiredConditions {
iq
cc
}
}
nightStats {
timeLoss
planScore
nToos
completionFraction
programCompletion
}
}
}
}
}
}
plansSummary
}
... on NightPlansError {
error
}
plansSummary
}
}
`);
Loading

0 comments on commit cf7e33a

Please sign in to comment.