Skip to content

Commit

Permalink
feat: ajout du temps passer sur le questionnaire + mutation de sauveg…
Browse files Browse the repository at this point in the history
…arde des reponses (#149)

* feat: ajout du temps passer sur le questionnaire + mutation de sauvegarde des reponses

* fix: remettre l'url de PP pour la review pour en faire une autre PR

* fix: issues with merge

* fix: issues with merge

* fix: remove dates from local storage and fix bugs

* fix: format date HH:MM:SS
  • Loading branch information
mbarry-ippon authored Apr 11, 2023
1 parent 5ac15b6 commit ac88957
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
45 changes: 45 additions & 0 deletions apollo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,51 @@ export const client = new ApolloClient({
link: new HttpLink({ uri: `${API_URL}/graphql?nocache`, fetch }),
})

export const EPDS_ADD_SURVEY_RESULTS = gql`
mutation (
$genre: ENUM_REPONSESEPDS_GENRE!
$compteur: Int!
$score: Int!
$reponseNum1: Int!
$reponseNum2: Int!
$reponseNum3: Int!
$reponseNum4: Int!
$reponseNum5: Int!
$reponseNum6: Int!
$reponseNum7: Int!
$reponseNum8: Int!
$reponseNum9: Int!
$reponseNum10: Int!
$tempsSurvey: String!
$langue: ID
$source: ENUM_REPONSESEPDS_SOURCE!
$sourceWidgetNom: String!
) {
createReponsesEpdsWidget(
genre: $genre
compteur: $compteur
score: $score
reponse_1: $reponseNum1
reponse_2: $reponseNum2
reponse_3: $reponseNum3
reponse_4: $reponseNum4
reponse_5: $reponseNum5
reponse_6: $reponseNum6
reponse_7: $reponseNum7
reponse_8: $reponseNum8
reponse_9: $reponseNum9
reponse_10: $reponseNum10
temps_survey: $tempsSurvey
langue: $langue
source: $source
source_widget_nom: $sourceWidgetNom
) {
id
created_at
}
}
`

export const EPDS_CONTACT_INFORMATION = gql`
mutation (
$email: String
Expand Down
1 change: 1 addition & 0 deletions pages/survey/before-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function BeforeSurvey() {
const router = useRouter()

const IMG_PRO = "../img/icone-pro-sante.svg"

const IMG_PARENTS = "../img/icone-parent.svg"
const IMG_ACCOMPAGNEMENT = "../img/icone-accompagnement.svg"

Expand Down
25 changes: 17 additions & 8 deletions pages/survey/epds-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useRef, useState } from "react"
import {
client,
UPDATE_REPONSES_EPDS_ID_IN_INFORMATION_DEMOGRAPHIQUES,
EPDS_ADD_SURVEY_RESULTS,
} from "../../apollo-client"
import { ContentLayout } from "../../src/components/Layout"
import { SurveyCarousel } from "../../src/components/survey/SurveyCarousel"
Expand All @@ -28,13 +29,11 @@ import {
} from "../../src/utils/score-level.utils"
import { Labels } from "../../src/constants/specificLabels"
import { WidgetHeader } from "../../src/components/WidgetHeader"
import {
EPDS_SAVE_RESPONSES_FOR_WIDGET,
EPDS_SURVEY_TRANSLATION_BY_LOCALE,
} from "@socialgouv/nos1000jours-lib"
import { EPDS_SURVEY_TRANSLATION_BY_LOCALE } from "@socialgouv/nos1000jours-lib"
import { updateDemographicData } from "../ab-testing/demographic-data-survey"
import * as StorageUtils from "../../src/utils/storage.utils"
import * as TrackerUtils from "../../src/utils/tracker.utils"
import * as ResultsUtils from "../../src/utils/result.utils"

export default function EpdsSurvey() {
const router = useRouter()
Expand All @@ -50,12 +49,11 @@ export default function EpdsSurvey() {
const [isEnabledNextButton, setEnabledNextButton] = useState(false)
const [sendScore, setSendScore] = useState(false)
const [isLoading, setLoading] = useState(false)

const [startSurveyTime, setStartSurveyTime] = useState()
const source = StorageUtils.getInLocalStorage(STORAGE_SOURCE)
const isBackFromConfirmed = StorageUtils.getInLocalStorage(
STORAGE_IS_BACK_RESULTS
)
const scoreValue = StorageUtils.getInLocalStorage(STORAGE_SCORE)

const [getEpdsSurveyQuery] = useLazyQuery(
gql(EPDS_SURVEY_TRANSLATION_BY_LOCALE),
Expand All @@ -73,7 +71,7 @@ export default function EpdsSurvey() {
}
)

const [saveResponseQuery] = useMutation(gql(EPDS_SAVE_RESPONSES_FOR_WIDGET), {
const [saveResponseQuery] = useMutation(EPDS_ADD_SURVEY_RESULTS, {
client: client,
onError: (err) => {
console.warn(err)
Expand All @@ -96,6 +94,7 @@ export default function EpdsSurvey() {
STORAGE_SCORE_LEVEL_MACARON,
scoreLevelForMacaron(totalScore, resultsBoard[9].points)
)

localStorage.setItem(STORAGE_RESULTS_ID, data.createReponsesEpdsWidget.id)
if (
TrackerUtils.seuilScore(totalScore) &&
Expand All @@ -121,13 +120,14 @@ export default function EpdsSurvey() {
}
)

const goToResults = async (event) => {
const goToResults = async () => {
router.push({
pathname: "/results",
})
}

useEffect(() => {
setStartSurveyTime(new Date())
setLocaleSelected(StorageUtils.getLocaleInLocalStorage())
}, [])

Expand Down Expand Up @@ -162,6 +162,14 @@ export default function EpdsSurvey() {
const score = resultsBoard
.map((data) => data.points)
.reduce((a, b) => a + b, 0)
const endSurveyTime = new Date()
let totalTime = ""
if (startSurveyTime && endSurveyTime) {
totalTime = ResultsUtils.getTotalTimeInSurvey(
startSurveyTime,
endSurveyTime
)
}

await saveResponseQuery({
variables: {
Expand All @@ -177,6 +185,7 @@ export default function EpdsSurvey() {
reponseNum8: resultsBoard[7].points,
reponseNum9: resultsBoard[8].points,
reponseNum10: resultsBoard[9].points,
tempsSurvey: totalTime,
score: score,
langue: localeSelected.id,
source: EPDS_SOURCE,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/result.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ export const convertResultsInStorageToContentTable = (resultsBoard) => {
}
})
}

export const getTotalTimeInSurvey = (startSurvey, endSurvey) => {
const totalSeconds = Math.floor(
(new Date(endSurvey).getTime() - new Date(startSurvey).getTime()) / 1000
)
if (totalSeconds < 60) {
return `00:00:${totalSeconds}`
} else {
const convertToMinutes = Math.floor(totalSeconds / 60)
const leftSeconds = totalSeconds % 60
return `00:${convertToMinutes}:${leftSeconds}`
}
}

0 comments on commit ac88957

Please sign in to comment.