Skip to main content

edit-user

Adding a user

  • localhost:3000/edituser

Users are edited with Supabase using the client import { supabase } from "@utils/supabaseClient";

User data is edited using a function to retrieve the user's data and a further function to update any of the fields that are changed

getUser function

src/pages/edituser.js
async function getUser(id) {
try {
setLoading(true);

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

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

if (data) {
const result = data.DOB.split("-");
const [year1, month1, day1] = result;

setYear({
entry: year1,
});

setMonth({
entry: month1,
});

setDay({
entry: day1,
});

setUserId(data.id);

setMobileNumber({
entry: data.mobile_number,
});

setName({
entry: data.name,
});

setGender(data.gender);

setRole(data.role);

setProvince(data.province);

setDistrict(data.district);

setHealthFacility(data.health_facility);

setCountry(data.country);
}
} catch (error) {
// console.log("user not logged in");
} finally {
setLoading(false);
}
}

addUserProfile function

src/pages/edituser.js
async function addUserProfile(dataSent, userData) {
try {
setLoading(true);

const dataEnter = {
id: dataSent.user.user.id,
name: userData.name,
gender: userData.gender,
role: userData.role,
province: userData.province,
district: userData.district,
health_facility: userData.health_facility,
mobile_number: userData.mobile_number,
admin_user: false,
};

const { data, error } = await supabase
.from("profiles")
.insert(dataEnter)
.select();

if (error) {
throw error;
}
} catch (error) {
alert(error.message);
} finally {
setLoading(false);
}
}