diff --git a/web/src/app/util/QuerySetFavoriteButton.tsx b/web/src/app/util/QuerySetFavoriteButton.tsx index 1888aa13b7..21e6dd6c8d 100644 --- a/web/src/app/util/QuerySetFavoriteButton.tsx +++ b/web/src/app/util/QuerySetFavoriteButton.tsx @@ -1,8 +1,9 @@ import React, { useState } from 'react' -import { gql, useQuery, useMutation } from '@apollo/client' +import { gql, useQuery, useMutation } from 'urql' import { SetFavoriteButton } from './SetFavoriteButton' import { Button, Dialog, DialogActions, DialogTitle } from '@mui/material' import DialogContentError from '../dialogs/components/DialogContentError' +import toTitleCase from './toTitleCase' const queries = { service: gql` @@ -62,25 +63,33 @@ export function QuerySetFavoriteButton({ id, type, }: QuerySetFavoriteButtonProps): JSX.Element { - const { data, loading } = useQuery(queries[type], { + const [{ data, fetching }] = useQuery({ + query: queries[type], variables: { id }, }) const isFavorite = data && data.data && data.data.isFavorite const [showMutationErrorDialog, setShowMutationErrorDialog] = useState(false) - const [toggleFav, toggleFavStatus] = useMutation(mutation, { - variables: { - input: { target: { id, type }, favorite: !isFavorite }, - }, - onError: () => setShowMutationErrorDialog(true), - }) + const [toggleFavStatus, commit] = useMutation(mutation) return ( toggleFav()} + loading={!data && fetching} + onClick={() => { + console.log(toTitleCase(type)) + commit( + { + input: { target: { id, type }, favorite: !isFavorite }, + }, + { additionalTypenames: [toTitleCase(type)] }, + ).then((result) => { + if (result.error) { + setShowMutationErrorDialog(true) + } + }) + }} /> { - return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() + return txt.charAt(0).toUpperCase() + txt.slice(1) }) } diff --git a/web/src/cypress/e2e/alerts.cy.ts b/web/src/cypress/e2e/alerts.cy.ts index e9e3345106..3115c3e1bc 100644 --- a/web/src/cypress/e2e/alerts.cy.ts +++ b/web/src/cypress/e2e/alerts.cy.ts @@ -415,6 +415,8 @@ function testAlerts(screen: ScreenFormat): void { it('should set alert noise reasons', () => { // set all noise reasons, checking carefully because of async setState + cy.get('path[id=crane-body]').should('not.exist') + cy.get('[data-cy=loading-spinner]').should('not.exist') cy.get('body').should('contain.text', 'Is this alert noise?') cy.get('[data-cy="False positive"] input[type="checkbox"]').check() cy.get('[data-cy="False positive"] input[type="checkbox"]').should( @@ -438,7 +440,7 @@ function testAlerts(screen: ScreenFormat): void { cy.get('label').contains('False positive').should('not.exist') // see notice - const noticeTitle = 'Info: This alert has been marked as noise' + const noticeTitle = 'This alert has been marked as noise' cy.get('body').should('contain.text', noticeTitle) cy.get('body').should( 'contain.text',