Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 👽️ remove doctor id #119

Merged
merged 3 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/components/DoctorCard/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { memo } from 'react';
import slugify from 'slugify';
import CardContent from '@mui/material/CardContent';
import CardMedia from '@mui/material/CardMedia';

Expand All @@ -17,9 +18,11 @@ const DoctorCard = function DoctorCard({ doctor, isPage = false, isReportError }
map.flyTo([lat, lon], 13);
};

const id = `${doctor.type}-${slugify(doctor.name).toLowerCase()}`;

if (isPage) {
return (
<Styled.PageInfoCard id={doctor.id} accepts={accepts.toString()}>
<Styled.PageInfoCard id={id} accepts={accepts.toString()}>
<Styled.PageInfoBox id="doctor-box">
<PageInfo doctor={doctor} handleZoom={handleZoom} isReportError={isReportError} />
<CardContent>
Expand All @@ -33,11 +36,11 @@ const DoctorCard = function DoctorCard({ doctor, isPage = false, isReportError }
}

return (
<Styled.InfoCard id={doctor.id} accepts={accepts.toString()}>
<Styled.InfoCard id={id} accepts={accepts.toString()}>
<Info doctor={doctor} handleZoom={handleZoom} />
</Styled.InfoCard>
);
};

const propsAreEqual = (prevProps, nextProps) => prevProps.doctor.id === nextProps.doctor.id;
const propsAreEqual = (prevProps, nextProps) => prevProps.doctor.key === nextProps.doctor.key;
export default memo(DoctorCard, propsAreEqual);
2 changes: 1 addition & 1 deletion src/components/Doctors/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function withLeaflet(Component) {
userLocation = false,
...other
}) {
const markers = doctors?.map(doctor => <Markers.Doctor key={doctor.id} doctor={doctor} />);
const markers = doctors?.map(doctor => <Markers.Doctor key={doctor.key} doctor={doctor} />);
const injectedProps = {
center,
zoom,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Doctors/Markers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const PopUpData = function PopUpData({ doctor }) {
);
};

const areEqual = (prevProps, nextProps) => prevProps.doctor.id === nextProps.doctor.id;
const areEqual = (prevProps, nextProps) => prevProps.doctor.key === nextProps.doctor.key;
export const Doctor = memo(({ doctor }) => {
const ref = createRef(null);
const theme = useTheme();
Expand Down
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
23 changes: 11 additions & 12 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,15 +36,11 @@ export function createDoctor(doctor, type, institution) {
post: _post,
};

const uuid = uuidv4();
const { availability, load } = doctor;

return Object.freeze({
get key() {
return uuid;
},
get id() {
return doctor.id;
return doctor.key;
},
get type() {
return doctor.type;
Expand Down Expand Up @@ -88,19 +83,24 @@ 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;
}, {});

const getById = id => doctors[`${id}`];

const doctorValues = Object.values(doctors);
const doctorsValues = Intl.Collator
? doctorValues.sort((a, b) => new Intl.Collator('sl').compare(a.name, b.name))
Expand All @@ -125,7 +125,6 @@ export default function createDoctors(doctorsDict, institutionsDict, typesDict)

return Object.freeze({
all: doctorsValues,
getById,
types,
filterByType,
typesDict,
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