state = {
selectedPatient : {
patientAllergiesID :
[{id: "123"},
{id: "124"}
]},
patientAllergies : [],
}
export const getPatientAllergies = createAsyncThunk(
"selectedPatient/getPatientAllergies",
async (arg, { getState }) => {
const state = getState();
const patient = state.selectedPatient.patient;
const token = state.userDetails.token;
try {
const apiUrl = process.env.REACT_APP_API_URL;
var config = {
method: "get",
url: `${apiUrl}/GetAllergyDetail/${patient}/${allergyID}`,
This is what the API url should look like. I want it to run for every patientAllergiesID for the selectedPatient and return an array with allergies. I don't know how to do that.
headers: {
accept: "application/json",
"Authorization-Token": token,
},
};
const response = await axios(config);
const data = await response.data;
return data;
} catch (error) {
console.log(error);
}
}
);
After this runs, I want to get:
state = {
selectedPatient : {
patientAllergiesID :
[{id: "123"},
{id: "124"}
]},
patientAllergies : [
{id: "123", allergy: "allergy1"},
{id: "124", allergy: "allergy2"],
}
But no mather what I do, I don't get past pushing only one id to the URL and getting only one object inside the state.patientAllergies array.
What I have tried:
try {
const apiUrl = process.env.REACT_APP_API_URL;
const fetchData = async (id) => {
var config = {
method: "get",
url: `${apiUrl}/GetAllergyDetail/${patient}/${id}`,
headers: {
accept: "application/json",
"Authorization-Token": token,
},
};
const response = await axios(config);
const data = await response.data;
return data;
};
const getCondtionsSync = async () => {
for (const id of allergyID) {
const result = await fetchData(id);
return result;
}
};
console.log(getCondtionsSync());
return getCondtionsSync();
} catch (error) {
console.log(error);
}
}