mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2025-06-15 13:36:35 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
c453a35763 | |||
d97248c6ec | |||
9fbc4e8383 | |||
57bebc92c7 | |||
5939e9dd0e | |||
0665869c30 | |||
199b27d63a | |||
2b28434712 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "foris",
|
||||
"version": "3.4.0",
|
||||
"version": "4.1.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "foris",
|
||||
"version": "3.4.0",
|
||||
"version": "4.1.0",
|
||||
"description": "Set of components and utils for Foris and its plugins.",
|
||||
"author": "CZ.NIC, z.s.p.o.",
|
||||
"repository": {
|
||||
|
@ -53,10 +53,11 @@ function createAPIHook(method) {
|
||||
payload: result.data,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorPayload = getErrorPayload(error);
|
||||
dispatch({
|
||||
type: API_ACTIONS.FAILURE,
|
||||
status: error.response && error.response.status,
|
||||
payload: getErrorPayload(error),
|
||||
payload: errorPayload,
|
||||
});
|
||||
}
|
||||
}, [urlRoot, contentType]);
|
||||
@ -80,6 +81,12 @@ function APIReducer(state, action) {
|
||||
if (action.status === 403) {
|
||||
window.location.assign(ForisURLs.login);
|
||||
}
|
||||
|
||||
// Not an API error - should be rethrown.
|
||||
if (action.payload && action.payload.stack && action.payload.message) {
|
||||
throw (action.payload);
|
||||
}
|
||||
|
||||
return {
|
||||
state: API_STATE.ERROR,
|
||||
data: action.payload,
|
||||
|
@ -65,9 +65,8 @@ export function getErrorPayload(error) {
|
||||
if (error.request) {
|
||||
return _("No response received.");
|
||||
}
|
||||
/* eslint no-console: "off" */
|
||||
console.error(error);
|
||||
return _("An unknown error occurred. Check the console for more info.");
|
||||
// Return original error because it's not directly related to API request/response.
|
||||
return error;
|
||||
}
|
||||
|
||||
export function getJSONErrorMessage(error) {
|
||||
|
@ -64,6 +64,7 @@ export {
|
||||
} from "./utils/conditionalHOCs";
|
||||
export { ErrorMessage } from "./utils/ErrorMessage";
|
||||
export { useClickOutside } from "./utils/hooks";
|
||||
export { toLocaleDateString } from "./utils/datetime";
|
||||
|
||||
// Foris URL
|
||||
export { ForisURLs, REFORIS_URL_PREFIX } from "./forisUrls";
|
||||
|
@ -17,7 +17,7 @@ global.afterEach(() => {
|
||||
global._ = (str) => str;
|
||||
global.ngettext = (str) => str;
|
||||
global.babel = { format: (str) => str };
|
||||
global.ForisTranslations = {};
|
||||
global.ForisTranslations = { locale: "en" };
|
||||
|
||||
// Mock web sockets
|
||||
window.WebSocket = jest.fn();
|
||||
|
51
src/utils/__tests__/datetime.test.js
Normal file
51
src/utils/__tests__/datetime.test.js
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
|
||||
*
|
||||
* This is free software, licensed under the GNU General Public License v3.
|
||||
* See /LICENSE for more information.
|
||||
*/
|
||||
|
||||
import { toLocaleDateString } from "../datetime";
|
||||
|
||||
describe("toLocaleDateString", () => {
|
||||
it("should work with different locale", () => {
|
||||
global.ForisTranslations = { locale: "fr" };
|
||||
expect(
|
||||
toLocaleDateString("2020-02-20T12:51:36+00:00")
|
||||
).toBe("20 février 2020 12:51");
|
||||
global.ForisTranslations = { locale: "en" };
|
||||
})
|
||||
|
||||
it("should convert with default format", () => {
|
||||
expect(
|
||||
toLocaleDateString("2020-02-20T12:51:36+00:00")
|
||||
).toBe("February 20, 2020 12:51 PM");
|
||||
});
|
||||
|
||||
it("should convert with custom input format", () => {
|
||||
expect(
|
||||
toLocaleDateString(
|
||||
"2020-02-20 12:51:36 +0000",
|
||||
{ inputFormat: "YYYY-MM-DD HH:mm:ss Z" },
|
||||
)
|
||||
).toBe("February 20, 2020 12:51 PM");
|
||||
});
|
||||
|
||||
it("should convert with custom output format", () => {
|
||||
expect(
|
||||
toLocaleDateString(
|
||||
"2020-02-20T12:51:36+00:00",
|
||||
{ outputFormat: "LL" },
|
||||
)
|
||||
).toBe("February 20, 2020");
|
||||
});
|
||||
|
||||
it("should convert with custom input and output format", () => {
|
||||
expect(
|
||||
toLocaleDateString(
|
||||
"2020-02-20 12:51:36 +0000",
|
||||
{ inputFormat: "YYYY-MM-DD HH:mm:ss Z", outputFormat: "LL" },
|
||||
)
|
||||
).toBe("February 20, 2020");
|
||||
});
|
||||
});
|
8
src/utils/datetime.js
Normal file
8
src/utils/datetime.js
Normal file
@ -0,0 +1,8 @@
|
||||
import moment from "moment";
|
||||
|
||||
export function toLocaleDateString(date, { inputFormat, outputFormat = "LLL" } = {}) {
|
||||
const parsedDate = inputFormat ? moment(date, inputFormat) : moment(date);
|
||||
return parsedDate
|
||||
.locale(ForisTranslations.locale)
|
||||
.format(outputFormat);
|
||||
}
|
Reference in New Issue
Block a user