csv export, clean up export type branching

This commit is contained in:
f0x 2023-01-26 19:08:39 +01:00
parent e478aeec0b
commit c577348edf

View file

@ -109,30 +109,53 @@ module.exports = (build) => ({
}), }),
exportDomainList: build.mutation({ exportDomainList: build.mutation({
queryFn: (formData, api, _extraOpts, baseQuery) => { queryFn: (formData, api, _extraOpts, baseQuery) => {
let process;
if (formData.exportType == "json") {
process = {
transformEntry: (entry) => ({
domain: entry.domain,
public_comment: entry.public_comment,
obfuscate: entry.obfuscate
}),
stringify: (list) => JSON.stringify(list),
extension: ".json",
mime: "application/json"
};
} else if (formData.exportType == "csv") {
process = {
transformEntry: (entry) => [
entry.domain,
"suspend", // severity
false, // reject_media
false, // reject_reports
entry.public_comment,
entry.obfuscate ?? false
],
stringify: (list) => csv.unparse({
fields: "#domain,#severity,#reject_media,#reject_reports,#public_comment,#obfuscate".split(","),
data: list
}),
extension: ".csv",
mime: "text/csv"
};
} else {
process = {
transformEntry: (entry) => entry.domain,
stringify: (list) => list.join("\n"),
extension: ".txt",
mime: "text/plain"
};
}
return Promise.try(() => { return Promise.try(() => {
return baseQuery({ return baseQuery({
url: `/api/v1/admin/domain_blocks` url: `/api/v1/admin/domain_blocks`
}); });
}).then(unwrapRes).then((blockedInstances) => { }).then(unwrapRes).then((blockedInstances) => {
return blockedInstances.map((entry) => { return blockedInstances.map(process.transformEntry);
if (formData.exportType == "json") {
return {
domain: entry.domain,
public_comment: entry.public_comment
};
} else {
return entry.domain;
}
});
}).then((exportList) => { }).then((exportList) => {
if (formData.exportType == "json") { return process.stringify(exportList);
return JSON.stringify(exportList);
} else if (formData.exportType == "csv") {
let header = `#domain,#severity,#reject_media,#reject_reports,#public_comment,#obfuscate`;
} else {
return exportList.join("\n");
}
}).then((exportAsString) => { }).then((exportAsString) => {
if (formData.action == "export") { if (formData.action == "export") {
return { return {
@ -141,7 +164,6 @@ module.exports = (build) => ({
} else if (formData.action == "export-file") { } else if (formData.action == "export-file") {
let domain = new URL(api.getState().oauth.instance).host; let domain = new URL(api.getState().oauth.instance).host;
let date = new Date(); let date = new Date();
let mime;
let filename = [ let filename = [
domain, domain,
@ -151,15 +173,11 @@ module.exports = (build) => ({
date.getDate().toString().padStart(2, "0"), date.getDate().toString().padStart(2, "0"),
].join("-"); ].join("-");
if (formData.exportType == "json") { fileDownload(
filename += ".json"; exportAsString,
mime = "application/json"; filename + process.extension,
} else { process.mime
filename += ".txt"; );
mime = "text/plain";
}
fileDownload(exportAsString, filename, mime);
} }
return { data: null }; return { data: null };
}).catch((e) => { }).catch((e) => {