Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | 27x 27x 27x | import React, { useEffect, useState, useContext } from 'react';
import PropTypes from 'prop-types';
import { ConfirmationModal } from '@folio/stripes/components';
import { CalloutContext } from '@folio/stripes/core';
import { useKintIntl, useMutateRefdataValue, useRefdata } from '../../hooks';
import ActionList from '../../ActionList';
import { required } from '../../validators';
import { parseErrorResponse, selectorSafe } from '../../utils';
const propTypes = {
afterQueryCalls: PropTypes.object,
allowSpecial: PropTypes.bool,
catchQueryCalls: PropTypes.object,
desc: PropTypes.string,
displayConditions: PropTypes.shape({
create: PropTypes.bool,
delete: PropTypes.bool,
view: PropTypes.bool,
}),
intlKey: PropTypes.string,
intlNS: PropTypes.string,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node
]),
labelOverrides: PropTypes.object,
refdataEndpoint: PropTypes.string
};
const EditableRefdataList = ({
afterQueryCalls,
allowSpecial = false, // special characters will be directly stripped out of the value before it is sent to the backend
catchQueryCalls,
desc,
/*
* Set of extra booleans for controlling access to actions
* edit/create/delete (View should be handled externally)
* This will not overwrite "internal" behaviour, ie setting
* delete to 'true' here would still not render a delete button
* for an internal refdata value
*/
displayConditions = {
create: true,
edit: true,
delete: true,
},
intlKey: passedIntlKey,
intlNS: passedIntlNS,
label,
labelOverrides = {}, // An object containing translation alternatives
refdataEndpoint
}) => {
const {
create: createCondition = true,
delete: deleteCondition = true,
edit: editCondition = true
} = displayConditions;
/* A component that allows for editing of refdata values */
const callout = useContext(CalloutContext);
const kintIntl = useKintIntl(passedIntlKey, passedIntlNS);
// fetch refdata values
const { data: { 0: refdata } = {}, isLoading: isRefdataLoading } = useRefdata({
desc,
endpoint: refdataEndpoint,
returnQueryObject: true
});
const [contentData, setContentData] = useState([]);
const [deleteModal, setDeleteModal] = useState({
visible: false,
refdata: null,
});
const sortByLabel = (a, b) => (a.label.localeCompare(b.label));
useEffect(() => {
if (!isRefdataLoading) {
setContentData(refdata?.values?.sort(sortByLabel) ?? []);
}
}, [isRefdataLoading, refdata]);
// Edit and Create will use the same PUT mutation
// TODO I believe after the changes to refactor to useMutateGeneric, we can now use POST separately to get differing callouts etc
const { delete: deleteRefdataValue, put: editRefdataValue } = useMutateRefdataValue({
afterQueryCalls: {
delete: json => {
setContentData(json?.values?.sort(sortByLabel) ?? []);
if (afterQueryCalls?.delete) {
afterQueryCalls.delete(json);
}
},
put: json => {
setContentData(json?.values?.sort(sortByLabel) ?? []);
if (afterQueryCalls?.put) {
afterQueryCalls.put(json);
}
}
},
catchQueryCalls: {
// Default delete behaviour is to fire a callout, either with kint-components default message
// or one provided in labelOverrides, which is passed the error message and refdata in question
delete: async (err) => {
const errorResp = await parseErrorResponse(err.response);
// console.log('ERRORRESP: %o', errorResp);
callout.sendCallout({
message: kintIntl.formatKintMessage({
id: 'refdata.deleteRefdataValue.errorMessage',
overrideValue: labelOverrides?.deleteError
},
{
label: deleteModal?.refdata?.label,
error: errorResp?.message
}),
type: 'error',
});
},
...catchQueryCalls // override defaults here
},
endpoint: refdataEndpoint,
id: refdata?.id,
queryParams: {
delete: {
enabled: !!refdata
},
put: {
enabled: !!refdata
}
}
});
if (isRefdataLoading) {
return 'loading';
}
// This is the function which will take a row in the table and assign the relevant actions to it
const actionAssigner = () => {
const actionArray = [];
if (editCondition) {
actionArray.push(
{
name: 'edit',
label: kintIntl.formatKintMessage({
id: 'edit',
overrideValue: labelOverrides?.edit
}),
icon: 'edit',
callback: (data) => editRefdataValue(data),
ariaLabel: (data) => kintIntl.formatKintMessage(
{
id: 'refdata.editAriaLabel',
overrideValue: labelOverrides?.editAriaLabel
},
{ label: data?.label }
),
}
);
}
if (!refdata?.internal && deleteCondition) {
actionArray.push({
name: 'delete',
label: kintIntl.formatKintMessage({
id: 'delete',
overrideValue: labelOverrides?.delete
}),
icon: 'trash',
callback: (data) => setDeleteModal({ visible: true, refdata: data }),
ariaLabel: (data) => kintIntl.formatKintMessage(
{
id: 'refdata.deleteAriaLabel',
overrideValue: labelOverrides?.deleteAriaLabel
},
{ label: data?.label }
),
});
}
return actionArray;
};
return (
<>
<ActionList
actionAssigner={actionAssigner}
columnMapping={{
label: kintIntl.formatKintMessage({
id: 'refdata.label',
overrideValue: labelOverrides?.label
}),
value: kintIntl.formatKintMessage({
id: 'refdata.value',
overrideValue: labelOverrides?.value
}),
}}
contentData={contentData}
creatableFields={{
value: () => false
}}
createCallback={
(!createCondition || refdata?.internal) ?
null :
(data) => {
if (allowSpecial) {
editRefdataValue(data);
} else {
editRefdataValue({ ...data, value: selectorSafe(data?.label)?.replaceAll('%20', ' ') });
}
}
}
editableFields={{
value: () => false
}}
hideActionsColumn={!deleteCondition && !editCondition}
hideCreateButton={!createCondition}
label={label}
validateFields={{
label: required
}}
visibleFields={['label', 'value']}
/>
<ConfirmationModal
confirmLabel={
kintIntl.formatKintMessage({
id: 'delete',
overrideValue: labelOverrides?.delete
})
}
heading={
kintIntl.formatKintMessage({
id: 'refdata.deleteRefdataValue',
overrideValue: labelOverrides?.deleteRefdataValue
})
}
message={
kintIntl.formatKintMessage({
id: 'refdata.deleteRefdataValue.confirmMessage',
overrideValue: labelOverrides?.deleteRefdataValueMessage
}, { name: deleteModal?.refdata?.label })
}
onCancel={() => setDeleteModal({ visible: false, refdata: null })}
onConfirm={() => {
deleteRefdataValue(deleteModal?.refdata?.id);
setDeleteModal({ visible: false, refdata: null });
}}
open={deleteModal?.visible}
/>
</>
);
};
EditableRefdataList.propTypes = propTypes;
export default EditableRefdataList;
|