Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conditional mentorConfig fix #304

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions client/src/components/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { useWithRecordQueue } from "hooks/graphql/use-with-record-queue";
import { trainMentor } from "api";
import { useWithConfig } from "store/slices/config/useWithConfig";
import { BuildMentorTooltip } from "./build-mentor-tooltip";
import { MentorConfig } from "types-gql";

const useStyles = makeStyles({ name: { HomePage } })((theme: Theme) => ({
toolbar: {
Expand Down Expand Up @@ -146,10 +147,14 @@ function HomePage(props: {
error: mentorError,
} = useActiveMentor();

const { setupStatus, navigateToMissingSetup } = useWithSetup();
const { setupStatus, navigateToMissingSetup } = useWithSetup({
accessToken: props.accessToken,
});
const mentorId = getData((m) => m.data?._id || "");
const mentorType = getData((m) => m.data?.mentorType);
const mentorConfig = getData((m) => m.data?.mentorConfig);
const mentorConfig: MentorConfig | undefined = getData(
(m) => m.data?.mentorConfig
);
const defaultMentor = props.user.defaultMentor._id;
const { classes } = useStyles();
const [showSetupAlert, setShowSetupAlert] = useState(true);
Expand Down Expand Up @@ -505,20 +510,22 @@ function HomePage(props: {
{name}
</MenuItem>
))}
<MenuItem
key={"add-subject"}
data-cy={"add-subject"}
value={"add-subject"}
>
<Button
data-cy="add-a-subject"
onClick={() => {
navigate("/subjects");
}}
{mentorConfig?.subjects.length ? undefined : (
<MenuItem
key={"add-subject"}
data-cy={"add-subject"}
value={"add-subject"}
>
+ Add a subject
</Button>
</MenuItem>
<Button
data-cy="add-a-subject"
onClick={() => {
navigate("/subjects");
}}
>
+ Add a subject
</Button>
</MenuItem>
)}
</Select>
</ColorTooltip>
</div>
Expand Down
11 changes: 9 additions & 2 deletions client/src/components/setup/record-subject-slide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import React from "react";
import { Typography, Button } from "@mui/material";
import { Subject, Answer, MentorType } from "types";
import { Slide } from "./slide";
import { isAnswerComplete, urlBuild } from "helpers";
import { getValueIfKeyExists, isAnswerComplete, urlBuild } from "helpers";
import { useAppSelector } from "store/hooks";

export function RecordSubjectSlide(props: {
classes: Record<string, string>;
Expand All @@ -20,8 +21,14 @@ export function RecordSubjectSlide(props: {
customTitle?: string; // pass in optional slide title
}): JSX.Element {
const { classes, subject, answers, i } = props;
const mentorQuestions = useAppSelector((state) => state.questions.questions);

const recorded = answers.filter((a) =>
isAnswerComplete(a, undefined, props.mentorType)
isAnswerComplete(
a,
getValueIfKeyExists(a.question, mentorQuestions)?.question?.name,
props.mentorType
)
);
const isRecorded = answers.length === recorded.length;

Expand Down
48 changes: 32 additions & 16 deletions client/src/hooks/graphql/use-with-setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
MentorType,
SetupStatus,
Subject,
UploadTaskStatuses,
UtteranceName,
} from "types";
import { LoadingError } from "./loading-reducer";
Expand All @@ -22,6 +23,7 @@ import useActiveMentor from "store/slices/mentor/useActiveMentor";
import useQuestions from "store/slices/questions/useQuestions";
import { LoadingStatus } from "./generic-loading-reducer";
import { useAppSelector } from "store/hooks";
import { useWithUploadStatus } from "./use-with-upload-status";

//order of the slides
export enum SetupStepType {
Expand Down Expand Up @@ -68,7 +70,11 @@ interface UseWithSetup {
onLeave: (cb: () => void) => void;
}

export function useWithSetup(search?: { i?: string }): UseWithSetup {
export function useWithSetup(
props: { accessToken: string },
search?: { i?: string }
): UseWithSetup {
const { accessToken } = props;
const [initialSetupStep] = useState<number>(
search?.i ? parseInt(search.i) : 0
);
Expand All @@ -82,6 +88,7 @@ export function useWithSetup(search?: { i?: string }): UseWithSetup {
isSaving: isMentorSaving,
error: mentorError,
} = useActiveMentor();
const { uploads } = useWithUploadStatus(accessToken);

const mentor: Mentor = getData((state) => state.data);
useQuestions(
Expand Down Expand Up @@ -149,12 +156,23 @@ export function useWithSetup(search?: { i?: string }): UseWithSetup {
return {
subject: s,
answers: answers,
complete: answers.every((a: Answer) =>
isAnswerComplete(
a,
getValueIfKeyExists(a.question, mentorQuestions)?.question?.name,
mentor.mentorType
)
complete: answers.every(
(a: Answer) =>
isAnswerComplete(
a,
getValueIfKeyExists(a.question, mentorQuestions)?.question
?.name,
mentor.mentorType
) ||
uploads.find(
(u) =>
u.question ==
(getValueIfKeyExists(a.question, mentorQuestions)?.question
?.question || "") &&
!u.taskList.some(
(t) => t.status === UploadTaskStatuses.FAILED
)
)
),
};
});
Expand All @@ -175,11 +193,11 @@ export function useWithSetup(search?: { i?: string }): UseWithSetup {
requiredSubjects,
isSetupComplete,
});
const mentorSubjectsLocked = mentor.mentorConfig.subjects.length;
const mentorSubjectsLocked = mentor.mentorConfig?.subjects.length;
const mentorPrivacyLocked =
mentor.mentorConfig.publiclyVisible !== undefined ||
mentor.mentorConfig.orgPermissions.length;
const mentorTypeLocked = mentor.mentorConfig.mentorType;
mentor.mentorConfig?.publiclyVisible !== undefined ||
mentor.mentorConfig?.orgPermissions.length;
const mentorTypeLocked = mentor.mentorConfig?.mentorType;
const status: SetupStep[] = [
{ type: SetupStepType.WELCOME, complete: true },
{ type: SetupStepType.MENTOR_INFO, complete: isMentorInfoDone },
Expand Down Expand Up @@ -211,8 +229,6 @@ export function useWithSetup(search?: { i?: string }): UseWithSetup {
type: SetupStepType.FINISH_SETUP,
complete: isSetupComplete,
});
console.log(status);
console.log(status.length);
setSteps(status);
}, [mentor, configState.config, isMentorLoading, questionsLoadingStatus]);

Expand All @@ -230,9 +246,9 @@ export function useWithSetup(search?: { i?: string }): UseWithSetup {
return;
}
if (isMentorEdited) {
if (idx === SetupStepType.SELECT_KEYWORDS) {
if (steps[idx].type === SetupStepType.SELECT_KEYWORDS) {
saveMentorKeywords();
} else if (idx === SetupStepType.MENTOR_PRIVACY) {
} else if (steps[idx].type === SetupStepType.MENTOR_PRIVACY) {
saveMentorPrivacy();
} else {
saveMentorDetails();
Expand All @@ -249,7 +265,7 @@ export function useWithSetup(search?: { i?: string }): UseWithSetup {
if (!status) {
return;
}
if (mentor.mentorConfig.configId) {
if (mentor.mentorConfig?.configId) {
// TODO: need to update the way we navigate setup via url, should not
// be hardcoded carousel indexes, should instead be based off the SetupStepType
navigate("/setup");
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function SetupPage(props: {
virtualBackgroundUrls,
defaultVirtualBackground,
onLeave,
} = useWithSetup(props.search);
} = useWithSetup({ accessToken: props.accessToken }, props.search);
const accessToken = props.accessToken;
const { data: keywords, isLoading: keywordsLoading } = useWithKeywords();
const { state: configState, isConfigLoaded } = useWithConfig();
Expand Down
2 changes: 1 addition & 1 deletion client/src/types-gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface MentorGQL {
isPrivate: boolean;
isArchived: boolean;
isAdvanced: boolean;
mentorConfig: MentorConfig;
mentorConfig?: MentorConfig;
orgPermissions: OrgPermission[];
defaultSubject?: SubjectGQL;
subjects: SubjectGQL[];
Expand Down
2 changes: 1 addition & 1 deletion client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export interface Mentor {
isPrivate: boolean;
isArchived: boolean;
isAdvanced: boolean;
mentorConfig: MentorConfig;
mentorConfig?: MentorConfig;
orgPermissions: OrgPermission[];
defaultSubject?: Subject;
subjects: Subject[];
Expand Down