Skip to content

Commit

Permalink
fix(datatrak): Fix survey autofill loading (#5883)
Browse files Browse the repository at this point in the history
* add loading

* Update TaskOverdueChecker.js
  • Loading branch information
tcaiger authored Sep 6, 2024
1 parent fd9323e commit 20bc1ac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/
import { sendEmail } from '@tupaia/server-utils';
import { requireEnv } from '@tupaia/utils';
import winston from 'winston';
import { ScheduledTask } from './ScheduledTask';

Expand Down Expand Up @@ -32,6 +33,7 @@ export class TaskOverdueChecker extends ScheduledTask {
winston.error(`Assignee with id ${overdueTask.assignee_id} not found`);
continue;
}
const datatrakURL = requireEnv('DATATRAK_FRONT_END_URL');

const result = await sendEmail(assignee.email, {
subject: 'Task overdue on Tupaia.org',
Expand All @@ -40,6 +42,10 @@ export class TaskOverdueChecker extends ScheduledTask {
userName: assignee.first_name,
surveyName: overdueTask.survey_name,
entityName: overdueTask.entity_name,
cta: {
url: `${datatrakURL}/tasks/${overdueTask.id}`,
text: 'View task',
},
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const SurveyContext = ({ children, surveyCode, countryCode }) => {
const primaryEntityQuestion = flattenedScreenComponents.find(
question => question.type === QuestionType.PrimaryEntity,
);
const autoFillAnswers = usePrimaryEntityQuestionAutoFill(
const { data: autoFillAnswers } = usePrimaryEntityQuestionAutoFill(
primaryEntityQuestion,
flattenedScreenComponents,
primaryEntityCode,
Expand Down
5 changes: 4 additions & 1 deletion packages/datatrak-web/src/features/Survey/SurveyLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import React from 'react';
import { useIsFetching } from 'react-query';
import { Outlet, generatePath, useNavigate, useParams } from 'react-router';
import { useFormContext } from 'react-hook-form';
import styled from 'styled-components';
Expand Down Expand Up @@ -76,6 +77,8 @@ export const SurveyLayout = () => {
const from = useFromLocation();
const primaryEntityCode = usePrimaryEntityLocation();
const params = useParams<SurveyParams>();
const isFetchingEntities = useIsFetching({ queryKey: ['entityAncestors'] });

const {
updateFormData,
formData,
Expand Down Expand Up @@ -152,7 +155,7 @@ export const SurveyLayout = () => {

const handleClickSubmit = handleSubmit(onSubmit, onError);

const showLoader = isSubmittingSurveyResponse || isResubmittingSurveyResponse;
const showLoader = isSubmittingSurveyResponse || isResubmittingSurveyResponse || isFetchingEntities;

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ export const usePrimaryEntityQuestionAutoFill = (
primaryEntityCode?: Entity['code'],
) => {
const user = useCurrentUserContext();
const { data: ancestors } = useEntityAncestors(user.project?.code, primaryEntityCode);
const { data = {}, ...query } = useEntityAncestors(user.project?.code, primaryEntityCode);

if (!ancestors) {
return {};
}
const ancestors = data
? getEntityQuestionAncestorAnswers(
primaryEntityQuestion as SurveyScreenComponent,
keyBy(questions, 'id'),
keyBy(data, 'type'),
)
: {};

return getEntityQuestionAncestorAnswers(
primaryEntityQuestion as SurveyScreenComponent,
keyBy(questions, 'id'),
keyBy(ancestors, 'type'),
);
return {
...query,
data: ancestors,
};
};

0 comments on commit 20bc1ac

Please sign in to comment.