mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
Migrated to TS main services except ShlinkApiClient
This commit is contained in:
parent
64a968711c
commit
ebd7a76896
6 changed files with 30 additions and 29 deletions
|
@ -35,3 +35,6 @@ export const isReachableServer = (server: SelectedServer): server is ReachableSe
|
||||||
|
|
||||||
export const isServerWithId = (server: SelectedServer | ServerWithId): server is ServerWithId =>
|
export const isServerWithId = (server: SelectedServer | ServerWithId): server is ServerWithId =>
|
||||||
!!server?.hasOwnProperty('id');
|
!!server?.hasOwnProperty('id');
|
||||||
|
|
||||||
|
export const isNotFoundServer = (server: SelectedServer): server is NotFoundServer =>
|
||||||
|
!!server?.hasOwnProperty('serverNotFound');
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import React, { FC, useEffect } from 'react';
|
import React, { FC, useEffect } from 'react';
|
||||||
import { RouteChildrenProps } from 'react-router';
|
import { RouteChildrenProps } from 'react-router';
|
||||||
import Message from '../../utils/Message';
|
import Message from '../../utils/Message';
|
||||||
import { isReachableServer, SelectedServer } from '../data';
|
import { isNotFoundServer, SelectedServer } from '../data';
|
||||||
|
|
||||||
interface WithSelectedServerProps extends RouteChildrenProps<{ serverId: string }> {
|
interface WithSelectedServerProps extends RouteChildrenProps<{ serverId: string }> {
|
||||||
selectServer: (serverId: string) => void;
|
selectServer: (serverId: string) => void;
|
||||||
|
@ -21,7 +21,7 @@ export const withSelectedServer = (WrappedComponent: FC<WithSelectedServerProps>
|
||||||
return <Message loading />;
|
return <Message loading />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isReachableServer(selectedServer)) {
|
if (isNotFoundServer(selectedServer)) {
|
||||||
return <ServerError />;
|
return <ServerError />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,21 @@
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { rangeOf } from '../utils';
|
import { rangeOf } from '../utils';
|
||||||
|
import LocalStorage from './LocalStorage';
|
||||||
|
|
||||||
const HEX_COLOR_LENGTH = 6;
|
const HEX_COLOR_LENGTH = 6;
|
||||||
const { floor, random } = Math;
|
const { floor, random } = Math;
|
||||||
const letters = '0123456789ABCDEF';
|
const letters = '0123456789ABCDEF';
|
||||||
const buildRandomColor = () =>
|
const buildRandomColor = () => `#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`;
|
||||||
`#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`;
|
const normalizeKey = (key: string) => key.toLowerCase().trim();
|
||||||
const normalizeKey = (key) => key.toLowerCase().trim();
|
|
||||||
|
|
||||||
export default class ColorGenerator {
|
export default class ColorGenerator {
|
||||||
constructor(storage) {
|
private readonly colors: Record<string, string>;
|
||||||
this.storage = storage;
|
|
||||||
this.colors = this.storage.get('colors') || {};
|
public constructor(private readonly storage: LocalStorage) {
|
||||||
|
this.colors = this.storage.get<Record<string, string>>('colors') || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
getColorForKey = (key) => {
|
public readonly getColorForKey = (key: string) => {
|
||||||
const normalizedKey = normalizeKey(key);
|
const normalizedKey = normalizeKey(key);
|
||||||
const color = this.colors[normalizedKey];
|
const color = this.colors[normalizedKey];
|
||||||
|
|
||||||
|
@ -26,7 +27,7 @@ export default class ColorGenerator {
|
||||||
return color;
|
return color;
|
||||||
};
|
};
|
||||||
|
|
||||||
setColorForKey = (key, color) => {
|
public readonly setColorForKey = (key: string, color: string) => {
|
||||||
const normalizedKey = normalizeKey(key);
|
const normalizedKey = normalizeKey(key);
|
||||||
|
|
||||||
this.colors[normalizedKey] = color;
|
this.colors[normalizedKey] = color;
|
||||||
|
@ -36,6 +37,7 @@ export default class ColorGenerator {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated Use ColorGenerator class instead */
|
||||||
export const colorGeneratorType = PropTypes.shape({
|
export const colorGeneratorType = PropTypes.shape({
|
||||||
getColorForKey: PropTypes.func,
|
getColorForKey: PropTypes.func,
|
||||||
setColorForKey: PropTypes.func,
|
setColorForKey: PropTypes.func,
|
|
@ -16,8 +16,6 @@ describe('<EditServer />', () => {
|
||||||
name: 'name',
|
name: 'name',
|
||||||
url: 'url',
|
url: 'url',
|
||||||
apiKey: 'apiKey',
|
apiKey: 'apiKey',
|
||||||
printableVersion: 'v1.2.0',
|
|
||||||
version: '1.2.0'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
|
import { Mock } from 'ts-mockery';
|
||||||
import ColorGenerator from '../../../src/utils/services/ColorGenerator';
|
import ColorGenerator from '../../../src/utils/services/ColorGenerator';
|
||||||
|
import LocalStorage from '../../../src/utils/services/LocalStorage';
|
||||||
|
|
||||||
describe('ColorGenerator', () => {
|
describe('ColorGenerator', () => {
|
||||||
let colorGenerator;
|
let colorGenerator: ColorGenerator;
|
||||||
const storageMock = {
|
const storageMock = Mock.of<LocalStorage>({
|
||||||
set: jest.fn(),
|
set: jest.fn(),
|
||||||
get: jest.fn(),
|
get: jest.fn(),
|
||||||
};
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
storageMock.set.mockReset();
|
jest.clearAllMocks();
|
||||||
storageMock.get.mockReset();
|
|
||||||
|
|
||||||
colorGenerator = new ColorGenerator(storageMock);
|
colorGenerator = new ColorGenerator(storageMock);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
|
import { Mock } from 'ts-mockery';
|
||||||
import LocalStorage from '../../../src/utils/services/LocalStorage';
|
import LocalStorage from '../../../src/utils/services/LocalStorage';
|
||||||
|
|
||||||
describe('LocalStorage', () => {
|
describe('LocalStorage', () => {
|
||||||
const localStorageMock = {
|
const getItem = jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null);
|
||||||
getItem: jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
|
const setItem = jest.fn();
|
||||||
setItem: jest.fn(),
|
const localStorageMock = Mock.of<Storage>({ getItem, setItem });
|
||||||
};
|
let storage: LocalStorage;
|
||||||
let storage;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
localStorageMock.getItem.mockClear();
|
jest.clearAllMocks();
|
||||||
localStorageMock.setItem.mockReset();
|
|
||||||
|
|
||||||
storage = new LocalStorage(localStorageMock);
|
storage = new LocalStorage(localStorageMock);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -20,15 +18,15 @@ describe('LocalStorage', () => {
|
||||||
|
|
||||||
storage.set('foo', value);
|
storage.set('foo', value);
|
||||||
|
|
||||||
expect(localStorageMock.setItem).toHaveBeenCalledTimes(1);
|
expect(setItem).toHaveBeenCalledTimes(1);
|
||||||
expect(localStorageMock.setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
|
expect(setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('get', () => {
|
describe('get', () => {
|
||||||
it('fetches item from local storage', () => {
|
it('fetches item from local storage', () => {
|
||||||
storage.get('foo');
|
storage.get('foo');
|
||||||
expect(localStorageMock.getItem).toHaveBeenCalledTimes(1);
|
expect(getItem).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns parsed value when requested value is found in local storage', () => {
|
it('returns parsed value when requested value is found in local storage', () => {
|
Loading…
Reference in a new issue