Skip to content

Commit

Permalink
fix: 👽️ use uuid as key for doctors dict
Browse files Browse the repository at this point in the history
It's ditry solution but will do for now.

BREAKING CHANGE: #115
  • Loading branch information
jalezi committed Dec 20, 2021
1 parent 87348b8 commit 7c3eb86
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/context/doctorsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}, [
Expand Down
17 changes: 11 additions & 6 deletions src/services/doctors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { t } from 'i18next';
import { v4 as uuidv4 } from 'uuid';

const trimString = str => str.replace(/\s+/g, ' ').trim();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 15 additions & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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,
};
}, {});
}
Expand Down

0 comments on commit 7c3eb86

Please sign in to comment.