Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 60 additions & 31 deletions web-ui/src/components/private-note/PrivateNote.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import {
selectIsPDL,
selectIsAdmin,
selectCheckin,
selectProfile
selectProfile,
selectCanViewPrivateNotesPermission,
selectCanCreatePrivateNotesPermission,
selectCanUpdatePrivateNotesPermission,
} from '../../context/selectors';
import { UPDATE_TOAST } from '../../context/actions';
import { debounce } from 'lodash/function';
import { Editor } from '@tinymce/tinymce-react';
import LockIcon from '@mui/icons-material/Lock';
Expand Down Expand Up @@ -49,19 +53,36 @@ const PrivateNote = () => {

useEffect(() => {
async function getPrivateNotes() {
setIsLoading(true);
try {
let res = await getPrivateNoteByCheckinId(checkinId, csrf);
if (res.error) throw new Error(res.error);
const currentNote =
res.payload && res.payload.data && res.payload.data.length > 0
? res.payload.data[0]
: null;
if (currentNote) {
setNote(currentNote);
} else if (currentUserId === pdlId) {
if (!noteRef.current.some(id => id === checkinId)) {
noteRef.current.push(checkinId);
if (selectCanViewPrivateNotesPermission(state)) {
setIsLoading(true);
try {
let res = await getPrivateNoteByCheckinId(checkinId, csrf);
if (res.error) throw new Error(res.error);
const currentNote =
res.payload && res.payload.data && res.payload.data.length > 0
? res.payload.data[0]
: null;
if (currentNote) {
setNote(currentNote);
} else if (currentUserId === pdlId) {
if (!noteRef.current.some(id => id === checkinId) &&
selectCanCreatePrivateNotesPermission(state)) {
noteRef.current.push(checkinId);
res = await createPrivateNote(
{
checkinid: checkinId,
createdbyid: currentUserId,
description: ''
},
csrf
);
noteRef.current = noteRef.current.filter(id => id !== checkinId);
if (res.error) throw new Error(res.error);
if (res && res.payload && res.payload.data) {
setNote(res.payload.data);
}
}
} else if (selectCanCreatePrivateNotesPermission(state)) {
res = await createPrivateNote(
{
checkinid: checkinId,
Expand All @@ -70,37 +91,45 @@ const PrivateNote = () => {
},
csrf
);
noteRef.current = noteRef.current.filter(id => id !== checkinId);
if (res.error) throw new Error(res.error);
if (res && res.payload && res.payload.data) {
setNote(res.payload.data);
}
}
} else {
res = await createPrivateNote(
{
checkinid: checkinId,
createdbyid: currentUserId,
description: ''
},
csrf
);
if (res.error) throw new Error(res.error);
if (res && res.payload && res.payload.data) {
setNote(res.payload.data);
}
} catch (e) {
console.error("getPrivateNotes: " + e);
}
} catch (e) {
console.error("getPrivateNotes: " + e);
setIsLoading(false);
}
setIsLoading(false);
}
if (csrf) {
getPrivateNotes();
}
}, [csrf, checkinId, currentUserId, pdlId]);

const handleNoteChange = (content, delta, source, editor) => {
if (note == null) {
window.snackDispatch({
type: UPDATE_TOAST,
payload: {
severity: 'error',
toast: selectCanCreatePrivateNotesPermission(state)
? 'No private note was created'
: 'No permission to create private notes'
}
});
return;
}
if (!selectCanUpdatePrivateNotesPermission(state)) {
window.snackDispatch({
type: UPDATE_TOAST,
payload: {
severity: 'error',
toast: 'No permission to update private notes'
}
});
return;
}
if (Object.keys(note).length === 0 || !csrf || currentCheckin?.completed) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion web-ui/src/context/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ export const reducer = (state, action) => {
state.userProfile.memberProfile.bioText = action.payload;
break;
case ADD_CHECKIN:
state.checkins = [...state.checkins, action.payload];
if (state?.checkins?.length > 0) {
state.checkins = [...state.checkins, action.payload];
} else {
state.checkins = [action.payload];
}
break;
case UPDATE_CHECKINS:
if (state?.checkins?.length > 0) {
Expand Down
12 changes: 12 additions & 0 deletions web-ui/src/context/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ export const selectCanViewCheckinsPermission = hasPermission(
'CAN_VIEW_CHECKINS'
);

export const selectCanViewPrivateNotesPermission = hasPermission(
'CAN_VIEW_PRIVATE_NOTE'
);

export const selectCanCreatePrivateNotesPermission = hasPermission(
'CAN_CREATE_PRIVATE_NOTE'
);

export const selectCanUpdatePrivateNotesPermission = hasPermission(
'CAN_UPDATE_PRIVATE_NOTE'
);

export const selectIsPDL = createSelector(
selectUserProfile,
userProfile =>
Expand Down
2 changes: 2 additions & 0 deletions web-ui/src/pages/CheckinsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
selectProfile,
selectCheckinsForMember,
selectCanViewCheckinsPermission,
selectCanViewPrivateNotesPermission,
} from '../context/selectors';
import { getCheckins, createNewCheckin } from '../context/thunks';
import { UPDATE_CHECKIN, UPDATE_TOAST } from '../context/actions';
Expand Down Expand Up @@ -91,6 +92,7 @@ const CheckinsPage = () => {
const isPdl = selectIsPDL(state);

const canViewPrivateNote =
selectCanViewPrivateNotesPermission(state) &&
(isAdmin || selectedProfile?.pdlId === currentUserId) &&
currentUserId !== memberId;

Expand Down
16 changes: 11 additions & 5 deletions web-ui/src/pages/ErrorBoundaryPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@ const ErrorFallback = ({ error }) => {
//before upload to server
let sanitizeBody = sanitizeQuillElements(body);
let res = await newGitHubIssue(sanitizeBody, title, csrf);
if (res && res.payload) {
if (res?.error) {
window.snackDispatch({
type: UPDATE_TOAST,
payload: {
severity: 'error',
toast: res.error.message,
}
});
} else if (res?.payload?.data) {
setLink(res.payload.data[0].html_url);
window.snackDispatch({
type: UPDATE_TOAST,
payload: {
severity: !res.error ? 'success' : 'error',
toast: !res.error
? `New issue ${title} created! Gratzie &#128512`
: res.error.message
severity: 'success',
toast: `New issue ${title} created! Gratzie &#128512`,
}
});
}
Expand Down
Loading