Skip to content

Commit

Permalink
Merge pull request #443 from commons-stack/fix/quantify_slider_displa…
Browse files Browse the repository at this point in the history
…ying_praise_score

Fix/quantify slider displaying praise score
  • Loading branch information
kristoferlund authored Jun 6, 2022
2 parents bba8c1a + ae6f6f4 commit 9a7cbd0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { PraiseDto } from 'api/dist/praise/types';
import Notice from '@/components/Notice';
import Praise from '@/components/praise/Praise';
import { ActiveUserId } from '@/model/auth';
import {
findPraiseQuantification,
shortenDuplicatePraiseId,
} from '@/utils/praise';
import { faCopy } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { useRecoilValue } from 'recoil';
import QuantifySlider from './QuantifySlider';

interface Props {
praise: PraiseDto;
periodId: string;
usePseudonyms: boolean;
allowedValues: number[];
checked?: boolean;
onToggleCheck();
onSetScore(newScore: number);
onDuplicateClick();
}

const QuantifyPraiseRow = ({
praise,
periodId,
usePseudonyms,
allowedValues,
checked = false,
onToggleCheck,
onSetScore,
onDuplicateClick,
}: Props): JSX.Element | null => {
const userId = useRecoilValue(ActiveUserId);
if (!userId) return null;

const quantification = findPraiseQuantification(praise, userId);
if (!quantification) return null;

const dismissed = quantification.dismissed;
const duplicate = !!quantification.duplicatePraise;

return (
<tr className="group">
<td>
<input
type="checkbox"
className="mr-4 text-xl w-5 h-5"
checked={checked}
onChange={onToggleCheck}
/>
</td>
<td>
<Praise
praise={praise}
showIdPrefix={true}
showReceiver={false}
periodId={periodId}
usePseudonyms={usePseudonyms}
dismissed={dismissed}
shortDuplicatePraiseId={shortenDuplicatePraiseId(praise, userId)}
/>
</td>
<td>
{duplicate ? (
<Notice type="info" className="w-40 py-2">
<>
Duplicate score: <br />
{quantification.scoreRealized}
</>
</Notice>
) : (
<QuantifySlider
allowedScores={allowedValues}
score={quantification.scoreRealized}
disabled={dismissed || duplicate}
onChange={onSetScore}
/>
)}
</td>
<td>
<div className="w-3">
<button
className="hidden group-hover:block text-gray-400 hover:text-gray-500 cursor-pointer"
disabled={duplicate}
onClick={onDuplicateClick}
>
<FontAwesomeIcon icon={faCopy} size="1x" />
</button>
</div>
</td>
</tr>
);
};

export default QuantifyPraiseRow;
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import Notice from '@/components/Notice';
import { PraiseDto } from 'api/dist/praise/types';
import { PeriodQuantifierReceiverPraise } from '@/model/periods';
import { useQuantifyPraise } from '@/model/praise';
import { usePeriodSettingValueRealized } from '@/model/periodsettings';
import Praise from '@/components/praise/Praise';
import { dismissed, duplicate, shortDuplicatePraiseId } from '@/utils/praise';
import { ActiveUserId } from '@/model/auth';
import { faCopy } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import getWeek from 'date-fns/getWeek';
import parseISO from 'date-fns/parseISO';
import { groupBy, sortBy } from 'lodash';
Expand All @@ -16,10 +10,10 @@ import { useRecoilValue } from 'recoil';
import { QuantifyBackNextLink } from './BackNextLink';
import DismissDialog from './DismissDialog';
import DuplicateDialog from './DuplicateDialog';
import QuantifySlider from './QuantifySlider';
import DuplicateSearchDialog from './DuplicateSearchDialog';
import MarkDuplicateButton from './MarkDuplicateButton';
import MarkDismissedButton from './MarkDismissedButton';
import QuantifyPraiseRow from './QuantifyPraiseRow';

interface Props {
periodId: string;
Expand All @@ -28,7 +22,6 @@ interface Props {
}

const QuantifyTable = ({ periodId, receiverId }: Props): JSX.Element | null => {
const userId = useRecoilValue(ActiveUserId);
const data = useRecoilValue(
PeriodQuantifierReceiverPraise({ periodId, receiverId })
);
Expand All @@ -53,7 +46,6 @@ const QuantifyTable = ({ periodId, receiverId }: Props): JSX.Element | null => {
) as number[];

if (!data) return null;
if (!userId) return null;

const handleDismiss = (): void => {
if (selectedPraises.length > 0) {
Expand Down Expand Up @@ -141,67 +133,21 @@ const QuantifyTable = ({ periodId, receiverId }: Props): JSX.Element | null => {
</tr>
)}

{weeklyData[weekKey].map((praise, index) => (
<tr className="group" key={index}>
<td>
<input
type="checkbox"
className="mr-4 text-xl w-5 h-5"
checked={isChecked(praise)}
onChange={(): void => handleToggleCheckbox(praise)}
/>
</td>
<td>
<Praise
praise={praise}
showIdPrefix={true}
showReceiver={false}
periodId={periodId}
usePseudonyms={usePseudonyms}
dismissed={dismissed(praise, userId)}
shortDuplicatePraiseId={shortDuplicatePraiseId(
praise,
userId
)}
/>
</td>
<td>
{duplicate(praise, userId) ? (
<Notice type="info" className="w-40 py-2">
<>
Duplicate score: <br />
{praise.scoreRealized}
</>
</Notice>
) : (
<QuantifySlider
allowedScores={allowedValues}
score={praise.scoreRealized}
disabled={
dismissed(praise, userId) ||
duplicate(praise, userId)
}
onChange={(newScore): void =>
handleSetScore(praise, newScore)
}
/>
)}
</td>
<td>
<div className="w-3">
<button
className="hidden group-hover:block text-gray-400 hover:text-gray-500 cursor-pointer"
disabled={duplicate(praise, userId)}
onClick={(): void => {
setDuplicateSearchDialogPraise(praise);
setIsDuplicateSearchDialogOpen(true);
}}
>
<FontAwesomeIcon icon={faCopy} size="1x" />
</button>
</div>
</td>
</tr>
{weeklyData[weekKey].map((praise) => (
<QuantifyPraiseRow
key={praise._id}
praise={praise}
periodId={periodId}
usePseudonyms={usePseudonyms}
allowedValues={allowedValues}
checked={isChecked(praise)}
onToggleCheck={(): void => handleToggleCheckbox(praise)}
onSetScore={(score): void => handleSetScore(praise, score)}
onDuplicateClick={(): void => {
setDuplicateSearchDialogPraise(praise);
setIsDuplicateSearchDialogOpen(true);
}}
/>
))}
</React.Fragment>
))}
Expand Down
16 changes: 3 additions & 13 deletions packages/frontend/src/utils/praise.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { PraiseDto, QuantificationDto } from 'api/dist/praise/types';

export const quantification = (
export const findPraiseQuantification = (
praise: PraiseDto,
userId: string
): QuantificationDto | undefined => {
return praise.quantifications.find((q) => q.quantifier === userId);
};

export const dismissed = (praise: PraiseDto, userId: string): boolean => {
const q = quantification(praise, userId);
return q ? !!q.dismissed : false;
};

export const duplicate = (praise: PraiseDto, userId: string): boolean => {
const q = quantification(praise, userId);
return q ? (q.duplicatePraise ? true : false) : false;
};

export const shortDuplicatePraiseId = (
export const shortenDuplicatePraiseId = (
praise: PraiseDto,
userId: string
): string => {
const q = quantification(praise, userId);
const q = findPraiseQuantification(praise, userId);
return q && q.duplicatePraise ? q.duplicatePraise?.slice(-4) : '';
};

0 comments on commit 9a7cbd0

Please sign in to comment.