diff --git a/src/context/doctorsContext.js b/src/context/doctorsContext.js index a208c70d..afee2521 100644 --- a/src/context/doctorsContext.js +++ b/src/context/doctorsContext.js @@ -31,10 +31,17 @@ const DoctorsProvider = function DoctorsProvider({ children }) { useEffect(() => { if (doctorsFetched) { const doctorsDict = fromArrayWithHeader(doctorsRequest.parsed); - const institutionsDict = fromArrayWithHeader(institutionsRequest.parsed); - const typesDict = fromArrayWithHeader(doctorTypesRequest.parsed); + const institutionsDict = fromArrayWithHeader(institutionsRequest.parsed, 'id_inst'); + const typesDict = fromArrayWithHeader(doctorTypesRequest.parsed, 'id'); - setDoctors(createDoctors(doctorsDict, institutionsDict, typesDict)); + setDoctors( + createDoctors({ + doctorsDict, + institutionsDict, + typesDict, + keys: { instKey: 'id_inst', typeKey: 'type' }, + }), + ); setDicts({ doctors: doctorsDict, institutions: institutionsDict, types: typesDict }); } }, [ diff --git a/src/services/doctors.js b/src/services/doctors.js index 3f0e687d..3aea718e 100644 --- a/src/services/doctors.js +++ b/src/services/doctors.js @@ -1,5 +1,4 @@ import { t } from 'i18next'; -import { v4 as uuidv4 } from 'uuid'; const trimString = str => str.replace(/\s+/g, ' ').trim(); @@ -37,12 +36,11 @@ export function createDoctor(doctor, type, institution) { post: _post, }; - const uuid = uuidv4(); const { availability, load } = doctor; return Object.freeze({ get key() { - return uuid; + return doctor.key; }, get type() { return doctor.type; @@ -85,12 +83,19 @@ export function createDoctor(doctor, type, institution) { }); } -export default function createDoctors(doctorsDict, institutionsDict, typesDict) { +export default function createDoctors({ + doctorsDict, + institutionsDict, + typesDict, + keys = { instKey: 'id_inst', typesKey: 'type' }, +}) { + const { instKey, typeKey } = keys; + const doctors = Object.entries(doctorsDict).reduce((acc, [doctorId, doctorData]) => { const doctor = createDoctor( doctorData, - typesDict[doctorData.type], - institutionsDict[doctorData.id_inst], + typesDict[doctorData[typeKey]], + institutionsDict[doctorData[instKey]], ); acc[doctorId] = doctor; return acc; diff --git a/src/utils/index.js b/src/utils/index.js index ddd5e7d6..1766d188 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,7 +1,14 @@ import L from 'leaflet'; +import { v4 as uuidv4 } from 'uuid'; -export function fromArrayWithHeader(arr = []) { +export function fromArrayWithHeader(arr = [], uniqueFieldName = '') { const header = arr[0]; + + // it's dirty but quick fix while doctor's id might to be available in the future. + const idIndex = header.findIndex(item => item === uniqueFieldName); + if (uniqueFieldName && idIndex === -1) + throw new Error(`Field "${uniqueFieldName}" does not exist!`); + const data = arr.slice(1, -1); return data.reduce((acc1, dataItem) => { const type = dataItem.reduce( @@ -11,9 +18,15 @@ export function fromArrayWithHeader(arr = []) { }), {}, ); + + const key = idIndex !== -1 ? dataItem[idIndex] : uuidv4(); + + if (idIndex === -1) { + type.key = key; + } return { ...acc1, - [dataItem[0]]: type, + [key]: type, }; }, {}); }