Skip to content

Commit

Permalink
feat(preselected topic): check if agency matches topic
Browse files Browse the repository at this point in the history
  • Loading branch information
RunzelRosinchen committed Jul 18, 2023
1 parent 79b0957 commit 61b8b17
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
20 changes: 0 additions & 20 deletions src/components/registration/Registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,6 @@ export const Registration = () => {

const checkForStepsWithMissingMandatoryFields = (): number[] => {
if (currentStep > 0) {
//fix missing step stuff
console.log(
availableSteps.reduce<number[]>(
(missingSteps, step, currentIndex) => {
if (
step?.mandatoryFields?.some(
(mandatoryField) =>
sessionStorageRegistrationData?.[
mandatoryField
] === undefined
)
) {
return [...missingSteps, currentIndex];
}
return missingSteps;
},
[]
)
);
console.log(sessionStorageRegistrationData, availableSteps);
return availableSteps.reduce<number[]>(
(missingSteps, step, currentIndex) => {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ export const PreselectionBox: VFC<{
useEffect(() => {
if (preselectedTopicName) {
setTopicName(preselectedTopicName);
} else {
setTopicName('-');
}
if (preselectedAgency) {
setAgencyName(preselectedAgency?.name);
} else {
setAgencyName('-');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [preselectedAgency, preselectedTopicName]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ export const TopicSelection: VFC<{
useEffect(() => {
if (topics.length === 1) {
setValue(topics[0].id);
setDataForSessionStorage({
topicId: topics[0].id
});
}
}, [topics]);
}, [setDataForSessionStorage, topics]);

useEffect(() => {
const getFilteredTopics = (topics: TopicsDataInterface[]) => {
Expand Down
32 changes: 30 additions & 2 deletions src/globalState/provider/RegistrationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
AgencyDataInterface,
ConsultantDataInterface
} from '../interfaces/UserDataInterface';
import { TopicsDataInterface } from '../interfaces/TopicsDataInterface';

interface RegistrationContextInterface {
disabledNextButton?: boolean;
Expand Down Expand Up @@ -73,6 +74,8 @@ export function RegistrationProvider(props) {
>([]);
const [preselectedAgency, setPreselectedAgency] =
useState<AgencyDataInterface>();
const [preselectedTopic, setPreselectedTopic] =
useState<TopicsDataInterface>();
const [preselectedTopicName, setPreselectedTopicName] = useState<string>();
const [dataPrepForSessionStorage, setDataPrepForSessionStorage] = useState<
Partial<RegistrationSessionStorageData>
Expand Down Expand Up @@ -117,7 +120,8 @@ export function RegistrationProvider(props) {
if (
urlQuery.get('postcode') ||
urlQuery.get('aid') ||
urlQuery.get('tid')
urlQuery.get('tid') ||
urlQuery.get('cid')
) {
const zipcodeRegex = new RegExp(
/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/
Expand Down Expand Up @@ -176,11 +180,17 @@ export function RegistrationProvider(props) {
}, [urlQuery]);

useEffect(() => {
// TODO: Check if topic and agency match once agencies get topics
if (sessionStorageRegistrationData.topicId) {
(async () => {
try {
const topicsResponse = await apiGetTopicsData();
setPreselectedTopic(
topicsResponse.filter(
(topic) =>
topic.id ===
sessionStorageRegistrationData.topicId
)[0] || undefined
);
setPreselectedTopicName(
topicsResponse.filter(
(topic) =>
Expand Down Expand Up @@ -211,6 +221,9 @@ export function RegistrationProvider(props) {
}
}
})();
} else {
setPreselectedTopic(undefined);
setPreselectedTopicName(undefined);
}
if (sessionStorageRegistrationData.agencyId) {
(async () => {
Expand All @@ -229,6 +242,8 @@ export function RegistrationProvider(props) {
}
}
})();
} else {
setPreselectedAgency(undefined);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sessionStorageRegistrationData]);
Expand All @@ -250,6 +265,19 @@ export function RegistrationProvider(props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasAgencyError, hasTopicError]);

useEffect(() => {
// Check if agency matches preselected topic
if (
urlQuery.get('tid') &&
preselectedTopic &&
preselectedAgency &&
!preselectedAgency?.topicIds?.includes(preselectedTopic.id)
) {
setPreselectedAgency(undefined);
setHasAgencyError(true);
}
}, [preselectedAgency, preselectedTopic, urlQuery]);

const updateSessionStorage = (
dataToAdd?: Partial<RegistrationSessionStorageData>
) => {
Expand Down

0 comments on commit 61b8b17

Please sign in to comment.