Skip to content

Commit

Permalink
feat: #6470 don't double save changes (#6476)
Browse files Browse the repository at this point in the history
* feat: #6470 don't double save changes

* chore: pre-push fixes

* feat: #6470 refetch developer
  • Loading branch information
willmcvay authored Mar 24, 2022
1 parent f0f0f0c commit 820dcc1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from 'react'
import { CurrentMemberRootState } from '../../../../../reducers/current-member'
import { developerStub } from '../../../../../sagas/__stubs__/developer'
import { reduxTestState, render } from '../../../../../tests/react-testing'
import { defaultAppSavingParams } from '../../state/defaults'
import { getTitle, handleCloseModal, SubmitReviewModal } from '../submit-review-modal'

const mockUpdateDeveloper = jest.fn()
Expand Down Expand Up @@ -51,12 +50,20 @@ describe('SubmitReviewModal', () => {
},
},
})
expect(render(<SubmitReviewModal closeModal={jest.fn()} developer={developerStub} />)).toMatchSnapshot()
expect(
render(<SubmitReviewModal closeModal={jest.fn()} developer={developerStub} refetchDeveloper={jest.fn()} />),
).toMatchSnapshot()
})

it('should match a snapshot where a customer and status is pending', () => {
expect(
render(<SubmitReviewModal closeModal={jest.fn()} developer={{ ...developerStub, status: 'pending' }} />),
render(
<SubmitReviewModal
closeModal={jest.fn()}
developer={{ ...developerStub, status: 'pending' }}
refetchDeveloper={jest.fn()}
/>,
),
).toMatchSnapshot()
})

Expand All @@ -69,7 +76,9 @@ describe('SubmitReviewModal', () => {
},
} as CurrentMemberRootState,
})
expect(render(<SubmitReviewModal closeModal={jest.fn()} developer={developerStub} />)).toMatchSnapshot()
expect(
render(<SubmitReviewModal closeModal={jest.fn()} developer={developerStub} refetchDeveloper={jest.fn()} />),
).toMatchSnapshot()
})

it('should match a snapshot where a customer and status is incomplete and user is an admin', () => {
Expand All @@ -81,7 +90,9 @@ describe('SubmitReviewModal', () => {
},
} as CurrentMemberRootState,
})
expect(render(<SubmitReviewModal closeModal={jest.fn()} developer={developerStub} />)).toMatchSnapshot()
expect(
render(<SubmitReviewModal closeModal={jest.fn()} developer={developerStub} refetchDeveloper={jest.fn()} />),
).toMatchSnapshot()
})
})

Expand All @@ -101,24 +112,20 @@ describe('getTitle', () => {

describe('handleCloseModal', () => {
it('should close the modal', () => {
const setAppEditSaving = jest.fn()
const closeModal = jest.fn()
const curried = handleCloseModal(setAppEditSaving, closeModal, true)
const refetchDeveloper = jest.fn()
const curried = handleCloseModal(closeModal, refetchDeveloper, true)
curried()
expect(closeModal).toHaveBeenCalled()
expect(setAppEditSaving).toHaveBeenCalledWith({
...defaultAppSavingParams,
isRevalidating: true,
isListed: true,
})
expect(refetchDeveloper).toHaveBeenCalled()
})

it('should not close the modal', () => {
const setAppEditSaving = jest.fn()
const closeModal = jest.fn()
const curried = handleCloseModal(setAppEditSaving, closeModal, false)
const refetchDeveloper = jest.fn()
const curried = handleCloseModal(closeModal, refetchDeveloper, false)
curried()
expect(closeModal).not.toHaveBeenCalled()
expect(setAppEditSaving).not.toHaveBeenCalled()
expect(refetchDeveloper).not.toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const Helper: FC = () => {
},
})

const [developer] = useReapitGet<DeveloperModel>({
const [developer, , , refetchDeveloper] = useReapitGet<DeveloperModel>({
reapitConnectBrowserSession,
action: getActions(window.reapit.config.appEnv)[GetActionNames.getDeveloper],
uriParams: {
Expand Down Expand Up @@ -235,7 +235,9 @@ export const Helper: FC = () => {
</>
)}
<Modal title={getTitle(isCustomer, developerStatus)}>
{developer && <SubmitReviewModal developer={developer} closeModal={closeModal} />}
{developer && (
<SubmitReviewModal developer={developer} closeModal={closeModal} refetchDeveloper={refetchDeveloper} />
)}
</Modal>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useReapitConnect } from '@reapit/connect-session'
import { BodyText, InputGroup, ButtonGroup, Button, Table, elMb6 } from '@reapit/elements'
import { useReapitGet, useReapitUpdate } from '@reapit/utils-react'
import { GetActionNames, getActions, UpdateActionNames, updateActions } from '@reapit/utils-common'
import React, { Dispatch, FC, SetStateAction, useEffect } from 'react'
import React, { FC, useEffect } from 'react'
import { useSelector } from 'react-redux'
import { reapitConnectBrowserSession } from '../../../../core/connect-session'
import { selectIsCustomer } from '../../../../selector/auth'
Expand All @@ -12,12 +12,11 @@ import { useForm } from 'react-hook-form'
import { object, SchemaOf, string } from 'yup'
import errorMessages from '../../../../constants/error-messages'
import { yupResolver } from '@hookform/resolvers/yup'
import { AppSavingParams, useAppState } from '../state/use-app-state'
import { defaultAppSavingParams } from '../state/defaults'

interface SubmitReviewModalProps {
developer: DeveloperModel
closeModal: () => void
refetchDeveloper: () => void
}

const schema: SchemaOf<{ reapitReference: string }> = object().shape({
Expand All @@ -38,26 +37,14 @@ export const getTitle = (isCustomer: boolean, orgStatus?: string): string => {
}

export const handleCloseModal =
(
setAppEditSaving: Dispatch<SetStateAction<AppSavingParams>>,
closeModal: () => void,
updateDeveloperSuccess?: boolean,
) =>
() => {
(closeModal: () => void, refetchDeveloper: () => void, updateDeveloperSuccess?: boolean) => () => {
if (updateDeveloperSuccess) {
setAppEditSaving({
...defaultAppSavingParams,
isRevalidating: true,
isListed: true,
})

closeModal()
refetchDeveloper()
}
}

export const SubmitReviewModal: FC<SubmitReviewModalProps> = ({ closeModal, developer }) => {
const { appEditState } = useAppState()

export const SubmitReviewModal: FC<SubmitReviewModalProps> = ({ closeModal, refetchDeveloper, developer }) => {
const {
register,
handleSubmit,
Expand All @@ -74,7 +61,6 @@ export const SubmitReviewModal: FC<SubmitReviewModalProps> = ({ closeModal, deve

const { connectSession } = useReapitConnect(reapitConnectBrowserSession)
const currentUser = useSelector(selectCurrentMemberData)
const { setAppEditSaving } = appEditState

const [members] = useReapitGet<MemberModelPagedResult>({
reapitConnectBrowserSession,
Expand All @@ -101,7 +87,7 @@ export const SubmitReviewModal: FC<SubmitReviewModalProps> = ({ closeModal, deve
},
})

useEffect(handleCloseModal(setAppEditSaving, closeModal, updateDeveloperSuccess), [updateDeveloperSuccess])
useEffect(handleCloseModal(closeModal, refetchDeveloper, updateDeveloperSuccess), [updateDeveloperSuccess])

const isCustomer = selectIsCustomer(connectSession)
const userRole = currentUser.role
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/developer-portal/src/tests/badges/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 820dcc1

Please sign in to comment.