Skip to content

Commit

Permalink
[F] Handle pending accounts in SaveForm
Browse files Browse the repository at this point in the history
Resolves #52
  • Loading branch information
dananjohnson authored and Blake Mason committed Oct 12, 2023
1 parent 66d52d8 commit d320c45
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
8 changes: 7 additions & 1 deletion app/[locale]/[investigation]/[...uriSegments]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ const UriSegments: (props: UriSegmentsProps) => Promise<JSX.Element> = async ({

const user = getUserFromJwt(craftToken);

return <Template {...{ site, user, locale }} data={entry} />;
return (
<Template
{...{ site, user, locale }}
data={entry}
userStatus={craftUserStatus}
/>
);
};

export default UriSegments;
21 changes: 18 additions & 3 deletions components/answers/SaveForm/SaveForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ import { getUserFromJwt } from "@/components/auth/serverHelpers";
export default function SaveForm({
investigationId,
user,
userStatus,
}: {
investigationId: InvestigationId;
user?: ReturnType<typeof getUserFromJwt>;
userStatus?: string;
}) {
const { t } = useTranslation();

const [status, setStatus] = useState<
"emptyError" | "refreshError" | "mutationError" | "success" | null
>(null);
| "emptyError"
| "refreshError"
| "mutationError"
| "statusError"
| "success"
| null
>(userStatus === "pending" ? "statusError" : null);

if (!investigationId || !user) return null;

Expand Down Expand Up @@ -48,6 +55,8 @@ export default function SaveForm({
);
if (result === "refreshError") {
setStatus("refreshError");
} else if (result === "statusError") {
setStatus("statusError");
} else {
setStatus("success");
}
Expand All @@ -57,7 +66,10 @@ export default function SaveForm({
}}
aria-label={t("answers.save_form.label") ?? undefined}
>
<Submit disabled={status !== null} icon="FloppyDisk">
<Submit
disabled={status !== null}
icon="FloppyDisk"
>
{(pending) =>
t(
pending
Expand All @@ -79,6 +91,9 @@ export default function SaveForm({
{status === "mutationError" && (
<p>{t("answers.save_form.mutation_error_message")}</p>
)}
{status === "statusError" && (
<p>{t("answers.save_form.status_error_message")}</p>
)}
</output>
</form>
);
Expand Down
6 changes: 5 additions & 1 deletion components/educator-schema/saveAnswersAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export default async function saveAnswers(
investigationId: NonNullable<InvestigationId>,
answers: Answers
) {
const { craftUserId, craftToken } = await getAuthCookies();
const { craftUserId, craftToken, craftUserStatus } = await getAuthCookies();

if (!craftUserId || !craftToken) {
return "refreshError";
}

if (craftUserStatus === "pending") {
return "statusError";
}

const answerSet = Object.values(answers);

const { data, error } = await mutateAPI({
Expand Down
6 changes: 5 additions & 1 deletion components/student-schema/saveAnswersAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export default async function saveAnswers(
investigationId: NonNullable<InvestigationId>,
answers: Answers
) {
const { craftUserId, craftToken } = await getAuthCookies();
const { craftUserId, craftToken, craftUserStatus } = await getAuthCookies();

if (!craftUserId || !craftToken) {
return "refreshError";
}

if (craftUserStatus === "pending") {
return "statusError";
}

const answerSet = Object.values(answers);

const { data, error } = await mutateAPI({
Expand Down
7 changes: 6 additions & 1 deletion components/templates/InvestigationChildPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const InvestigationChildPage: FunctionComponent<{
data: FragmentType<typeof Fragment>;
site: string;
user?: ReturnType<typeof getUserFromJwt>;
userStatus?: string;
children?: React.ReactNode;
}> = (props) => {
const data = useFragment(Fragment, props.data);
Expand All @@ -145,7 +146,11 @@ const InvestigationChildPage: FunctionComponent<{
)
)}
{data.hasSavePoint && props.user && (
<SaveForm investigationId={data.parent?.id} user={props.user} />
<SaveForm
investigationId={data.parent?.id}
user={props.user}
userStatus={props.userStatus}
/>
)}
</Styled.ContentBlocks>
);
Expand Down
3 changes: 2 additions & 1 deletion public/localeStrings/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@
"success_message": "Answers saved!",
"empty_error_message": "No new answers to save.",
"refresh_error_message": "Your session has expired. Please log back in and try again.",
"mutation_error_message": "There was an error saving your answers to your account."
"mutation_error_message": "There was an error saving your answers to your account.",
"status_error_message": "Activate your account to begin saving. Check your inbox for an activation email."
}
},
"review": {
Expand Down

0 comments on commit d320c45

Please sign in to comment.