Implemented importing servers from CSV file

This commit is contained in:
Alejandro Celaya
2018-08-21 20:33:12 +02:00
parent ac52f55c5e
commit 9b063a4616
7 changed files with 140 additions and 67 deletions

View File

@@ -0,0 +1,27 @@
import csvjson from 'csvjson';
export class ServersImporter {
constructor(csvjson) {
this.csvjson = csvjson;
}
importServersFromFile = (file) => {
if (!file || file.type !== 'text/csv') {
return Promise.reject('No file provided or file is not a CSV');
}
const reader = new FileReader();
return new Promise(resolve => {
reader.onloadend = e => {
const content = e.target.result;
const servers = this.csvjson.toObject(content);
resolve(servers);
};
reader.readAsText(file);
});
};
}
const serversImporter = new ServersImporter(csvjson);
export default serversImporter;

View File

@@ -1,5 +1,5 @@
import Storage from '../../utils/Storage';
import { dissoc } from 'ramda';
import { assoc, dissoc, reduce } from 'ramda';
const SERVERS_STORAGE_KEY = 'servers';
@@ -8,25 +8,26 @@ export class ServersService {
this.storage = storage;
}
listServers = () => {
return this.storage.get(SERVERS_STORAGE_KEY) || {};
listServers = () => this.storage.get(SERVERS_STORAGE_KEY) || {};
findServerById = serverId => this.listServers()[serverId];
createServer = server => this.createServers([server]);
createServers = servers => {
const allServers = reduce(
(serversObj, server) => assoc(server.id, server, serversObj),
this.listServers(),
servers
);
this.storage.set(SERVERS_STORAGE_KEY, allServers);
};
findServerById = serverId => {
const servers = this.listServers();
return servers[serverId];
};
createServer = server => {
const servers = this.listServers();
servers[server.id] = server;
this.storage.set(SERVERS_STORAGE_KEY, servers);
};
deleteServer = server => {
const servers = dissoc(server.id, this.listServers());
this.storage.set(SERVERS_STORAGE_KEY, servers);
};
deleteServer = server =>
this.storage.set(
SERVERS_STORAGE_KEY,
dissoc(server.id, this.listServers())
);
}
export default new ServersService(Storage);