Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: GPS coordinates during registration #1377

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions client/src/elm/App/Model.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module App.Model exposing
( ConfiguredModel
, Flags
, GPSCoordinates
, LoggedInModel
, MemoryQuota
, Model
Expand Down Expand Up @@ -163,6 +164,9 @@ type alias Model =

-- List of errors we'll send to console.log
, errors : List Error

-- GPS coordinates that need to be recorded during registration.
, coordinates : Maybe GPSCoordinates
}


Expand Down Expand Up @@ -214,6 +218,13 @@ emptyModel key url flags =
, villageId = villageId
, syncManager = SyncManager.Model.emptyModel syncManagerFlags
, errors = []
, coordinates = Nothing
}


type alias GPSCoordinates =
{ latitude : Float
, longitude : Float
}


Expand Down Expand Up @@ -403,6 +414,7 @@ type Msg
| SetPersistentStorage Bool
| SetStorageQuota StorageQuota
| SetMemoryQuota MemoryQuota
| SetGPSCoordinates GPSCoordinates
| SetHealthCenter (Maybe HealthCenterId)
| SetVillage (Maybe VillageId)
| Tick Time.Posix
Expand Down
12 changes: 11 additions & 1 deletion client/src/elm/App/Ports.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
port module App.Ports exposing (..)

import App.Model exposing (MemoryQuota, StorageQuota)
import App.Model exposing (GPSCoordinates, MemoryQuota, StorageQuota)


{-| Saves PIN code entered by user, so that we can use it again if
Expand All @@ -14,6 +14,11 @@ port cachePinCode : String -> Cmd msg
port setLanguage : String -> Cmd msg


{-| Requests GPS coordinates.
-}
port getCoordinates : () -> Cmd msg


{-| Let the Javascript tell us if we've successfully requested persistent
storage.
-}
Expand All @@ -30,6 +35,11 @@ port memoryQuota : (MemoryQuota -> msg) -> Sub msg
port storageQuota : (StorageQuota -> msg) -> Sub msg


{-| Let the Javascript tell us about our GPS coordinates.
-}
port coordinates : (GPSCoordinates -> msg) -> Sub msg


{-| Saves Health center ID selected by user, so that we can use it again if
the browser is reloaded.
-}
Expand Down
17 changes: 16 additions & 1 deletion client/src/elm/App/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ update msg model =
Backend.Update.updateIndexedDb model.language
currentDate
model.currentTime
model.coordinates
model.zscores
site
features
Expand Down Expand Up @@ -1097,6 +1098,14 @@ update msg model =
cmd =
Nav.pushUrl model.navigationKey (Url.toString redirectUrl)

extraCmd =
case page of
UserPage (CreatePersonPage _ _) ->
App.Ports.getCoordinates ()

_ ->
Cmd.none

extraMsgs =
case page of
-- When navigating to Device page (which is used for Sync management), trigger Sync.
Expand Down Expand Up @@ -1128,7 +1137,7 @@ update msg model =
[]
in
( { model | activePage = page }
, cmd
, Cmd.batch [ cmd, extraCmd ]
)
|> sequence update extraMsgs

Expand All @@ -1147,6 +1156,11 @@ update msg model =
, Cmd.none
)

SetGPSCoordinates coordinates ->
( { model | coordinates = Just coordinates }
, Cmd.none
)

SetStorageQuota quota ->
( { model | storageQuota = Just quota }
, Cmd.none
Expand Down Expand Up @@ -1532,6 +1546,7 @@ subscriptions model =
, persistentStorage SetPersistentStorage
, storageQuota SetStorageQuota
, memoryQuota SetMemoryQuota
, coordinates SetGPSCoordinates
, Sub.map App.Model.MsgSyncManager (SyncManager.Update.subscriptions model.syncManager)
]
++ checkDataWanted
Expand Down
2 changes: 2 additions & 0 deletions client/src/elm/Backend/Person/Decoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ decodePerson =
|> optional "sector" (nullable decodeGeoField) Nothing
|> optional "cell" (nullable decodeGeoField) Nothing
|> optional "village" (nullable decodeGeoField) Nothing
|> optional "latitude" (nullable string) Nothing
|> optional "longitude" (nullable string) Nothing
|> optional "phone_number" (nullable string) Nothing
|> optional "health_center" (nullable decodeEntityUuid) Nothing
|> required "deleted" bool
Expand Down
2 changes: 2 additions & 0 deletions client/src/elm/Backend/Person/Encoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ encodePerson person =
, ( "sector", maybe string person.sector )
, ( "cell", maybe string person.cell )
, ( "village", maybe string person.village )
, ( "latitude", maybe string person.registrationLatitude )
, ( "longitude", maybe string person.registrationLongitude )
, ( "phone_number", maybe string person.telephoneNumber )
, ( "health_center", maybe encodeEntityUuid person.healthCenterId )
, ( "deleted", bool person.deleted )
Expand Down
4 changes: 4 additions & 0 deletions client/src/elm/Backend/Person/Form.elm
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ validatePerson site maybeRelated operation maybeCurrentDate =
|> andMap (field sector (validateSector geoInfo maybeRelated))
|> andMap (field cell (validateCell geoInfo maybeRelated))
|> andMap (field village (validateVillage geoInfo maybeRelated))
|> andMap (succeed Nothing)
|> andMap (succeed Nothing)
|> andMap (field phoneNumber <| nullable validateDigitsOnly)
|> andMap (field healthCenter (validateHealthCenterId maybeRelated))
|> andMap (succeed False)
Expand Down Expand Up @@ -468,6 +470,8 @@ validateContact site =
|> andMap (field sector (validateSectorForContact geoInfo))
|> andMap (field cell (validateCellForContact geoInfo))
|> andMap (field village (validateVillageForContact geoInfo))
|> andMap (succeed Nothing)
|> andMap (succeed Nothing)
|> andMap (field phoneNumber <| nullable validateDigitsOnly)
|> andMap (succeed Nothing)
|> andMap (succeed False)
Expand Down
2 changes: 2 additions & 0 deletions client/src/elm/Backend/Person/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type alias Person =
, sector : Maybe String
, cell : Maybe String
, village : Maybe String
, registrationLatitude : Maybe String
, registrationLongitude : Maybe String
, telephoneNumber : Maybe String
, healthCenterId : Maybe HealthCenterId
, deleted : Bool
Expand Down
22 changes: 20 additions & 2 deletions client/src/elm/Backend/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ updateIndexedDb :
Language
-> NominalDate
-> Time.Posix
-> Maybe App.Model.GPSCoordinates
-> ZScore.Model.Model
-> Site
-> EverySet SiteFeature
Expand All @@ -166,7 +167,7 @@ updateIndexedDb :
-> MsgIndexedDb
-> ModelIndexedDb
-> ( ModelIndexedDb, Cmd MsgIndexedDb, List App.Model.Msg )
updateIndexedDb language currentDate currentTime zscores site features nurseId healthCenterId villageId isChw isLabTech activePage syncManager msg model =
updateIndexedDb language currentDate currentTime coordinates zscores site features nurseId healthCenterId villageId isChw isLabTech activePage syncManager msg model =
let
noChange =
( model, Cmd.none, [] )
Expand Down Expand Up @@ -328,6 +329,7 @@ updateIndexedDb language currentDate currentTime zscores site features nurseId h
(updateIndexedDb language
currentDate
currentTime
coordinates
zscores
site
features
Expand Down Expand Up @@ -1474,6 +1476,7 @@ updateIndexedDb language currentDate currentTime zscores site features nurseId h
(updateIndexedDb language
currentDate
currentTime
coordinates
zscores
site
features
Expand Down Expand Up @@ -4141,6 +4144,7 @@ updateIndexedDb language currentDate currentTime zscores site features nurseId h
(updateIndexedDb language
currentDate
currentTime
coordinates
zscores
site
features
Expand Down Expand Up @@ -4196,8 +4200,21 @@ updateIndexedDb language currentDate currentTime zscores site features nurseId h
)

PostPerson relation initiator person ->
let
-- Adding GPS coordinates.
personWithCoordinates =
Maybe.map
(\coords ->
{ person
| registrationLatitude = String.fromFloat coords.latitude |> Just
, registrationLongitude = String.fromFloat coords.longitude |> Just
}
)
coordinates
|> Maybe.withDefault person
in
( { model | postPerson = Loading }
, sw.post personEndpoint person
, sw.post personEndpoint personWithCoordinates
|> toCmd (RemoteData.fromResult >> RemoteData.map Tuple.first >> HandlePostedPerson relation initiator)
, []
)
Expand Down Expand Up @@ -4355,6 +4372,7 @@ updateIndexedDb language currentDate currentTime zscores site features nurseId h
(updateIndexedDb language
currentDate
currentTime
coordinates
zscores
site
features
Expand Down
17 changes: 17 additions & 0 deletions client/src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,23 @@ elmApp.ports.scrollToElement.subscribe(function(elementId) {
waitForElement(elementId, scrollToElement, null);
});

elmApp.ports.getCoordinates.subscribe(function() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
const result = {latitude: latitude, longitude: longitude};
elmApp.ports.coordinates.send(result);
},
(error) => {
console.error("Error fetching location:", error);
}
);
} else {
console.error("Geolocation is not available.");
}
});


function scrollToElement(elementId) {
var element = document.getElementById(elementId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,50 @@ function hedley_person_field_default_field_bases() {
'type' => 'text',
);

// Exported field_base: 'field_latitude'.
$field_bases['field_latitude'] = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_id_type' => NULL,
'entity_types' => array(),
'field_name' => 'field_latitude',
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);

// Exported field_base: 'field_longitude'.
$field_bases['field_longitude'] = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_id_type' => NULL,
'entity_types' => array(),
'field_name' => 'field_longitude',
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);

// Exported field_base: 'field_marital_status'.
$field_bases['field_marital_status'] = array(
'active' => 1,
Expand Down
Loading