Skip to content

Commit e106ca2

Browse files
committed
chore: refactor logic to only be in storage service
* storage information now only calculated if the feature is enabled, and now lives in dedicated quotaIncrease object * update docstrings * add all translations * fix: account for user having their own quota * hide storage menu if feature not enabled * fix: account for all versions of a record for min quota value calculation
1 parent f103688 commit e106ca2

File tree

11 files changed

+250
-165
lines changed

11 files changed

+250
-165
lines changed

invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/components/StorageSettings/StorageOverview.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function StorageOverview({ storage }) {
4141
<Message
4242
icon="hdd"
4343
size="tiny"
44-
header={<Header as="h4">{storage.default_quota} GB</Header>}
44+
header={<Header as="h4">{storage.default_quota}&nbsp;GB</Header>}
4545
content={i18next.t("Default quota per record")}
4646
className="rel-p-2"
4747
/>
@@ -53,7 +53,9 @@ export default function StorageOverview({ storage }) {
5353
size="tiny"
5454
positive={storage.additional_available_quota > 5}
5555
warning={storage.additional_available_quota < 5}
56-
header={<Header as="h4">{storage.additional_available_quota} GB</Header>}
56+
header={
57+
<Header as="h4">{storage.additional_available_quota}&nbsp;GB</Header>
58+
}
5759
content={i18next.t("Available of {{total}} GB allowance", {
5860
total: storage.total_allowed_quota,
5961
})}
@@ -66,7 +68,7 @@ export default function StorageOverview({ storage }) {
6668
icon="sitemap"
6769
size="tiny"
6870
info
69-
header={<Header as="h4">{storage.additional_granted_quota} GB</Header>}
71+
header={<Header as="h4">{storage.additional_granted_quota}&nbsp;GB</Header>}
7072
content={i18next.t("Quota granted across {{count}} records", {
7173
count: storage.records.length,
7274
})}
@@ -79,11 +81,11 @@ export default function StorageOverview({ storage }) {
7981
<Grid>
8082
<Grid.Row columns={2}>
8183
<Grid.Column textAlign="left">
82-
<Label className="medium rel-mb-1">0 GB</Label>
84+
<Label className="medium rel-mb-1">0&nbsp;GB</Label>
8385
</Grid.Column>
8486
<Grid.Column textAlign="right">
8587
<Label className="medium rel-mb-1">
86-
{storage.total_allowed_quota} GB
88+
{storage.total_allowed_quota}&nbsp;GB
8789
</Label>
8890
</Grid.Column>
8991
</Grid.Row>
@@ -127,7 +129,7 @@ export default function StorageOverview({ storage }) {
127129
</Table.Cell>
128130

129131
<Table.Cell>
130-
<strong>+{record.additional_quota} GB</strong>
132+
<strong>+{record.additional_quota}&nbsp;GB</strong>
131133
<div className="ui tiny text-muted">
132134
(
133135
{i18next.t("{{total}} GB total", {
@@ -149,7 +151,7 @@ export default function StorageOverview({ storage }) {
149151
</Grid.Column>
150152
<Grid.Column width={7} textAlign="left">
151153
<p className="text-muted">
152-
{record.used} / {record.total} GB
154+
{record.used} / {record.total}&nbsp;GB
153155
</p>
154156
</Grid.Column>
155157
</Grid>

invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/FileUploader.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const FileUploaderComponent = ({
5959
const [warningMsg, setWarningMsg] = useState();
6060
const [showQuotaSection, setShowQuotaSection] = useState(false);
6161
const [additionalQuota, _setAdditionalQuota] = useState(
62-
quota.additionalStorage / Math.pow(10, 9)
62+
quota.quotaIncrease.additionalStorage / Math.pow(10, 9) || 0
6363
);
6464
const lockFileUploader = !isDraftRecord && filesLocked;
6565
const dropzoneParams = {
@@ -185,33 +185,27 @@ export const FileUploaderComponent = ({
185185
};
186186

187187
// rescale quota from bytes to GB, as user input requires GB
188-
const quotaInGB = Object.keys(quota).reduce((obj, key) => {
189-
if (key === "maxFiles") {
190-
obj[key] = quota[key];
188+
const quotaInGB = Object.keys(quota["quotaIncrease"]).reduce((obj, key) => {
189+
if (typeof quota["quotaIncrease"][key] === "number") {
190+
obj[key] = quota["quotaIncrease"][key] / Math.pow(10, 9);
191191
} else {
192-
obj[key] = quota[key] / Math.pow(10, 9);
192+
obj[key] = quota["quotaIncrease"][key];
193193
}
194194
return obj;
195195
}, {});
196196

197197
const setAdditionalQuota = (value) => {
198-
// TODO this should take into account all versions of the record instead
199-
// this is how much files they have uploaded so that they can't request less than uploaded
200-
const alreadyUsedAdditional =
201-
Math.ceil(filesSize / Math.pow(10, 9)) - quotaInGB.defaultStorage;
202-
//
203-
const maxAllowed = Math.min(
204-
quotaInGB.maxAdditionalStorage,
205-
quotaInGB.remainingStorage
206-
);
207-
if (value < alreadyUsedAdditional) {
208-
_setAdditionalQuota(alreadyUsedAdditional);
209-
} else if (0 <= value && value <= maxAllowed) {
198+
const minAdditional = quotaInGB["minAdditionalQuotaValue"];
199+
const maxAdditional = quotaInGB["maxAdditionalQuotaValue"];
200+
201+
if (value < minAdditional) {
202+
_setAdditionalQuota(minAdditional);
203+
} else if (minAdditional <= value && value <= maxAdditional) {
210204
_setAdditionalQuota(value);
211-
} else if (value > maxAllowed) {
212-
_setAdditionalQuota(maxAllowed);
205+
} else if (value > maxAdditional) {
206+
_setAdditionalQuota(maxAdditional);
213207
} else if (isNaN(value)) {
214-
_setAdditionalQuota(Math.max(alreadyUsedAdditional, 0));
208+
_setAdditionalQuota(minAdditional);
215209
}
216210
};
217211

@@ -434,10 +428,7 @@ FileUploaderComponent.propTypes = {
434428
quota: PropTypes.shape({
435429
maxStorage: PropTypes.number,
436430
maxFiles: PropTypes.number,
437-
defaultStorage: PropTypes.number,
438-
additionalStorage: PropTypes.number,
439-
maxAdditionalStorage: PropTypes.number,
440-
remainingStorage: PropTypes.number,
431+
quotaIncrease: PropTypes.object,
441432
}),
442433
record: PropTypes.object,
443434
uploadButtonIcon: PropTypes.string,

invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/FileUploaderToolbar.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,24 @@ export const FileUploaderToolbar = (props) => {
120120
>
121121
{humanReadableBytes(filesSize, decimalSizeDisplay)}{" "}
122122
{i18next.t("out of")}{" "}
123-
{humanReadableBytes(
124-
quota.defaultStorage + additionalQuota * Math.pow(10, 9),
125-
decimalSizeDisplay
126-
)}
123+
{quota.quotaIncrease.enabled && quota.quotaIncrease.valid_user
124+
? humanReadableBytes(
125+
quota.quotaIncrease.defaultStorage +
126+
additionalQuota * Math.pow(10, 9),
127+
decimalSizeDisplay
128+
)
129+
: humanReadableBytes(quota.maxStorage, decimalSizeDisplay)}
127130
</Label>
128131
</List.Item>
129132
{quota.quotaIncrease?.enabled && quota.quotaIncrease?.valid_user && (
130133
<List.Item>
131134
<Button
132135
type="button"
133-
// color="green"
134136
size="tiny"
135137
compact
136138
labelPosition="left"
137139
icon="cog"
138-
content="Manage storage"
140+
content={i18next.t("Manage storage")}
139141
onClick={() => {
140142
toggleQuotaSection();
141143
}}

invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/QuotaManager/QuotaDisplay.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { BarStackHorizontal } from "@visx/shape";
55
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
66
import { Text } from "@visx/text";
77
import { Label } from "semantic-ui-react";
8+
import { i18next } from "@translations/invenio_rdm_records/i18next";
9+
import { Trans } from "react-i18next";
810

911
export const QuotaDisplay = (props) => {
1012
const { width, height, additionalQuota, quota } = props;
13+
const { defaultStorage, remainingStorage } = quota;
1114

1215
// Colors for the bar segments
1316
const colors = {
@@ -20,9 +23,9 @@ export const QuotaDisplay = (props) => {
2023
const data = [
2124
{
2225
category: "Storage",
23-
default: quota.defaultStorage,
26+
default: defaultStorage,
2427
additional: additionalQuota,
25-
available: quota.remainingStorage - additionalQuota,
28+
available: remainingStorage - additionalQuota,
2629
},
2730
];
2831

@@ -36,7 +39,7 @@ export const QuotaDisplay = (props) => {
3639
});
3740

3841
const valueScale = scaleLinear({
39-
domain: [0, quota.defaultStorage + quota.remainingStorage],
42+
domain: [0, defaultStorage + remainingStorage],
4043
});
4144

4245
const colorScale = scaleOrdinal({
@@ -52,7 +55,7 @@ export const QuotaDisplay = (props) => {
5255
<>
5356
<div className="flex align-items-center justify-space-between">
5457
<div>0 GB</div>
55-
<div>{quota.defaultStorage + quota.remainingStorage} GB</div>
58+
<div>{defaultStorage + remainingStorage} GB</div>
5659
</div>
5760
{/* Storage Visualization */}
5861
<div>
@@ -123,22 +126,29 @@ export const QuotaDisplay = (props) => {
123126
<div className="flex">
124127
<div className="flex align-items-center">
125128
<Label circular color="blue" empty key="blue" />
126-
<span className="ml-5">Default ({quota.defaultStorage}&nbsp;GB)</span>
129+
<span className="ml-5">
130+
{i18next.t("Default")} ({defaultStorage}&nbsp;GB)
131+
</span>
127132
</div>
128133
<div className="flex align-items-center ml-10">
129134
<Label circular color="green" empty key="green" />
130-
<span className="ml-5">Additional (+{additionalQuota}&nbsp;GB)</span>
135+
<span className="ml-5">
136+
{i18next.t("Additional")} (+{additionalQuota}&nbsp;GB)
137+
</span>
131138
</div>
132139
<div className="flex align-items-center ml-10">
133140
<Label circular color="grey" empty key="grey" />
134141
<span className="ml-5">
135-
Available ({quota.remainingStorage - additionalQuota}&nbsp;GB)
142+
{i18next.t("Available")} ({remainingStorage - additionalQuota}
143+
&nbsp;GB)
136144
</span>
137145
</div>
138146
</div>
139147
<div>
140-
See your <a href="/account/settings/quota">storage settings</a> for usage
141-
across all records
148+
<Trans>
149+
See your <a href="/account/settings/quota">storage settings</a> for usage
150+
across all records
151+
</Trans>
142152
</div>
143153
</div>
144154
</div>
@@ -150,16 +160,5 @@ QuotaDisplay.propTypes = {
150160
width: PropTypes.number.isRequired,
151161
height: PropTypes.number.isRequired,
152162
additionalQuota: PropTypes.number.isRequired,
153-
quota: PropTypes.object,
154-
};
155-
156-
QuotaDisplay.defaultProps = {
157-
quota: {
158-
maxFiles: 5,
159-
maxStorage: 10,
160-
defaultStorage: 10,
161-
additionalStorage: 0,
162-
maxAdditionalStorage: 0,
163-
remainingStorage: 0,
164-
},
163+
quota: PropTypes.object.isRequired,
165164
};

invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/fields/FileUploader/QuotaManager/QuotaManager.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import { connect } from "react-redux";
1111
export const QuotaManagerComponent = (props) => {
1212
const { draft, quota, toggleQuotaSection, additionalQuota, setAdditionalQuota } =
1313
props;
14+
const { defaultStorage, additionalStorage, maxAdditionalStorage, remainingStorage } =
15+
quota;
1416
const [loading, setLoading] = useState(false);
1517
const [error, setError] = useState("");
1618

1719
const handleSubmit = async () => {
1820
setLoading(true);
19-
const { additionalQuota, quota, saveAndFetchDraftAction } = props;
21+
const { additionalQuota, saveAndFetchDraftAction } = props;
2022
const payload = {
21-
quota_size: (quota.defaultStorage + additionalQuota).toString(),
23+
quota_size: (defaultStorage + additionalQuota).toString(),
2224
};
2325

2426
const savedDraft = await saveAndFetchDraftAction(draft); // we require the recid to be reserved
@@ -27,7 +29,7 @@ export const QuotaManagerComponent = (props) => {
2729

2830
if (!quotaIncreaseEndpoint) {
2931
setLoading(false);
30-
setError("Could not submit the quota increase request");
32+
setError(i18next.t("Could not submit the quota increase request"));
3133
return;
3234
}
3335

@@ -54,7 +56,7 @@ export const QuotaManagerComponent = (props) => {
5456
<Message className="pt-20 pb-20">
5557
<Grid>
5658
<Grid.Column width={15} className="pt-0">
57-
<strong>Manage storage</strong>
59+
<strong>{i18next.t("Manage storage")}</strong>
5860
</Grid.Column>
5961
<Grid.Column width={1} className="pt-0">
6062
<Icon
@@ -64,18 +66,25 @@ export const QuotaManagerComponent = (props) => {
6466
/>
6567
</Grid.Column>
6668
</Grid>
67-
{/* TODO translation */}
68-
{quota.remainingStorage > 0 ? (
69+
{remainingStorage > 0 ? (
6970
<Message info>
70-
Each record has a default quota of {quota.defaultStorage} GB. You have an
71-
additional allowance of up to {quota.maxAdditionalStorage} GB that can be
72-
distributed across your uploads as needed.
71+
{i18next.t(
72+
"Each record has a default quota of {{defaultStorage}} GB. You have an additional allowance of up to {{maxAdditionalStorage}} GB that can be distributed across your uploads as needed.",
73+
{
74+
defaultStorage: defaultStorage,
75+
maxAdditionalStorage: maxAdditionalStorage,
76+
}
77+
)}
7378
</Message>
7479
) : (
7580
<Message warning className="display">
76-
Each record has a default quota of {quota.defaultStorage} GB. You have
77-
already used your additional allowance of {quota.maxAdditionalStorage} GB
78-
across your uploads.
81+
{i18next.t(
82+
"Each record has a default quota of {{defaultStorage}} GB. You have already used your additional allowance of {{maxAdditionalStorage}} GB across your uploads.",
83+
{
84+
defaultStorage: defaultStorage,
85+
maxAdditionalStorage: maxAdditionalStorage,
86+
}
87+
)}
7988
</Message>
8089
)}
8190
{/* Progress bar */}
@@ -89,7 +98,7 @@ export const QuotaManagerComponent = (props) => {
8998
/>
9099
)}
91100
</ParentSize>
92-
{quota.remainingStorage > 0 && (
101+
{remainingStorage > 0 && (
93102
<>
94103
<div className="flex align-items-center justify-space-between mt-25">
95104
<strong id="additional-storage">{i18next.t("Additional storage")}</strong>
@@ -108,7 +117,7 @@ export const QuotaManagerComponent = (props) => {
108117
value={additionalQuota}
109118
onChange={(e, { k, value }) => setAdditionalQuota(parseInt(value))}
110119
min={0}
111-
max={Math.min(quota.maxAdditionalStorage, quota.remainingStorage)}
120+
max={Math.min(maxAdditionalStorage, remainingStorage)}
112121
name="quota"
113122
step={1}
114123
type="range"
@@ -118,9 +127,10 @@ export const QuotaManagerComponent = (props) => {
118127
<div className="flex align-items-center justify-space-between pb-20">
119128
<div>0 GB</div>
120129
<div>
121-
New total: <strong>{quota.defaultStorage + additionalQuota} GB</strong>
130+
{i18next.t("New total:")}{" "}
131+
<strong>{defaultStorage + additionalQuota} GB</strong>
122132
</div>
123-
<div>{quota.remainingStorage} GB</div>
133+
<div>{remainingStorage} GB</div>
124134
</div>
125135
{error && (
126136
<ErrorMessage
@@ -137,10 +147,10 @@ export const QuotaManagerComponent = (props) => {
137147
color="green"
138148
labelPosition="left"
139149
icon="check"
140-
content="Apply"
150+
content={i18next.t("Apply")}
141151
onClick={() => handleSubmit()}
142152
loading={loading}
143-
disabled={loading || additionalQuota === quota.additionalStorage}
153+
disabled={loading || additionalQuota === additionalStorage}
144154
/>
145155
</div>
146156
</>
@@ -152,24 +162,13 @@ export const QuotaManagerComponent = (props) => {
152162

153163
QuotaManagerComponent.propTypes = {
154164
draft: PropTypes.object.isRequired,
155-
quota: PropTypes.object,
165+
quota: PropTypes.object.isRequired,
156166
toggleQuotaSection: PropTypes.func.isRequired,
157167
additionalQuota: PropTypes.number.isRequired,
158168
setAdditionalQuota: PropTypes.func.isRequired,
159169
saveAndFetchDraftAction: PropTypes.func.isRequired,
160170
};
161171

162-
QuotaManagerComponent.defaultProps = {
163-
quota: {
164-
maxFiles: 5,
165-
maxStorage: 10,
166-
defaultStorage: 10,
167-
additionalStorage: 0,
168-
maxAdditionalStorage: 0,
169-
remainingStorage: 0,
170-
},
171-
};
172-
173172
const mapDispatchToProps = (dispatch) => ({
174173
saveAndFetchDraftAction: (values) => dispatch(saveAndFetchDraft(values)),
175174
});

0 commit comments

Comments
 (0)