Skip to content

Commit 1b9cb75

Browse files
authored
KER-422 Review fixes (#1104)
* fix: fixed the use of defaultNickname and unified it * fix: fixed label form not clearing on cancel / submit * chore: updated snapshots * chore: linter fixes * fix: fixing KER-422 remarks * fix: fixed logout loop * fix: created place to show server side errors * fix: fixed undefined error on contact creation * fix: fixed whitepage on new contact creation * chore: linter fixes
1 parent 81c022a commit 1b9cb75

File tree

23 files changed

+210
-240
lines changed

23 files changed

+210
-240
lines changed

src/actions/__tests__/hearingEditor.test.jsx

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,6 @@ describe('HearingEditor actions', () => {
5858
store.clearActions();
5959
store = mockStore(initialState);
6060
});
61-
describe('addContact', () => {
62-
it('dispatches ADD_CONTACT_SUCCESS on successful contact addition', async () => {
63-
const contact = { name: 'John Doe', email: 'john@example.com' };
64-
const response = { id: '123', name: 'John Doe', email: 'john@example.com' };
65-
api.post.mockResolvedValue({ status: 201, json: () => Promise.resolve(response) });
66-
67-
const expectedActions = [
68-
{ type: EditorActions.ADD_CONTACT },
69-
{ type: EditorActions.ADD_CONTACT_SUCCESS, payload: { contact: response } }
70-
];
71-
72-
await store.dispatch(actions.addContact(contact, []));
73-
expect(store.getActions()).toContainEqual(expectedActions[0]);
74-
expect(store.getActions()).toContainEqual(expectedActions[1]);
75-
});
76-
77-
it('dispatches ADD_CONTACT_FAILED on failure', async () => {
78-
const contact = { name: 'John Doe', email: 'invalid-email' };
79-
api.post.mockResolvedValue({ status: 400, json: () => Promise.resolve({ message: 'Invalid email' }) });
80-
81-
const expectedActions = [
82-
{ type: EditorActions.ADD_CONTACT },
83-
{ type: EditorActions.ADD_CONTACT_FAILED, payload: { errors: { message: 'Invalid email' } } }
84-
];
85-
86-
await store.dispatch(actions.addContact(contact, []));
87-
88-
expect(store.getActions()).toContainEqual(expectedActions[0]);
89-
expect(store.getActions()).toContainEqual(expectedActions[1]);
90-
});
91-
});
9261
describe('Simple Synchronous Actions', () => {
9362
it('should create an action to change a project', () => {
9463
const projectId = '123';

src/actions/hearingEditor.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import getMessage from '../utils/getMessage';
1818
export const EditorActions = {
1919
ACTIVE_PHASE: 'activePhase',
2020
ADD_ATTACHMENT: 'addAttachment',
21-
ADD_CONTACT_FAILED: 'addContactFailed',
22-
ADD_CONTACT_SUCCESS: 'addContactSuccess',
23-
ADD_CONTACT: 'addContact',
2421
ADD_LABEL_FAILED: 'addLabelFailed',
2522
ADD_LABEL_SUCCESS: 'addLabelSuccess',
2623
ADD_LABEL: 'addLabel',
@@ -200,37 +197,32 @@ export function fetchHearingEditorContactPersons() {
200197

201198
export const changeHearing = (field, value) => dispatch => dispatch(createAction(EditorActions.EDIT_HEARING)({ field, value }));
202199

203-
export function addContact(contact, selectedContacts) {
204-
return (dispatch) => {
205-
const postContactAction = createAction(EditorActions.ADD_CONTACT)();
206-
dispatch(postContactAction);
207-
const url = '/v1/contact_person/';
208-
return post(url, contact)
209-
.then(checkResponseStatus)
210-
.then(response => {
211-
if (response.status === 400) {
212-
response.json().then(errors => {
213-
dispatch(createAction(EditorActions.ADD_CONTACT_FAILED)({ errors }));
214-
});
215-
// Bad request with error message
216-
throw Error('Tarkista yhteyshenkilön tiedot.');
217-
} else if (response.status === 401) {
218-
// Unauthorized
219-
throw Error('Et voi luoda yhteyshenkilöä.');
220-
} else {
221-
response.json().then(contactJSON => {
222-
selectedContacts.push(contactJSON.id);
223-
dispatch(createAction(EditorActions.ADD_CONTACT_SUCCESS)({ contact: contactJSON }));
224-
dispatch(changeHearing('contact_persons', selectedContacts));
225-
});
226-
// TODO: Add translations
227-
dispatch(addToast(createNotificationPayload(NOTIFICATION_TYPES.success, HEARING_CREATED_MESSAGE)));
228-
}
229-
})
230-
.then(() => dispatch(fetchHearingEditorContactPersons()))
231-
.catch(requestErrorHandler(dispatch));
232-
};
233-
}
200+
export const addContact = (contact, selectedContacts) => async dispatch => {
201+
const url = '/v1/contact_person/';
202+
return post(url, contact)
203+
.then(checkResponseStatus)
204+
.then(async response => {
205+
if (response.status === 400) {
206+
// Bad request with error message
207+
throw Error('Tarkista yhteyshenkilön tiedot.');
208+
} else if (response.status === 401) {
209+
// Unauthorized
210+
throw Error('Et voi luoda yhteyshenkilöä.');
211+
} else {
212+
response.json().then(async contactJSON => {
213+
selectedContacts.push(contactJSON.id);
214+
await dispatch(changeHearing('contact_persons', selectedContacts));
215+
await dispatch(fetchHearingEditorContactPersons());
216+
});
217+
// TODO: Add translations
218+
dispatch(addToast(createNotificationPayload(NOTIFICATION_TYPES.success, HEARING_CREATED_MESSAGE)));
219+
}
220+
})
221+
.catch((err) => {
222+
requestErrorHandler(dispatch)(err);
223+
return Promise.reject(err);
224+
}).finally(() => true)
225+
};
234226

235227
export function saveContact(contact) {
236228
return (dispatch) => {

src/actions/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export const requestErrorHandler = (
5858
payload = createNotificationPayload(NOTIFICATION_TYPES.error, err.message);
5959
}
6060
dispatch(addToast(payload));
61-
return false;
6261
}
6362

6463
export function fetchInitialHearingList(listId, endpoint, params) {

src/components/BaseCommentForm.jsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ const BaseCommentForm = ({
7171

7272
const dispatch = useDispatch();
7373

74-
const [formData, setFormData] = useState({
75-
collapsed: true,
74+
const defaultState = useMemo(() => ({
7675
commentText: '',
77-
nickname: defaultNickname || '',
76+
nickname: defaultNickname,
7877
imageTooBig: false,
7978
images: [],
8079
pinned: false,
@@ -84,6 +83,11 @@ const BaseCommentForm = ({
8483
mapCommentText: '',
8584
commentRequiredError: false,
8685
commentOrAnswerRequiredError: false,
86+
}), [defaultNickname]);
87+
88+
const [formData, setFormData] = useState({
89+
...defaultState,
90+
collapsed: true,
8791
});
8892

8993
/**
@@ -113,17 +117,8 @@ const BaseCommentForm = ({
113117
if (canComment) {
114118
setFormData({
115119
...formData,
120+
...defaultState,
116121
collapsed: !formData.collapsed,
117-
commentText: '',
118-
nickname: defaultNickname || '',
119-
imageTooBig: false,
120-
images: [],
121-
pinned: false,
122-
showAlert: true,
123-
hideName: false,
124-
mapCommentText: '',
125-
commentRequiredError: false,
126-
commentOrAnswerRequiredError: false,
127122
});
128123

129124
if (onOverrideCollapse instanceof Function) {
@@ -132,7 +127,7 @@ const BaseCommentForm = ({
132127
} else {
133128
dispatch(addToast(createLocalizedNotificationPayload(NOTIFICATION_TYPES.error, getSectionCommentingErrorMessage(section))));
134129
}
135-
}, [canComment, defaultNickname, formData, onOverrideCollapse, section, dispatch]);
130+
}, [canComment, defaultState, formData, onOverrideCollapse, section, dispatch]);
136131

137132
useEffect(() => {
138133
if (isUserAdmin) {
@@ -262,7 +257,7 @@ const BaseCommentForm = ({
262257
...formData,
263258
collapsed: false,
264259
commentText: '',
265-
nickname: defaultNickname || '',
260+
nickname: defaultNickname,
266261
imageTooBig: false,
267262
images: [],
268263
pinned: false,

src/components/Footer.jsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const Footer = (props) => {
2121

2222
const userIsAdmin = isAdmin(user);
2323

24+
const scrollToFn = () => window.scrollTo(0, 0);
25+
2426
return (
2527
<HDSFooter
2628
className={classNames(['footer', userIsAdmin && 'footer--is-admin'])}
@@ -33,10 +35,10 @@ const Footer = (props) => {
3335
}}
3436
>
3537
<HDSFooter.Navigation>
36-
<HDSFooter.Link label={<FormattedMessage id='hearingsHeaderText' />} href='/hearings/list' />
37-
<HDSFooter.Link label={<FormattedMessage id='hearingMapHeaderText' />} href='/hearings/map' />
38-
{userIsAdmin && <HDSFooter.Link label={<FormattedMessage id='ownHearings' />} href='/user-hearings' />}
39-
{user && <HDSFooter.Link label={<FormattedMessage id='userInfo' />} href='/user-profile' />}
38+
<HDSFooter.Link label={<FormattedMessage id='hearingsHeaderText' />} to='/hearings/list' as={Link} onClick={scrollToFn} />
39+
<HDSFooter.Link label={<FormattedMessage id='hearingMapHeaderText' />} to='/hearings/map' as={Link} onClick={scrollToFn} />
40+
{userIsAdmin && <HDSFooter.Link label={<FormattedMessage id='ownHearings' />} to='/user-hearings' as={Link} onClick={scrollToFn} />}
41+
{user && <HDSFooter.Link label={<FormattedMessage id='userInfo' />} to='/user-profile' as={Link} onClick={scrollToFn} />}
4042
</HDSFooter.Navigation>
4143
<HDSFooter.Utilities>
4244
<HDSFooter.Link
@@ -68,6 +70,7 @@ const Footer = (props) => {
6870
label={<FormattedMessage id='accessibilityLink' />}
6971
to={`/accessibility?lang=${language}`}
7072
as={Link}
73+
onClick={scrollToFn}
7174
/>
7275
<HDSFooter.Link
7376
label={<FormattedMessage id='dataProtection' />}
@@ -80,9 +83,10 @@ const Footer = (props) => {
8083
label={<FormattedMessage id='cookieManagementLink' />}
8184
to={`/cookies?lang=${language}`}
8285
as={Link}
86+
onClick={scrollToFn}
8387
/>
8488
)}
85-
<HDSFooter.Link label={<FormattedMessage id='infoHeaderText' />} to={`/info?lang=${language}`} as={Link} />
89+
<HDSFooter.Link label={<FormattedMessage id='infoHeaderText' />} to={`/info?lang=${language}`} as={Link} onClick={scrollToFn} />
8690
</HDSFooter.Base>
8791
</HDSFooter>
8892
);

src/components/Hearing/Section/SectionContainer.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import getUser from '../../../selectors/user';
4343
import 'react-image-lightbox/style.css';
4444
import { getApiTokenFromStorage, getApiURL, get as apiGet } from '../../../api';
4545
import LoadSpinner from '../../LoadSpinner';
46+
import { getNickname } from '../../../utils/user';
4647

4748
const SectionContainerComponent = ({
4849
editCommentFn,
@@ -418,7 +419,7 @@ const SectionContainerComponent = ({
418419
canFlag={isHearingAdmin()}
419420
onPostVote={onVoteComment}
420421
onPostFlag={onFlagComment}
421-
defaultNickname={user && user.displayName}
422+
defaultNickname={getNickname(user)}
422423
isSectionComments={section}
423424
onDeleteComment={handleDeleteClick}
424425
onEditComment={onEditComment}

src/components/SortableCommentList.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Icon from '../utils/Icon';
1414
import MapQuestionnaire from './plugins/MapQuestionnaire';
1515
import QuestionResults from './QuestionResults';
1616
import CommentForm from './BaseCommentForm';
17-
import { getNickname, getAuthorDisplayName } from '../utils/user';
17+
import { getAuthorDisplayName } from '../utils/user';
1818
import { getSectionCommentingMessage } from '../utils/section';
1919
import getUser from '../selectors/user';
2020

@@ -65,6 +65,7 @@ const SortableCommentListComponent = ({
6565
hearingGeojson,
6666
hearingSlug,
6767
displayVisualization,
68+
defaultNickname,
6869
intl,
6970
language,
7071
published,
@@ -359,7 +360,7 @@ const SortableCommentListComponent = ({
359360
canComment={canComment}
360361
hearingId={hearingId}
361362
onPostComment={onPostComment}
362-
defaultNickname={getNickname(user)}
363+
defaultNickname={defaultNickname}
363364
nicknamePlaceholder={getAuthorDisplayName(user) || intl.formatMessage({ id: 'anonymous' })}
364365
collapseForm={listState.collapseForm}
365366
section={section}
@@ -439,7 +440,7 @@ const SortableCommentListComponent = ({
439440
canVote={canVote}
440441
canFlag={canFlag}
441442
comments={sectionComments.results}
442-
defaultNickname={getNickname(user)}
443+
defaultNickname={defaultNickname}
443444
hearingId={hearingId}
444445
intl={intl}
445446
isLoading={listState.showLoader}
@@ -470,6 +471,7 @@ SortableCommentListComponent.propTypes = {
470471
canFlag: PropTypes.bool,
471472
closed: PropTypes.bool,
472473
displayVisualization: PropTypes.bool,
474+
defaultNickname: PropTypes.string,
473475
fetchAllComments: PropTypes.func,
474476
fetchComments: PropTypes.func,
475477
fetchMoreComments: PropTypes.func,

0 commit comments

Comments
 (0)