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

Bug/anson/228 #229

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions components/records/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { useRef, useState } from 'react';
import toast from 'react-hot-toast';
import moment from 'moment';
import { ChevronDownIcon } from '@heroicons/react/24/outline';
import { CLOUDINARY_URL } from '@/utils/constants';
import { Button } from '../TextComponents';
import axiosInstance from '@/pages/api/_axiosInstance';
import useWithLoading from '@/utils/loading';
import CustomModal from '../CustomModal';
import { FileForm } from './FileForm';
import { getImageUrl } from '@/utils/helpers';

export function Header({ patient, visits, handleVisitChange }) {
const fileInputRef = useRef(null);
Expand Down Expand Up @@ -69,7 +69,7 @@ export function Header({ patient, visits, handleVisitChange }) {
<div className="grid grid-cols-12 gap-4 mb-2">
<div className="col-span-12 md:col-span-2">
<img
src={`${CLOUDINARY_URL}/${patient.picture}`}
src={getImageUrl(patient)}
alt="Patient"
className="h-48 w-48 object-cover rounded-md"
/>
Expand Down
10 changes: 2 additions & 8 deletions components/registration/PatientInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import moment from 'moment';

import { DisplayField, Button } from '@/components/TextComponents/';
import { CLOUDINARY_URL, OFFLINE, defaultAPI_URL } from '@/utils/constants';
import { getImageUrl } from '@/utils/helpers';

export function PatientInfo({ patient, submitNewVisit }) {
if (!patient.pk) {
Expand All @@ -25,17 +25,11 @@ export function PatientInfo({ patient, submitNewVisit }) {
{ label: 'Allergies', key: 'drug_allergy' },
];

const imageUrl = OFFLINE
? `${defaultAPI_URL}/${patient.offline_picture}`
: `${CLOUDINARY_URL}/${patient.picture}`;

console.log(OFFLINE);

return (
<div>
<div>
<img
src={imageUrl}
src={getImageUrl(patient)}
alt="Placeholder image"
className="has-ratio h-48 w-48 object-cover"
/>
Expand Down
8 changes: 2 additions & 6 deletions components/registration/RegistrationAutoSuggest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import Autosuggest from 'react-autosuggest';
import { CLOUDINARY_URL, OFFLINE, defaultAPI_URL } from '@/utils/constants';
import { getImageUrl } from '@/utils/helpers';

export function RegistrationAutoSuggest({ setPatient, patientsList }) {
const [value, setValue] = useState('');
Expand All @@ -11,18 +11,14 @@ export function RegistrationAutoSuggest({ setPatient, patientsList }) {
const id = `${suggestion.village_prefix} ${suggestion.pk
.toString()
.padStart(3, '0')}`;
const imageUrl = OFFLINE
? `${defaultAPI_URL}/${suggestion.offline_picture}`
: `${CLOUDINARY_URL}/${suggestion.picture}`;

return (
<div
className="card cursor-pointer grid grid-cols-2"
onClick={() => setPatient(suggestion)}
>
<div className="self-center">
<img
src={imageUrl}
src={getImageUrl(suggestion)}
alt="Placeholder image"
className="object-cover h-28 w-28 my-2"
/>
Expand Down
4 changes: 2 additions & 2 deletions pages/pharmacy/orders/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useEffect, useState } from 'react';
import moment from 'moment';
import { CLOUDINARY_URL } from '@/utils/constants';
import withAuth from '@/utils/auth';
import { Button, InputField, PageTitle } from '@/components/TextComponents';
import axiosInstance from '@/pages/api/_axiosInstance';
import toast from 'react-hot-toast';
import useWithLoading from '@/utils/loading';
import { VILLAGE_COLOR_CLASSES } from '@/utils/constants';
import { getImageUrl } from '@/utils/helpers';

const Orders = () => {
const [orders, setOrders] = useState([]);
Expand Down Expand Up @@ -97,7 +97,7 @@ const Orders = () => {
</td>
<td className="whitespace-nowrap px-3 py-4">
<img
src={`${CLOUDINARY_URL}/${visit.patient.picture}`}
src={getImageUrl(visit.patient)}
alt="Patient"
className="object-cover h-28 w-28 rounded-lg"
/>
Expand Down
7 changes: 2 additions & 5 deletions pages/records/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useState, useEffect } from 'react';
import Router from 'next/router';
import toast from 'react-hot-toast';
import { CLOUDINARY_URL, OFFLINE, defaultAPI_URL } from '@/utils/constants';
import withAuth from '@/utils/auth';
import { Button, InputField } from '@/components/TextComponents';
import axiosInstance from '@/pages/api/_axiosInstance';
import { VENUE_OPTIONS } from '@/utils/constants';
import useWithLoading from '@/utils/loading';
import { VILLAGE_COLOR_CLASSES } from '@/utils/constants';
import { getImageUrl } from '@/utils/helpers';

function VillageDropdown({ handleDropdownChangeWithStyle, PATIENT_CODE_ALL }) {
return (
Expand Down Expand Up @@ -122,9 +122,6 @@ function PatientList() {
.slice(startIndex, endIndex)
.map(patient => {
const patientID = patient.patient_id;
const imageUrl = OFFLINE
? `${defaultAPI_URL}/${patient.offline_picture}`
: `${CLOUDINARY_URL}/${patient.picture}`;

const patientVillagePrefix = patient.village_prefix;
const fullName = patient.name;
Expand Down Expand Up @@ -168,7 +165,7 @@ function PatientList() {
</td>
<td>
<img
src={imageUrl}
src={getImageUrl(patient)}
alt="Placeholder image"
className="object-cover h-28 w-28 my-2"
/>
Expand Down
26 changes: 26 additions & 0 deletions utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CLOUDINARY_URL, defaultAPI_URL, OFFLINE } from './constants';

// convert base64 url screenshot to file
export function urltoFile(url, filename, mimeType) {
return fetch(url)
Expand All @@ -8,3 +10,27 @@ export function urltoFile(url, filename, mimeType) {
return new File([buf], filename, { type: mimeType });
});
}

/**
* This function returns the relevant image url based on whether it is offline.
*
* @param {pictureData} data - must contain either `.offline_picture` or `.picture`
* @return {String} the image url
*
* @typedef {Object} pictureData
* @property {String|null} data.offline_picture
* @property {String|null} data.picture
*/
export function getImageUrl(data) {
if (data.offline_picture == null && data.picture == null) {
console.error(
`Invalid data is passed.
Attribute 'offline_picture'/'picture' are both null.
Data is:\n`,
data
);
}
return OFFLINE && data.offline_picture != null
? `${defaultAPI_URL}/${data.offline_picture}`
: `${CLOUDINARY_URL}/${data.picture}`;
}
Loading