diff --git a/package.json b/package.json index a0091f073..683d42104 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/components/common/PublishButton.js b/src/components/common/PublishButton.js index de1050e40..3a3751a2f 100644 --- a/src/components/common/PublishButton.js +++ b/src/components/common/PublishButton.js @@ -20,9 +20,9 @@ const PublishButton = ({ itemId }) => { }; return ( - + { // 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); @@ -136,6 +139,7 @@ const ItemPublishConfiguration = ({ item, edit }) => { getValidationStatusFromItemValidations( ivByStatus, validationStatusesMap, + itemValidationData, ), ); } @@ -160,7 +164,7 @@ const ItemPublishConfiguration = ({ item, edit }) => { }; const handleRefresh = () => { - queryClient.invalidateQueries(buildItemValidationAndReviewsKey(itemId)); + queryClient.invalidateQueries(buildItemValidationAndReviewKey(itemId)); }; const publishItem = () => { diff --git a/src/components/item/publish/ItemPublishTab.js b/src/components/item/publish/ItemPublishTab.js index 9485092eb..c81d20149 100644 --- a/src/components/item/publish/ItemPublishTab.js +++ b/src/components/item/publish/ItemPublishTab.js @@ -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: { diff --git a/src/utils/itemValidation.js b/src/utils/itemValidation.js index 2a6079391..c6d2cfb75 100644 --- a/src/utils/itemValidation.js +++ b/src/utils/itemValidation.js @@ -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; }; diff --git a/yarn.lock b/yarn.lock index c78fb7f9b..7d3076b7d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -1929,7 +1929,7 @@ __metadata: uuid: 8.3.2 peerDependencies: react: ^17.0.0 - checksum: 79da7437aca9df3bac8faa344ba5ed5f0a101e9c70ea0032bdc6126cd23c80009a18b4a603b1d8c8460b2bf7e74eab7ded529e8b26a183a4cab732d7817da749 + checksum: 55503e8279b58e764fe35476cac2307d396a0326e1afda691ff1faaf43ed72b682c4b5903ffbed80fdb1e86b145889b9b9553d8fb775e0fd3d126c96c6875637 languageName: node linkType: hard @@ -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"