Skip to content

Commit f62a671

Browse files
committed
feat: download redemption codes as file (#12)
1 parent 9e2f238 commit f62a671

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

web/src/helpers/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,13 @@ export function timestamp2string(timestamp) {
143143
':' +
144144
second
145145
);
146+
}
147+
148+
export function downloadTextAsFile(text, filename) {
149+
let blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
150+
let url = URL.createObjectURL(blob);
151+
let a = document.createElement('a');
152+
a.href = url;
153+
a.download = filename;
154+
a.click();
146155
}

web/src/pages/Redemption/EditRedemption.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from 'react';
22
import { Button, Form, Header, Segment } from 'semantic-ui-react';
33
import { useParams } from 'react-router-dom';
4-
import { API, isAdmin, showError, showSuccess, quotatamp2string } from '../../helpers';
4+
import { API, downloadTextAsFile, showError, showSuccess } from '../../helpers';
55

66
const EditRedemption = () => {
77
const params = useParams();
@@ -11,7 +11,7 @@ const EditRedemption = () => {
1111
const originInputs = {
1212
name: '',
1313
quota: 100,
14-
count: 1,
14+
count: 1
1515
};
1616
const [inputs, setInputs] = useState(originInputs);
1717
const { name, quota, count } = inputs;
@@ -46,10 +46,10 @@ const EditRedemption = () => {
4646
res = await API.put(`/api/redemption/`, { ...localInputs, id: parseInt(redemptionId) });
4747
} else {
4848
res = await API.post(`/api/redemption/`, {
49-
...localInputs,
49+
...localInputs
5050
});
5151
}
52-
const { success, message } = res.data;
52+
const { success, message, data } = res.data;
5353
if (success) {
5454
if (isEdit) {
5555
showSuccess('兑换码更新成功!');
@@ -60,6 +60,13 @@ const EditRedemption = () => {
6060
} else {
6161
showError(message);
6262
}
63+
if (!isEdit && data) {
64+
let text = "";
65+
for (let i = 0; i < data.length; i++) {
66+
text += data[i] + "\n";
67+
}
68+
downloadTextAsFile(text, `${inputs.name}.txt`);
69+
}
6370
};
6471

6572
return (

0 commit comments

Comments
 (0)