Skip to content

Commit

Permalink
feat: add hooks, refactor files
Browse files Browse the repository at this point in the history
  • Loading branch information
louisewang1 committed Apr 14, 2022
1 parent eef97dd commit eb39c56
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "AGPL-3.0-only",
"dependencies": {
"@graasp/chatbox": "github:graasp/graasp-chatbox.git",
"@graasp/query-client": "github:graasp/graasp-query-client.git",
"@graasp/query-client": "github:graasp/graasp-query-client.git#151/validationHooks",
"@graasp/translations": "github:graasp/graasp-translations.git",
"@graasp/ui": "github:graasp/graasp-ui.git",
"@graasp/utils": "github:graasp/graasp-utils.git",
Expand Down
4 changes: 2 additions & 2 deletions src/components/common/PublishButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const PublishButton = ({ itemId }) => {
};

return (
<Tooltip title={t('Share')}>
<Tooltip title={t('Publish')}>
<IconButton
aria-label="share"
aria-label="publish"
className={PUBLISH_ITEM_BUTTON_CLASS}
onClick={onClick}
id={buildPublishButtonId(itemId)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ import {
} from '../../../utils/itemTag';

const { DELETE_ITEM_TAG, POST_ITEM_TAG, POST_ITEM_VALIDATION } = MUTATION_KEYS;
const { buildItemValidationAndReviewsKey } = DATA_KEYS;
const { buildItemValidationAndReviewKey } = DATA_KEYS;
const {
useTags,
useItemTags,
useItemValidationAndReviews,
useItemValidationAndReview,
useItemValidationGroups,
useItemValidationStatuses,
useItemValidationReviewStatuses,
} = hooks;
Expand Down Expand Up @@ -100,16 +101,18 @@ const ItemPublishConfiguration = ({ item, edit }) => {

// get item validation data
const { data: itemValidationData, isLoading } =
useItemValidationAndReviews(itemId);
// remove iv records before the item is last updated
const validItemValidation = itemValidationData?.filter(
(entry) =>
new Date(entry.validationUpdatedAt) >= new Date(item?.get('updatedAt')),
);
useItemValidationAndReview(itemId);
// check if validation is still valid
const iVId =
new Date(itemValidationData?.createdAt) >= new Date(item?.get('updatedAt'))
? itemValidationData?.itemValidationId
: undefined;
// get item validation groups
const { data: itemValidationGroups } = useItemValidationGroups(iVId);

// group iv records by item validation status
const ivByStatus = validItemValidation?.groupBy(({ validationStatusId }) =>
validationStatusesMap?.get(validationStatusId),
const ivByStatus = itemValidationGroups?.groupBy(({ statusId }) =>
validationStatusesMap?.get(statusId),
);

const [itemValidationStatus, setItemValidationStatus] = useState(false);
Expand All @@ -136,6 +139,7 @@ const ItemPublishConfiguration = ({ item, edit }) => {
getValidationStatusFromItemValidations(
ivByStatus,
validationStatusesMap,
itemValidationData,
),
);
}
Expand All @@ -160,7 +164,7 @@ const ItemPublishConfiguration = ({ item, edit }) => {
};

const handleRefresh = () => {
queryClient.invalidateQueries(buildItemValidationAndReviewsKey(itemId));
queryClient.invalidateQueries(buildItemValidationAndReviewKey(itemId));
};

const publishItem = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/item/publish/ItemPublishTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { makeStyles } from '@material-ui/core';
import { isItemUpdateAllowedForUser } from '../../../utils/membership';
import { LayoutContext } from '../../context/LayoutContext';
import { CurrentUserContext } from '../../context/CurrentUserContext';
import ItemPublishConfiguration from '../sharing/ItemPublishConfiguration';
import ItemPublishConfiguration from './ItemPublishConfiguration';

const useStyles = makeStyles((theme) => ({
wrapper: {
Expand Down
45 changes: 21 additions & 24 deletions src/utils/itemValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,37 @@ import {
ITEM_VALIDATION_STATUSES,
} from '../config/constants';

export const processFailureValidations = (records, validationStatusesMap) => {
// first try to find successful validations, where ivrStatus is 'rejected'
const successfulRecord = records?.find(
(record) =>
validationStatusesMap.get(record.reviewStatusId) ===
ITEM_VALIDATION_REVIEW_STATUSES.REJECTED,
);
if (successfulRecord) return ITEM_VALIDATION_STATUSES.SUCCESS;

// try to find pending review
const pendingRecord = records?.find(
(record) =>
validationStatusesMap.get(record.reviewStatusId) ===
ITEM_VALIDATION_REVIEW_STATUSES.PENDING,
);
if (pendingRecord) return ITEM_VALIDATION_STATUSES.PENDING;
return ITEM_VALIDATION_STATUSES.FAILURE; // only failed records
export const processFailureValidations = (
validationStatusesMap,
itemValidationData,
) => {
switch (validationStatusesMap?.get(itemValidationData?.reviewStatusId)) {
case ITEM_VALIDATION_REVIEW_STATUSES.PENDING:
return ITEM_VALIDATION_STATUSES.PENDING;
case ITEM_VALIDATION_REVIEW_STATUSES.REJECTED:
return ITEM_VALIDATION_STATUSES.SUCCESS;
case ITEM_VALIDATION_REVIEW_STATUSES.ACCEPTED:
return ITEM_VALIDATION_STATUSES.FAILURE;
default:
return false;
}
};

export const getValidationStatusFromItemValidations = (
ivByStatus,
validationStatusesMap,
itemValidationData,
) => {
// first check if there exist any valid successful record
if (ivByStatus.get(ITEM_VALIDATION_STATUSES.SUCCESS))
return ITEM_VALIDATION_STATUSES.SUCCESS;
// then check if there exist any pending item validation or review
// first check if there exist any pending entry
if (ivByStatus.get(ITEM_VALIDATION_STATUSES.PENDING))
return ITEM_VALIDATION_STATUSES.PENDING;

const failureValidations = ivByStatus.get(ITEM_VALIDATION_STATUSES.FAILURE);
// only process when there is failed item validation records
if (failureValidations)
return processFailureValidations(failureValidations, validationStatusesMap);
if (failureValidations) {
return processFailureValidations(validationStatusesMap, itemValidationData);
}

return false;
// if no pending and no failed entry, validation is successful
return ITEM_VALIDATION_STATUSES.SUCCESS;
};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1914,9 +1914,9 @@ __metadata:
languageName: node
linkType: hard

"@graasp/query-client@github:graasp/graasp-query-client.git":
"@graasp/query-client@github:graasp/graasp-query-client.git#151/validationHooks":
version: 0.1.0
resolution: "@graasp/query-client@https://github.com/graasp/graasp-query-client.git#commit=1a7a124944787dd3a70c64e2c76e27b70b356fda"
resolution: "@graasp/query-client@https://github.com/graasp/graasp-query-client.git#commit=eef6b317561800c4b3771de9d3a5508d1acd47f5"
dependencies:
"@graasp/translations": "github:graasp/graasp-translations.git"
axios: 0.26.1
Expand All @@ -1929,7 +1929,7 @@ __metadata:
uuid: 8.3.2
peerDependencies:
react: ^17.0.0
checksum: 79da7437aca9df3bac8faa344ba5ed5f0a101e9c70ea0032bdc6126cd23c80009a18b4a603b1d8c8460b2bf7e74eab7ded529e8b26a183a4cab732d7817da749
checksum: 55503e8279b58e764fe35476cac2307d396a0326e1afda691ff1faaf43ed72b682c4b5903ffbed80fdb1e86b145889b9b9553d8fb775e0fd3d126c96c6875637
languageName: node
linkType: hard

Expand Down Expand Up @@ -9926,7 +9926,7 @@ __metadata:
"@cypress/code-coverage": 3.9.12
"@cypress/instrument-cra": 1.4.0
"@graasp/chatbox": "github:graasp/graasp-chatbox.git"
"@graasp/query-client": "github:graasp/graasp-query-client.git"
"@graasp/query-client": "github:graasp/graasp-query-client.git#151/validationHooks"
"@graasp/translations": "github:graasp/graasp-translations.git"
"@graasp/ui": "github:graasp/graasp-ui.git"
"@graasp/utils": "github:graasp/graasp-utils.git"
Expand Down

0 comments on commit eb39c56

Please sign in to comment.