Merged develop

This commit is contained in:
Alejandro Celaya 2021-09-12 12:28:01 +02:00
commit 6780aa623b
5 changed files with 106 additions and 31 deletions

View file

@ -11,8 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* [#462](https://github.com/shlinkio/shlink-web-client/pull/462) Now it is possible to paste multiple comma-separated tags in the tags selector, making all of them to be added as individual tags.
* [#463](https://github.com/shlinkio/shlink-web-client/pull/463) The strategy to determine which tags to suggest in the TagsSelector during short URL creation, can now be configured:
* `startsWith`: Suggests tags that start with the input. This is the default behavior for keep it as it was so far.
* `includes`: Suggests tags that contain the input.
* `startsWith`: Suggests tags that start with the input. This is the default behavior for keep it as it was so far.
* `includes`: Suggests tags that contain the input.
* [#464](https://github.com/shlinkio/shlink-web-client/pull/464) Added support to download QR codes. This feature requires an unreleased version of Shlink, so it comes disabled, and will get enabled as soon as Shlink v2.9 is released.
* [#469](https://github.com/shlinkio/shlink-web-client/pull/469) Added support `errorCorrection` in QR codes, when consuming Shlink 2.8 or higher.
@ -26,8 +26,27 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed
* *Nothing*
### Fixed
* *Nothing*
## [3.2.1] - 2021-09-12
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#478](https://github.com/shlinkio/shlink-web-client/pull/478) Fixed tags including special chars not being properly URL encoded before using them as query params.
* [#480](https://github.com/shlinkio/shlink-web-client/pull/480) Fixed servers import on Chromium-based browsers when using windows.
* [#482](https://github.com/shlinkio/shlink-web-client/pull/480) Fixed end date not being set to the end of the day when filtering visits using a "smart filter" (last 7 days, last 30 days, etc).
## [3.2.0] - 2021-07-12

View file

@ -7,7 +7,7 @@ type Ref<T> = RefObject<T> | MutableRefObject<T>;
export interface ImportServersBtnProps {
onImport?: () => void;
onImportError?: () => void;
onImportError?: (error: Error) => void;
}
interface ImportServersBtnConnectProps extends ImportServersBtnProps {

View file

@ -1,29 +1,37 @@
import { CsvJson } from 'csvjson';
import { ServerData } from '../data';
interface CsvFile extends File {
type: 'text/csv' | 'text/comma-separated-values' | 'application/csv';
}
const validateServer = (server: any): server is ServerData =>
typeof server.url === 'string' && typeof server.apiKey === 'string' && typeof server.name === 'string';
const CSV_MIME_TYPES = [ 'text/csv', 'text/comma-separated-values', 'application/csv' ];
const isCsv = (file?: File | null): file is CsvFile => !!file && CSV_MIME_TYPES.includes(file.type);
const validateServers = (servers: any): servers is ServerData[] =>
Array.isArray(servers) && servers.every(validateServer);
export default class ServersImporter {
public constructor(private readonly csvjson: CsvJson, private readonly fileReaderFactory: () => FileReader) {}
public constructor(private readonly csvJson: CsvJson, private readonly fileReaderFactory: () => FileReader) {}
public readonly importServersFromFile = async (file?: File | null): Promise<ServerData[]> => {
if (!isCsv(file)) {
throw new Error('No file provided or file is not a CSV');
if (!file) {
throw new Error('No file provided');
}
const reader = this.fileReaderFactory();
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
reader.addEventListener('loadend', (e: ProgressEvent<FileReader>) => {
const content = e.target?.result?.toString() ?? '';
const servers = this.csvjson.toObject<ServerData>(content);
try {
// TODO Read as stream, otherwise, if the file is too big, this will block the browser tab
const content = e.target?.result?.toString() ?? '';
const servers = this.csvJson.toObject(content);
resolve(servers);
if (!validateServers(servers)) {
throw new Error('Provided file does not have the right format.');
}
resolve(servers);
} catch (e) {
reject(e);
}
});
reader.readAsText(file);
});

View file

@ -55,6 +55,7 @@ export const rangeOrIntervalToString = (range?: DateRange | DateInterval): strin
};
const startOfDaysAgo = (daysAgo: number) => startOfDay(subDays(new Date(), daysAgo));
const endingToday = (startDate: Date): DateRange => ({ startDate, endDate: endOfDay(new Date()) });
export const intervalToDateRange = (dateInterval?: DateInterval): DateRange => {
if (!dateInterval) {
@ -63,19 +64,19 @@ export const intervalToDateRange = (dateInterval?: DateInterval): DateRange => {
switch (dateInterval) {
case 'today':
return { startDate: startOfDay(new Date()), endDate: new Date() };
return endingToday(startOfDay(new Date()));
case 'yesterday':
return { startDate: startOfDaysAgo(1), endDate: endOfDay(subDays(new Date(), 1)) };
case 'last7Days':
return { startDate: startOfDaysAgo(7), endDate: new Date() };
return endingToday(startOfDaysAgo(7));
case 'last30Days':
return { startDate: startOfDaysAgo(30), endDate: new Date() };
return endingToday(startOfDaysAgo(30));
case 'last90Days':
return { startDate: startOfDaysAgo(90), endDate: new Date() };
return endingToday(startOfDaysAgo(90));
case 'last180days':
return { startDate: startOfDaysAgo(180), endDate: new Date() };
return endingToday(startOfDaysAgo(180));
case 'last365Days':
return { startDate: startOfDaysAgo(365), endDate: new Date() };
return endingToday(startOfDaysAgo(365));
}
return {};

View file

@ -21,23 +21,70 @@ describe('ServersImporter', () => {
describe('importServersFromFile', () => {
it('rejects with error if no file was provided', async () => {
await expect(importer.importServersFromFile()).rejects.toEqual(
new Error('No file provided or file is not a CSV'),
new Error('No file provided'),
);
});
it('rejects with error if provided file is not a CSV', async () => {
await expect(importer.importServersFromFile(Mock.of<File>({ type: 'text/html' }))).rejects.toEqual(
new Error('No file provided or file is not a CSV'),
);
it('rejects with error if parsing the file fails', async () => {
const expectedError = new Error('Error parsing file');
toObject.mockImplementation(() => {
throw expectedError;
});
await expect(importer.importServersFromFile(Mock.of<File>({ type: 'text/html' }))).rejects.toEqual(expectedError);
});
it.each([
[ 'text/csv' ],
[ 'text/comma-separated-values' ],
[ 'application/csv' ],
])('reads file when a CSV is provided', async (type) => {
await importer.importServersFromFile(Mock.of<File>({ type }));
[{}],
[ undefined ],
[[{ foo: 'bar' }]],
[
[
{
url: 1,
apiKey: 1,
name: 1,
},
],
],
[
[
{
url: 'foo',
apiKey: 'foo',
name: 'foo',
},
{ bar: 'foo' },
],
],
])('rejects with error if provided file does not parse to valid list of servers', async (parsedObject) => {
toObject.mockReturnValue(parsedObject);
await expect(importer.importServersFromFile(Mock.of<File>({ type: 'text/html' }))).rejects.toEqual(
new Error('Provided file does not have the right format.'),
);
});
it('reads file when a CSV containing valid servers is provided', async () => {
const expectedServers = [
{
url: 'foo',
apiKey: 'foo',
name: 'foo',
},
{
url: 'bar',
apiKey: 'bar',
name: 'bar',
},
];
toObject.mockReturnValue(expectedServers);
const result = await importer.importServersFromFile(Mock.all<File>());
expect(result).toEqual(expectedServers);
expect(readAsText).toHaveBeenCalledTimes(1);
expect(toObject).toHaveBeenCalledTimes(1);
});