Skip to content

Commit

Permalink
feat(dashboard,app): only healthcare professional (#543)
Browse files Browse the repository at this point in the history
* feat(dashboard,app): only healthcare professional

* sanitize updated field ?

* fix: table key

* fix: up

Co-authored-by: Arnaud AMBROSELLI <[email protected]>
  • Loading branch information
rap2hpoutre and Arnaud AMBROSELLI authored Mar 28, 2022
1 parent 099abc2 commit 64ff190
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/src/controllers/organisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ router.put(
enabled: z.optional(z.boolean()),
required: z.optional(z.boolean()),
showInStats: z.optional(z.boolean()),
onlyHealthcareProfessional: z.optional(z.boolean()),
options: z.optional(z.array(z.string())),
})
.strict();
Expand Down
4 changes: 3 additions & 1 deletion app/src/scenes/Persons/FileMedical.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import InputLabelled from '../../components/InputLabelled';
import colors from '../../utils/colors';
import CustomFieldInput from '../../components/CustomFieldInput';
import { customFieldsPersonsMedicalSelector } from '../../recoil/persons';
import { userState } from '../../recoil/auth';

const FileMedical = ({ navigation, editable, onChange, onUpdatePerson, onEdit, isUpdateDisabled, updating, backgroundColor, person }) => {
const customFieldsPersonsMedical = useRecoilValue(customFieldsPersonsMedicalSelector);

const user = useRecoilValue(userState);
const scrollViewRef = useRef(null);
const refs = useRef({});
const _scrollToInput = (ref) => {
Expand Down Expand Up @@ -49,6 +50,7 @@ const FileMedical = ({ navigation, editable, onChange, onUpdatePerson, onEdit, i
{(customFieldsPersonsMedical || [])
.filter((f) => f)
.filter((f) => f.enabled)
.filter((f) => !f.onlyHealthcareProfessional || user.healthcareProfessional)
.map((field) => {
const { label, name } = field;
return (
Expand Down
41 changes: 37 additions & 4 deletions dashboard/src/components/TableCustomFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useApi from '../services/api';
import ButtonCustom from './ButtonCustom';
import SelectCustom from './SelectCustom';
import Table from './table';
import QuestionMarkButton from './QuestionMarkButton';

const newField = () => ({
// Todo: I guess could use crypto here.
Expand All @@ -23,7 +24,7 @@ const newField = () => ({

const getValueFromType = (type) => typeOptions.find((opt) => opt.value === type);

const TableCustomFields = ({ data, customFields }) => {
const TableCustomFields = ({ data, customFields, showHealthcareProfessionnalColumn = false }) => {
const [isSubmitting, setIsSubmitting] = useState(false);
const [mutableData, setMutableData] = useState(data);
const [editingField, setEditingField] = useState(null);
Expand All @@ -41,6 +42,11 @@ const TableCustomFields = ({ data, customFields }) => {
setMutableData(mutableData.map((field) => (field.name !== fieldToUpdate.name ? field : { ...fieldToUpdate, showInStats })));
};

const onOnlyHealthcareProfessionalChange = (fieldToUpdate) => (event) => {
const onlyHealthcareProfessional = event.target.checked;
setMutableData(mutableData.map((field) => (field.name !== fieldToUpdate.name ? field : { ...fieldToUpdate, onlyHealthcareProfessional })));
};

const onSaveField = async (editedField) => {
const isUpdate = !!mutableData.find((f) => f.name === editedField.name);
const newData = isUpdate ? mutableData.map((field) => (field.name !== editedField.name ? field : editedField)) : [...mutableData, editedField];
Expand All @@ -59,7 +65,16 @@ const TableCustomFields = ({ data, customFields }) => {
};

const handleSubmit = async (newData) => {
if (!newData) newData = mutableData.filter((field) => !!field.label.length);
if (!newData)
newData = mutableData
.filter((field) => !!field.label.length)
.map((field) => {
const sanitizedField = {};
for (const key of Object.keys(field)) {
if (![undefined, null].includes(field[key])) sanitizedField[key] = field[key];
}
return sanitizedField;
});
setIsSubmitting(true);
try {
const response = await API.put({
Expand Down Expand Up @@ -137,12 +152,29 @@ const TableCustomFields = ({ data, customFields }) => {
render: (f) => <CellWrapper>{!['enum', 'multi-choice'].includes(f.type) ? null : (f?.options || []).join(', ')}</CellWrapper>,
},
{ title: 'Activé', dataKey: 'enabled', render: (f) => <input type="checkbox" checked={f.enabled} onChange={onEnabledChange(f)} /> },
!showHealthcareProfessionnalColumn
? null
: {
title: (
<div>
Professionnel
<br /> santé
<QuestionMarkButton
onClick={() => {
alert('Le champ ne sera visible que par les comptes de type "professionnel de santé" dans un onglet dédié.');
}}
/>
</div>
),
dataKey: 'onlyHealthcareProfessional',
render: (f) => <input type="checkbox" checked={f.onlyHealthcareProfessional} onChange={onOnlyHealthcareProfessionalChange(f)} />,
},
{
title: (
<>
Voir dans les
<br />
satistiques
statistiques
</>
),
dataKey: 'showInStats',
Expand All @@ -154,7 +186,8 @@ const TableCustomFields = ({ data, customFields }) => {
small: true,
render: (f) => <ButtonCustom title="Supprimer" loading={isSubmitting} onClick={() => onDelete(f)} width={75} color="danger" />,
},
]}
// Remove null fields
].filter((e) => e)}
/>
<ButtonsWrapper>
<ButtonCustom title="Ajouter un champ" loading={isSubmitting} onClick={() => setIsNewField(true)} width={200} />
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/components/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const Table = ({ columns = [], data = [], rowKey, onRowClick, nullDisplay = '',
<td
onClick={!!onSortBy ? onNameClick : null}
className={`column-header ${column.left && 'align-left'} ${!!onSortBy && 'clickable'}`}
key={column.title || dataKey}>
key={String(dataKey) + String(column.title)}>
<span>{column.title}</span>
{(sortBy === sortableKey || sortBy === dataKey) && (
<>
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/scenes/organisation/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ const View = () => {
<Row>
<TableCustomFields
customFields="customFieldsPersonsMedical"
showHealthcareProfessionnalColumn
key="customFieldsPersonsMedical"
data={(() => {
if (Array.isArray(organisation.customFieldsPersonsMedical)) return organisation.customFieldsPersonsMedical;
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/scenes/person/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ const Summary = ({ person }) => {
</Col>
{customFieldsPersonsMedical
.filter((f) => f.enabled)
.filter((f) => !f.onlyHealthcareProfessional || user.healthcareProfessional)
.map((field) => (
<CustomFieldInput model="person" values={values} handleChange={handleChange} field={field} key={field.name} />
))}
Expand Down

0 comments on commit 64ff190

Please sign in to comment.