Skip to content

Commit

Permalink
ui: update SetFavoriteButton and graphql.test.js to urql (#3584)
Browse files Browse the repository at this point in the history
* update setfavoritebutton to urql

* make check

* convert WizardRouter from Apollo to urql

* Revert "convert WizardRouter from Apollo to urql"

This reverts commit f15d6b3.

* add loading check

* remove only

* remove info from notice title
  • Loading branch information
Forfold authored Jan 11, 2024
1 parent 23c19fb commit 0b0289f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
29 changes: 19 additions & 10 deletions web/src/app/util/QuerySetFavoriteButton.tsx
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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 (
<React.Fragment>
<SetFavoriteButton
typeName={type}
isFavorite={isFavorite}
loading={!data && loading}
onClick={() => 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)
}
})
}}
/>
<Dialog
// if showMutationErrorDialog is true, the dialog will open
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/util/graphql.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from '@apollo/client'
import { gql } from 'urql'
import { print } from 'graphql'

import {
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/util/toTitleCase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function toTitleCase(str: string): string {
return str.replace(/\w\S*/g, (txt) => {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
return txt.charAt(0).toUpperCase() + txt.slice(1)
})
}
4 changes: 3 additions & 1 deletion web/src/cypress/e2e/alerts.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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',
Expand Down

0 comments on commit 0b0289f

Please sign in to comment.