Skip to content

Commit

Permalink
fix(KUI-1378): the json.stringify is now removed and more memorizatio…
Browse files Browse the repository at this point in the history
…n has been used
  • Loading branch information
amirhossein-haerian committed Aug 29, 2024
1 parent 73deb9c commit f1e2293
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
19 changes: 11 additions & 8 deletions public/js/app/hooks/__tests__/useApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ describe('useApi', () => {
})

test('calls apiToCall with given parameters', async () => {
const { result } = renderHook(() => useApi(apiToCall, { one: 'one', two: 2 }, null, null))
const apiParams1 = { one: 'one', two: 2 }
const { result } = renderHook(() => useApi(apiToCall, apiParams1, null, null))

expect(apiToCall).toHaveBeenLastCalledWith({ one: 'one', two: 2 })
expect(apiToCall).toHaveBeenLastCalledWith(apiParams1)

await waitFor(() => expect(result.current.data).toStrictEqual('somePlannedModules'))

Expand All @@ -50,15 +51,18 @@ describe('useApi', () => {
data: 'someData',
})

const { result: result2 } = renderHook(() => useApi(anotherApiToCall, { two: 'two', three: 3 }, null, null))
const apiParams2 = { two: 'two', three: 3 }

expect(anotherApiToCall).toHaveBeenLastCalledWith({ two: 'two', three: 3 })
const { result: result2 } = renderHook(() => useApi(anotherApiToCall, apiParams2, null, null))

expect(anotherApiToCall).toHaveBeenLastCalledWith(apiParams2)

await waitFor(() => expect(result2.current.data).toStrictEqual('someData'))
})

test('returns data from apiToCall', async () => {
const { result } = renderHook(() => useApi(apiToCall, {}, null, null))
const params = {}
const { result } = renderHook(() => useApi(apiToCall, params, null, null))

await waitFor(() => expect(result.current.data).toStrictEqual('somePlannedModules'))
})
Expand Down Expand Up @@ -192,9 +196,8 @@ describe('useApi', () => {

await waitFor(() => expect(firstResult.current.isError).toStrictEqual(true))

const { result: secodResult } = renderHook(() =>
useApi(apiToCall, { ...defaultParams, applicationCode: 54321 }, null, null)
)
const newParams = { ...defaultParams, applicationCode: 54321 }
const { result: secodResult } = renderHook(() => useApi(apiToCall, newParams, null, null))

await waitFor(() => expect(secodResult.current.isError).toStrictEqual(false))
jest.advanceTimersByTime(100)
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/hooks/useApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useApi = (apiToCall, apiParams, defaultValue, defaulValueIfNullResp
// we do not want to react on defaultValue and defaulValueIfNullResponse, because otherwise
// we cannot use empty objects
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [apiToCall, JSON.stringify(apiParams)])
}, [apiToCall, apiParams])

return {
data,
Expand Down
18 changes: 12 additions & 6 deletions public/js/app/hooks/useCourseEmployees.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMemo } from 'react'
import { useWebContext } from '../context/WebContext'
import { useApi } from './useApi'
import { getCourseEmployees } from './api/getCourseEmployees'
Expand All @@ -7,12 +8,17 @@ export const useCourseEmployees = ({ courseCode, selectedSemester, applicationCo

const { uri } = context.paths.api.employees

const { data, isError, isLoading } = useApi(getCourseEmployees, {
uri,
courseCode,
selectedSemester,
applicationCode,
})
const requestData = useMemo(
() => ({
uri,
courseCode,
selectedSemester,
applicationCode,
}),
[uri, courseCode, selectedSemester, applicationCode]
)

const { data, isError, isLoading } = useApi(getCourseEmployees, requestData)

return {
courseRoundEmployees: data,
Expand Down
16 changes: 11 additions & 5 deletions public/js/app/hooks/usePlannedModules.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMemo } from 'react'
import { useWebContext } from '../context/WebContext'
import { getPlannedModules } from './api/getPlannedModules'
import { useApi } from './useApi'
Expand All @@ -11,13 +12,18 @@ export const usePlannedModules = ({ courseCode, semester, applicationCode }) =>

const basePath = context.paths.api.plannedSchemaModules.uri

const { data, isError, isLoading } = useApi(
getPlannedModules,
{ basePath, courseCode, semester, applicationCode },
null,
MISSING_INFO
const requestData = useMemo(
() => ({
basePath,
courseCode,
semester,
applicationCode,
}),
[basePath, courseCode, semester, applicationCode]
)

const { data, isError, isLoading } = useApi(getPlannedModules, requestData, null, MISSING_INFO)

const plannedModules = data === MISSING_INFO ? missingInfoLabel : data

return {
Expand Down

0 comments on commit f1e2293

Please sign in to comment.