Skip to content

Commit c079dcc

Browse files
committed
add notification & validation messages
1 parent 99015de commit c079dcc

File tree

8 files changed

+95
-54
lines changed

8 files changed

+95
-54
lines changed

docs/frontend/constants.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ Stores all of the action creators' names
1111

1212
Stores the following messages;
1313

14-
* delete message
15-
* leave message
16-
* override static file message
14+
* confirm delete message
15+
* confirm leave message
16+
* confirm override static file message
1717
* not found message
18+
* notification messages
19+
* validation messages
1820

1921
This allows us to control all of the messages in a single place and also will help us localize the admin panel in the future.
2022

src/actions/collections.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as ActionTypes from '../constants/actionTypes';
22
import _ from 'underscore';
33
import moment from 'moment';
4-
4+
import { validationError } from '../actions/utils';
5+
import { get, put, del } from '../utils/fetch';
6+
import { validator } from '../utils/validation';
7+
import { slugify } from '../utils/helpers';
58
import {
69
getCollectionsUrl,
710
getCollectionUrl,
@@ -10,18 +13,16 @@ import {
1013
putCollectionDocumentUrl,
1114
deleteCollectionDocumentUrl
1215
} from '../constants/api';
13-
1416
import {
1517
getCollections,
1618
getCollection,
1719
getCollectionDocuments
1820
} from '../constants/api';
19-
20-
import { validationError } from '../actions/utils';
21-
22-
import { get, put, del } from '../utils/fetch';
23-
import { validator } from '../utils/validation';
24-
import { slugify } from '../utils/helpers';
21+
import {
22+
getTitleRequiredMessage,
23+
getFilenameRequiredMessage,
24+
getFilenameNotValidMessage
25+
} from '../constants/messages';
2526

2627
export function fetchCollections() {
2728
return dispatch => {
@@ -76,15 +77,15 @@ export function putDocument(id, collection) {
7677
'path': 'required'
7778
};
7879
let messages = {
79-
'title.required': 'The title is required.',
80-
'path.required': 'The filename is required.'
80+
'title.required': getTitleRequiredMessage(),
81+
'path.required': getFilenameRequiredMessage()
8182
};
8283
if (collection == 'posts') {
8384
validations['path'] = 'required|date';
84-
messages['path.date'] = 'The filename is not valid.';
85+
messages['path.date'] = getFilenameNotValidMessage();
8586
}else {
8687
validations['path'] = 'required|filename';
87-
messages['path.filename'] = 'The filename is not valid.';
88+
messages['path.filename'] = getFilenameNotValidMessage();
8889
}
8990
const errors = validator(metadata, validations, messages);
9091
if(errors.length) {
@@ -101,7 +102,6 @@ export function putDocument(id, collection) {
101102
}),
102103
raw_content
103104
});
104-
// TODO dispatch({type: ActionTypes.PUT_DOCUMENT_REQUEST, doc});
105105
return put(
106106
putCollectionDocumentUrl(
107107
collection, id || path.substring(path.lastIndexOf('/') + 1)

src/actions/config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as ActionTypes from '../constants/actionTypes';
22
import { getConfigurationUrl, putConfigurationUrl } from '../constants/api';
3-
3+
import { getParserErrorMessage } from '../constants/messages';
44
import { addNotification } from './notifications';
5-
65
import { get, put } from '../utils/fetch';
76
import { toJSON } from '../utils/helpers';
87

@@ -24,7 +23,7 @@ export function putConfig(config) {
2423
try {
2524
json = toJSON(config);
2625
} catch (e) {
27-
return dispatch(addNotification('Parse Error', e.message, 'error'));
26+
return dispatch(addNotification(getParserErrorMessage(), e.message, 'error'));
2827
}
2928
return put(
3029
putConfigurationUrl(),

src/actions/datafiles.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import * as ActionTypes from '../constants/actionTypes';
22
import _ from 'underscore';
3-
3+
import { getParserErrorMessage } from '../constants/messages';
4+
import { validationError } from './utils';
5+
import { addNotification } from './notifications';
6+
import { get, put, del } from '../utils/fetch';
7+
import { toJSON } from '../utils/helpers';
8+
import { validator } from '../utils/validation';
49
import {
510
getDataFilesUrl,
611
getDataFileUrl,
712
putDataFileUrl,
813
deleteDataFileUrl
914
} from '../constants/api';
1015

11-
import { validationError } from './utils';
12-
import { addNotification } from './notifications';
13-
14-
import { get, put, del } from '../utils/fetch';
15-
import { toJSON } from '../utils/helpers';
16-
import { validator } from '../utils/validation';
17-
1816
export function fetchDataFiles() {
1917
return (dispatch) => {
2018
dispatch({ type: ActionTypes.FETCH_DATAFILES_REQUEST});
@@ -60,7 +58,7 @@ export function putDataFile(filename, data) {
6058
try {
6159
json = toJSON(data);
6260
} catch (e) {
63-
return dispatch(addNotification('Parse Error', e.message, 'error'));
61+
return dispatch(addNotification(getParserErrorMessage(), e.message, 'error'));
6462
}
6563
return put(
6664
putDataFileUrl(filename),

src/actions/pages.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import * as ActionTypes from '../constants/actionTypes';
22
import _ from 'underscore';
3-
3+
import { validationError } from '../actions/utils';
4+
import { get, put, del } from '../utils/fetch';
5+
import { validator } from '../utils/validation';
6+
import { slugify } from '../utils/helpers';
47
import {
58
getPagesUrl,
69
getPageUrl,
710
putPageUrl,
811
deletePageUrl
912
} from '../constants/api';
10-
11-
import { validationError } from '../actions/utils';
12-
13-
import { get, put, del } from '../utils/fetch';
14-
import { validator } from '../utils/validation';
15-
import { slugify } from '../utils/helpers';
13+
import {
14+
getTitleRequiredMessage,
15+
getFilenameRequiredMessage,
16+
getFilenameNotValidMessage
17+
} from '../constants/messages';
1618

1719
export function fetchPages() {
1820
return (dispatch) => {
@@ -49,8 +51,8 @@ export function putPage(name) {
4951
metadata,
5052
{ 'path': 'required|filename' },
5153
{
52-
'path.required': 'The filename is required.',
53-
'path.filename': 'The filename is not valid.'
54+
'path.required': getTitleRequiredMessage(),
55+
'path.filename': getFilenameNotValidMessage()
5456
}
5557
);
5658
if (errors.length) {
@@ -67,7 +69,6 @@ export function putPage(name) {
6769
}),
6870
raw_content
6971
});
70-
// TODO dispatch({type: ActionTypes.PUT_PAGE_REQUEST, page});
7172
return put(
7273
putPageUrl(name || path),
7374
page,

src/actions/staticfiles.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import * as ActionTypes from '../constants/actionTypes';
22
import _ from 'underscore';
3-
3+
import { get, put, del } from '../utils/fetch';
4+
import { addNotification } from './notifications';
5+
import {
6+
getSuccessMessage,
7+
getErrorMessage,
8+
getUploadSuccessMessage,
9+
getUploadErrorMessage
10+
} from '../constants/messages';
411
import {
512
getStaticFilesUrl,
613
getStaticFileUrl,
714
putStaticFileUrl,
815
deleteStaticFileUrl
916
} from '../constants/api';
1017

11-
import { get, put, del } from '../utils/fetch';
12-
13-
import { addNotification } from './notifications';
14-
1518
export function fetchStaticFiles() {
1619
return dispatch => {
1720
dispatch({ type: ActionTypes.FETCH_STATICFILES_REQUEST});
@@ -47,8 +50,8 @@ export function uploadStaticFiles(files) {
4750
});
4851
dispatch(fetchStaticFiles());
4952
dispatch(addNotification(
50-
'Success',
51-
`${file.name} uploaded successfully`,
53+
getSuccessMessage(),
54+
getUploadSuccessMessage(file.name),
5255
'success'
5356
));
5457
})
@@ -58,8 +61,8 @@ export function uploadStaticFiles(files) {
5861
error
5962
});
6063
dispatch(addNotification(
61-
'Upload Error',
62-
`Error occurred uploading the file.`,
64+
getErrorMessage(),
65+
getUploadErrorMessage(),
6366
'error'
6467
));
6568
});

src/constants/messages.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// inline messages
12
export const getDeleteMessage = (filename) =>
23
`Are you sure that you want to delete "${filename}" ?`;
34

@@ -9,3 +10,35 @@ export const getNotFoundMessage = (type) =>
910

1011
export const getOverrideMessage = (filename) =>
1112
`${filename} will be overwritten. Continue anyway?`;
13+
14+
// notification messages
15+
export const getParserErrorMessage = () => "Parse Error";
16+
17+
export const getSuccessMessage = () => "Success";
18+
19+
export const getErrorMessage = () => "Error";
20+
21+
export const getUploadSuccessMessage = (filename) =>
22+
`${filename} uploaded successfully`;
23+
24+
export const getUploadErrorMessage = () =>
25+
`Error occurred uploading the file.`;
26+
27+
export const getFetchErrorMessage = (filename) =>
28+
`Could not fetch the ${filename}`;
29+
30+
export const getUpdateErrorMessage = (filename) =>
31+
`Could not update the ${filename}`;
32+
33+
export const getDeleteErrorMessage = (filename) =>
34+
`Could not delete the ${filename}`;
35+
36+
// validation messages
37+
export const getTitleRequiredMessage = () =>
38+
"The title is required.";
39+
40+
export const getFilenameRequiredMessage = () =>
41+
"The filename is required.";
42+
43+
export const getFilenameNotValidMessage = () =>
44+
"The filename is not valid.";

src/utils/fetch.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import fetch from 'isomorphic-fetch';
2-
32
import { addNotification } from '../actions/notifications';
3+
import {
4+
getErrorMessage,
5+
getFetchErrorMessage,
6+
getUpdateErrorMessage,
7+
getDeleteMessage
8+
} from '../constants/messages';
49

510
/**
611
* Fetch wrapper for GET request that dispatches actions according to the
@@ -23,8 +28,8 @@ export const get = (url, action_success, action_failure, dispatch) => {
2328
[action_failure.name]: error
2429
});
2530
dispatch(addNotification(
26-
'Fetch Error',
27-
`Could not fetch the ${action_success.name}`,
31+
getErrorMessage(),
32+
getFetchErrorMessage(action_success.name),
2833
'error'
2934
));
3035
});
@@ -59,8 +64,8 @@ export const put = (url, body, action_success, action_failure, dispatch) => {
5964
[action_failure.name]: error
6065
});
6166
dispatch(addNotification(
62-
'Update Error',
63-
`Could not update the ${action_success.name}`,
67+
getErrorMessage(),
68+
getUpdateErrorMessage(action_success.name),
6469
'error'
6570
));
6671
});
@@ -88,8 +93,8 @@ export const del = (url, action_success, action_failure, dispatch) => {
8893
[action_failure.name]: error
8994
});
9095
dispatch(addNotification(
91-
'Delete Error',
92-
`Could not delete the ${action_success.name}`,
96+
getErrorMessage(),
97+
getDeleteMessage(action_success.name),
9398
'error'
9499
));
95100
});

0 commit comments

Comments
 (0)