Skip to main content

Local storage

Due to the connectivity issues with users local storage is used throughout the app. It is used to store user profile data and session data.

For example the user's ID and other profile data once authenticated via Supabase is added to local storage:

/pages/index.js

...

async function getProfile() {
try {
const user = session.user;

let { data, error, status } = await supabase
.from("profiles")
.select("*")
.eq("id", user.id)
.single();

if (error && status !== 406) {
throw error;
}

if (data) {
typeof window !== "undefined" &&
localStorage.setItem("yb-user-id", JSON.stringify(data.id));

...

Local storage variables are then retrieved where needed within the pages and components.

/pages/en/favourites.js

...

// user_id
const ybUserIdTmp =
typeof window !== "undefined" && localStorage.getItem("yb-user-id");
const pdUserId = JSON.parse(ybUserIdTmp);


For example this data can be used to perform a screening offline and passing in these local variables as the user's data.

other local storage variable examples

...

// user_name
const ybUserNameTmp =
typeof window !== "undefined" && localStorage.getItem("yb-user-name");
const pdUserName = JSON.parse(ybUserNameTmp);

// user_role
const ybUserRoleTmp =
typeof window !== "undefined" && localStorage.getItem("yb-user-role");
const pdUserRole = JSON.parse(ybUserRoleTmp);

// user_gender
const ybUserGenderTmp =
typeof window !== "undefined" && localStorage.getItem("yb-user-gender");
const pdUserGender = JSON.parse(ybUserGenderTmp);

// user_province
const ybUserProvinceTmp =
typeof window !== "undefined" && localStorage.getItem("yb-user-province");
const pdUserProvince = JSON.parse(ybUserProvinceTmp);

// user_district
const ybUserDistrictTmp =
typeof window !== "undefined" && localStorage.getItem("yb-user-district");
const pdUserDistrict = JSON.parse(ybUserDistrictTmp);


Screening totals relating to targets is also kept in local storage

other local storage variable examples

...

// total screenings
const totalScreeningsUserTmp =
typeof window !== "undefined" &&
localStorage.getItem("yb-user-total-screenings");
const totalScreeningsUser = JSON.parse(totalScreeningsUserTmp);

// total screenings HIV
const totalScreeningsHIVTmp =
typeof window !== "undefined" &&
localStorage.getItem("yb-user-total-screenings-hiv");
const totalScreeningsUserHIV = JSON.parse(totalScreeningsHIVTmp);

// total screenings Malaria
const totalScreeningsMaariaTmp =
typeof window !== "undefined" &&
localStorage.getItem("yb-user-total-screenings-malaria");
const totalScreeningsUserMalaria = JSON.parse(totalScreeningsMaariaTmp);

// total screenings TB
const totalScreeningsTBTmp =
typeof window !== "undefined" &&
localStorage.getItem("yb-user-total-screenings-tb");
const totalScreeningsUserTB = JSON.parse(totalScreeningsTBTmp);

// user_project_id
const ybUserProjectIdTmp =
typeof window !== "undefined" && localStorage.getItem("yb-user-project-id");
const pdUserProjectId = JSON.parse(ybUserProjectIdTmp);