utils#getPercent JavaScript Examples

The following examples show how to use utils#getPercent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: paradata.js    From Queen with MIT License 6 votes vote down vote up
useSendParadatas = updateProgress => {
  const { postParadata } = useAPI();
  const postParadataRef = useAsyncValue(postParadata);

  const send = async () => {
    const paradatas = await paradataIdbService.getAll();
    let i = 0;
    updateProgress(0);
    const paradatasInError = [];
    await paradatas.reduce(async (previousPromise, paradata) => {
      await previousPromise;
      const sendParadata = async () => {
        const { error } = await postParadataRef.current(paradata);
        if (error) {
          paradatasInError.push(paradata);
          throw new Error('Server is not responding');
        }
        i += 1;
        updateProgress(getPercent(i, paradatas.length));
      };
      return sendParadata();
    }, Promise.resolve());
    return paradatasInError;
  };

  return send;
}
Example #2
Source File: surveyUnit.js    From Queen with MIT License 6 votes vote down vote up
useSaveSUsToLocalDataBase = updateProgress => {
  const { getSurveyUnits } = useAPI();
  const saveSurveyUnit = useSaveSUToLocalDataBase();

  const refrehGetSurveyUnits = useAsyncValue(getSurveyUnits);

  const putSUS = async campaignId => {
    const { data, error, status, statusText } = await refrehGetSurveyUnits.current(campaignId);

    let i = 0;
    if (!error) {
      await (data || []).reduce(async (previousPromise, surveyUnit) => {
        await previousPromise;
        i += 1;
        updateProgress(getPercent(i, data.length));
        return saveSurveyUnit(surveyUnit);
      }, Promise.resolve());
      updateProgress(100);
    } else if (![400, 403, 404, 500].includes(status)) throw new Error(statusText);
  };

  return putSUS;
}
Example #3
Source File: surveyUnit.js    From Queen with MIT License 6 votes vote down vote up
useSendSurveyUnits = updateProgress => {
  const { putUeData, putUeDataToTempZone } = useAPI();

  const putDataRef = useAsyncValue(putUeData);
  const putDataTempZoneRef = useAsyncValue(putUeDataToTempZone);
  const send = async () => {
    const surveyUnits = await surveyUnitIdbService.getAll();
    let i = 0;
    updateProgress(0);
    const surveyUnitsInTempZone = [];
    await surveyUnits.reduce(async (previousPromise, surveyUnit) => {
      await previousPromise;
      const { id, ...other } = surveyUnit;
      const sendSurveyUnit = async () => {
        const { error, status } = await putDataRef.current(id, other);
        if (error && [400, 403, 404, 500].includes(status)) {
          const { error: tempZoneError } = await putDataTempZoneRef.current(id, other);
          if (!tempZoneError) surveyUnitsInTempZone.push(id);
          else throw new Error('Server is not responding');
        }
        if (error && ![400, 403, 404, 500].includes(status))
          throw new Error('Server is not responding');
        i += 1;
        updateProgress(getPercent(i, surveyUnits.length));
      };
      return sendSurveyUnit();
    }, Promise.resolve());
    return surveyUnitsInTempZone;
  };

  return send;
}
Example #4
Source File: resources.js    From Queen with MIT License 5 votes vote down vote up
usePutResourcesInCache = updateProgress => {
  const { getRequiredNomenclatures, getNomenclature } = useAPI();

  const refreshGetRequiredNomenclatures = useAsyncValue(getRequiredNomenclatures);
  const refreshGetNomenclature = useAsyncValue(getNomenclature);

  const putResourcesInCache = async questionnaireId => {
    const ressourcesFailed = [];
    const { data, error: mainError } = await refreshGetRequiredNomenclatures.current(
      questionnaireId
    );
    if (!mainError) {
      updateProgress(0);
      await (data || []).reduce(async (previousPromise, resourceId) => {
        await previousPromise;
        const putResource = async () => {
          const { error, status, statusText } = await refreshGetNomenclature.current(resourceId);
          if (error) {
            if ([400, 403, 404, 500].includes(status)) {
              ressourcesFailed.push(resourceId);
            } else {
              throw new Error(statusText);
            }
          }
        };

        return putResource();
      }, Promise.resolve());
      updateProgress(100);
      return { success: ressourcesFailed.length === 0, ressourcesFailed };
    } else {
      return { success: false, ressourcesFailed };
    }
  };

  const putAllResourcesInCache = async questionnaireIds => {
    const questionnaireIdsSuccess = [];
    let i = 0;
    updateProgress(0);
    await (questionnaireIds || []).reduce(async (previousPromise, questionnaireId) => {
      await previousPromise;
      const putAllResources = async () => {
        const { success } = await putResourcesInCache(questionnaireId);
        if (success) questionnaireIdsSuccess.push(questionnaireId);
      };

      i += 1;
      updateProgress(getPercent(i, questionnaireIds.length));
      return putAllResources();
    }, Promise.resolve());
    return questionnaireIdsSuccess;
  };

  return putAllResourcesInCache;
}
Example #5
Source File: index.js    From Queen with MIT License 4 votes vote down vote up
useSynchronisation = () => {
  const { getCampaigns } = useAPI();

  const refrehGetCampaigns = useAsyncValue(getCampaigns);

  const [waitingMessage, setWaitingMessage] = useState(null);
  const [sendingProgress, setSendingProgress] = useState(null);
  const [sendingParadatasProgress, setSendingParadatasProgress] = useState(null);
  const [campaignProgress, setCampaignProgress] = useState(null);
  const [resourceProgress, setResourceProgress] = useState(0);
  const [surveyUnitProgress, setSurveyUnitProgress] = useState(0);
  const [current, setCurrent] = useState(null);

  const sendData = useSendSurveyUnits(setSendingProgress);
  const sendParadata = useSendParadatas(setSendingParadatasProgress);
  const putQuestionnairesInCache = usePutQuestionnairesInCache();
  const putAllResourcesInCache = usePutResourcesInCache(setResourceProgress);
  const saveSurveyUnitsToLocalDataBase = useSaveSUsToLocalDataBase(setSurveyUnitProgress);

  const getAllCampaign = async campaign => {
    const { id, questionnaireIds } = campaign;
    setResourceProgress(0);
    setSurveyUnitProgress(0);
    setCurrent('questionnaire');
    const questionnaireIdsSuccessQ = await putQuestionnairesInCache(questionnaireIds);
    setCurrent('resources');
    const questionnaireIdsSuccessR = await putAllResourcesInCache(questionnaireIds);
    setCurrent('survey-units');
    await saveSurveyUnitsToLocalDataBase(id);
    setCurrent(null);
    return {
      questionnaireIdsSuccess: innerJoinList(questionnaireIdsSuccessQ, questionnaireIdsSuccessR),
    };
  };

  const synchronize = async () => {
    var surveyUnitsInTempZone;
    var paradataInError;
    var questionnairesAccessible = [];
    // (2) : send the local data to server
    try {
      setWaitingMessage(D.waitingSendingData);
      setCurrent('send');
      surveyUnitsInTempZone = await sendData();
      paradataInError = await sendParadata();
    } catch (e) {
      return { error: 'send' };
    }

    setSendingProgress(null);
    setSendingParadatasProgress(null);

    // (3) : clean
    try {
      setCurrent('clean');
      setWaitingMessage(D.waitingCleaning);
      await clean();
    } catch (e) {
      return { error: 'clean' };
    }

    // (4) : Get the data
    try {
      setWaitingMessage(D.waintingData);
      const { data: campaigns, status, error, statusText } = await refrehGetCampaigns.current();
      let i = 0;
      setCampaignProgress(0);

      if (!error) {
        await (campaigns || []).reduce(async (previousPromise, campaign) => {
          await previousPromise;
          const loadCampaign = async () => {
            const { questionnaireIdsSuccess } = await getAllCampaign(campaign);
            questionnairesAccessible = simpleMerge(
              questionnairesAccessible,
              questionnaireIdsSuccess
            );
          };

          i += 1;
          setCampaignProgress(getPercent(i, campaigns.length));
          return loadCampaign();
        }, Promise.resolve({}));
      } else if (![404, 403, 500].includes(status)) throw new Error(statusText);
    } catch (e) {
      return { error: 'get', surveyUnitsInTempZone, questionnairesAccessible };
    }

    return { surveyUnitsInTempZone, questionnairesAccessible, paradataInError };
  };

  return {
    synchronize,
    current,
    waitingMessage,
    sendingProgress,
    sendingParadatasProgress,
    campaignProgress,
    resourceProgress,
    surveyUnitProgress,
  };
}