Loading a questionnaire
This section illustrates how the data flows within Pogues. For this, it looks at what happens when a user selects a questionnaire.
When a user selects a questionnaire from the questionnaire list, there are many elementary steps to process, from sending the request to updating the UI. Here is a global picture:
- The application shows the
QuestionnairePicker
component. - The user selects a questionnaire in the questionnaire list.
- The
switchQuestionnaire
action creator is called. - The
SWITCH_TO_QUESTIONNAIRE
action is dispatched. It contains the questionnaire id in its payload. - The switchToQuestionnaire handler from the
appState
reducer updates theview
entry and set it toQUESTIONNAIRE
. - The application re-renders.
- When rendering the main
PoguesApp
component, theview
entry is checked, and theQuestionnaireContainer
component is rendered, in place of the previousQuestionnairePicker
component. - In its
componentWillMount
method, the loadQuestionnaireIfNeeded function is called. It takes the questionnaire id as an argument. - Assuming the questionnaire has not been already loaded, the
loadQuestionnaire
action creator is called. - The component renders a spinner to indicate that the questionnaire is loading.
- The
LOAD_QUESTIONNAIRE
action will be dispatched, with the questionnaire id in its payload. - The getQuestionnaire function is called. The browser tries to fetch the given resource and returns a Promise.
. The fetch call succeeds and the Promise resolves to a
JSON
representation of the questionnaire. - The
then
handler in the action creators file is executed. - It calls the toSate function and dispatches the
LOAD_QUESTIONNAIRE_SUCCESS
action with the result of this function in its payload. - The
LOAD_QUESTIONNAIRE_SUCCESS
is processed by many reducers to update the application state: to add an entry in thequestionnaires-by-id
reducer, to add multiple entries in thecomponents-by-id
reducer (one for each sequence and each question in the questionnaire), to add multiple entries in thecode-by-id
reducer (one for each code in every code list in the questionnaire)... - The application re renders. The questionnaire content is now visible.