|
| 1 | +import React, { useState } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import urlPropType from "url-prop-type"; |
| 4 | + |
| 5 | +import ChecklistMaterialButton from "./checklist-material-button"; |
| 6 | +import MaterialList from "../../core/MaterialList"; |
| 7 | + |
| 8 | +function ChecklistMaterialButtonEntry({ |
| 9 | + materialListUrl, |
| 10 | + addText, |
| 11 | + addSuccessText, |
| 12 | + addErrorText, |
| 13 | + removeText, |
| 14 | + removeSuccessText, |
| 15 | + removeErrorText, |
| 16 | + id, |
| 17 | + loginUrl, |
| 18 | + initialOnList, |
| 19 | + containerClass |
| 20 | +}) { |
| 21 | + const [status, setStatus] = useState("ready"); |
| 22 | + const [onList, setOnList] = useState(initialOnList); |
| 23 | + |
| 24 | + function setRestoreStatus(newOnList) { |
| 25 | + setOnList(newOnList); |
| 26 | + setStatus("ready"); |
| 27 | + } |
| 28 | + |
| 29 | + function setListErrorStatus() { |
| 30 | + setStatus("failed"); |
| 31 | + setTimeout(() => { |
| 32 | + setRestoreStatus("unknown"); |
| 33 | + }, 4000); |
| 34 | + } |
| 35 | + |
| 36 | + function addToList() { |
| 37 | + setStatus("processing"); |
| 38 | + |
| 39 | + const client = new MaterialList({ baseUrl: materialListUrl }); |
| 40 | + client |
| 41 | + .addListMaterial({ materialId: id }) |
| 42 | + .then(() => { |
| 43 | + setTimeout(() => { |
| 44 | + setRestoreStatus("on"); |
| 45 | + }, 4000); |
| 46 | + }) |
| 47 | + .catch(setListErrorStatus); |
| 48 | + } |
| 49 | + |
| 50 | + function removeFromList() { |
| 51 | + setStatus("processing"); |
| 52 | + |
| 53 | + const client = new MaterialList({ baseUrl: materialListUrl }); |
| 54 | + client |
| 55 | + .deleteListMaterial({ materialId: id }) |
| 56 | + .then(() => { |
| 57 | + setTimeout(() => { |
| 58 | + setRestoreStatus("off"); |
| 59 | + }, 4000); |
| 60 | + }) |
| 61 | + .catch(setListErrorStatus); |
| 62 | + } |
| 63 | + |
| 64 | + let onClick = addToList; |
| 65 | + let text = addText; |
| 66 | + let errorText = addErrorText; |
| 67 | + let successText = addSuccessText; |
| 68 | + if (onList === "on") { |
| 69 | + onClick = removeFromList; |
| 70 | + text = removeText; |
| 71 | + errorText = removeErrorText; |
| 72 | + successText = removeSuccessText; |
| 73 | + } |
| 74 | + |
| 75 | + if (status === "ready" && onList === "unknown") { |
| 76 | + const client = new MaterialList({ baseUrl: materialListUrl }); |
| 77 | + client |
| 78 | + .checkListMaterial({ materialId: id }) |
| 79 | + .then(listMaterial => { |
| 80 | + setOnList(listMaterial ? "on" : "off"); |
| 81 | + }) |
| 82 | + // eslint-disable-next-line no-unused-vars |
| 83 | + .catch(err => { |
| 84 | + // Do nothing. If the call fails then we show the add button by default. |
| 85 | + // If this is a permanent error then clicking the button will trigger an |
| 86 | + // error. If this is a temporary error and the user clicks the button |
| 87 | + // then the material will not be added multiple times and thus cause |
| 88 | + // further problems. |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + return ( |
| 93 | + <ChecklistMaterialButton |
| 94 | + text={text} |
| 95 | + errorText={errorText} |
| 96 | + successText={successText} |
| 97 | + status={status} |
| 98 | + onClick={onClick} |
| 99 | + loginUrl={loginUrl} |
| 100 | + materialId={id} |
| 101 | + containerClass={containerClass} |
| 102 | + /> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +ChecklistMaterialButtonEntry.propTypes = { |
| 107 | + materialListUrl: urlPropType, |
| 108 | + addText: PropTypes.string, |
| 109 | + addErrorText: PropTypes.string, |
| 110 | + addSuccessText: PropTypes.string, |
| 111 | + removeText: PropTypes.string, |
| 112 | + removeErrorText: PropTypes.string, |
| 113 | + removeSuccessText: PropTypes.string, |
| 114 | + id: PropTypes.string.isRequired, |
| 115 | + loginUrl: urlPropType.isRequired, |
| 116 | + initialOnList: PropTypes.oneOf(["unknown", "on", "off"]), |
| 117 | + containerClass: PropTypes.string |
| 118 | +}; |
| 119 | + |
| 120 | +ChecklistMaterialButtonEntry.defaultProps = { |
| 121 | + materialListUrl: "https://test.materiallist.dandigbib.org", |
| 122 | + addText: "Tilføj til min liste", |
| 123 | + addErrorText: "Det lykkedes ikke at gemme materialet.", |
| 124 | + addSuccessText: "Materialet er tilføjet", |
| 125 | + removeText: "Fjern fra min liste", |
| 126 | + removeErrorText: "Det lykkedes ikke at fjerne materialet.", |
| 127 | + removeSuccessText: "Materialet er fjernet", |
| 128 | + initialOnList: "unknown", |
| 129 | + containerClass: "ddb-checklist-material-button__container" |
| 130 | +}; |
| 131 | + |
| 132 | +export default ChecklistMaterialButtonEntry; |
0 commit comments