Skip to content

Commit

Permalink
Split useRequest into useRequest and useDeferredRequest (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
petertrr authored Aug 25, 2022
1 parent ded4752 commit ed22687
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private fun contestEnrollerComponent() = FC<ContestEnrollerProps> { props ->
})

val (availableOptions, setAvailableOptions) = useState(emptyList<String>())
useRequest(isDeferred = false) {
useRequest {
val availableVariants = get(
if (isContestSelector) {
"$apiUrl/contests/$organizationName/$projectName/eligible-contests"
Expand All @@ -171,9 +171,9 @@ private fun contestEnrollerComponent() = FC<ContestEnrollerProps> { props ->
it.decodeFromJsonString<List<String>>()
}
setAvailableOptions(availableVariants)
}()
}

val enrollRequest = useRequest {
val enrollRequest = useDeferredRequest {
val responseFromBackend = get(
"$apiUrl/contests/$contestName/enroll?organizationName=$organizationName&projectName=$projectName",
headers = Headers(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ external interface ManageUserRoleCardProps : Props {
fun manageUserRoleCardComponent() = FC<ManageUserRoleCardProps> { props ->
val (changeUsersFromGroup, setChangeUsersFromGroup) = useState(true)
val (usersFromGroup, setUsersFromGroup) = useState(emptyList<UserInfo>())
val getUsersFromGroup = useRequest(dependencies = arrayOf(changeUsersFromGroup)) {
val getUsersFromGroup = useDeferredRequest {
val usersFromDb = get(
url = "$apiUrl/${props.groupType}s/${props.groupPath}/users",
headers = Headers().also {
Expand All @@ -103,7 +103,7 @@ fun manageUserRoleCardComponent() = FC<ManageUserRoleCardProps> { props ->
}

val (roleChange, setRoleChange) = useState(SetRoleRequest("", Role.NONE))
val updatePermissions = useRequest(dependencies = arrayOf(roleChange)) {
val updatePermissions = useDeferredRequest {
val headers = Headers().apply {
set("Accept", "application/json")
set("Content-Type", "application/json")
Expand All @@ -124,7 +124,7 @@ fun manageUserRoleCardComponent() = FC<ManageUserRoleCardProps> { props ->
val (userToAdd, setUserToAdd) = useState(UserInfo(""))
val (usersNotFromGroup, setUsersNotFromGroup) = useState(emptyList<UserInfo>())
val getUsersNotFromGroup = debounce(
useRequest(dependencies = arrayOf(changeUsersFromGroup, userToAdd)) {
useDeferredRequest {
val headers = Headers().apply {
set("Accept", "application/json")
set("Content-Type", "application/json")
Expand All @@ -141,7 +141,7 @@ fun manageUserRoleCardComponent() = FC<ManageUserRoleCardProps> { props ->
},
DEFAULT_DEBOUNCE_PERIOD,
)
val addUserToGroup = useRequest {
val addUserToGroup = useDeferredRequest {
val headers = Headers().apply {
set("Accept", "application/json")
set("Content-Type", "application/json")
Expand All @@ -163,7 +163,7 @@ fun manageUserRoleCardComponent() = FC<ManageUserRoleCardProps> { props ->
}

val (userToDelete, setUserToDelete) = useState(UserInfo(""))
val deleteUser = useRequest(dependencies = arrayOf(userToDelete)) {
val deleteUser = useDeferredRequest {
val headers = Headers().apply {
set("Accept", "application/json")
set("Content-Type", "application/json")
Expand All @@ -184,7 +184,7 @@ fun manageUserRoleCardComponent() = FC<ManageUserRoleCardProps> { props ->
}

val (selfRole, setSelfRole) = useState(Role.NONE)
useRequest(isDeferred = false) {
useRequest {
val role = get(
"$apiUrl/${props.groupType}s/${props.groupPath}/users/roles",
headers = Headers().also {
Expand All @@ -200,7 +200,7 @@ fun manageUserRoleCardComponent() = FC<ManageUserRoleCardProps> { props ->
props.showGlobalRoleWarning()
}
setSelfRole(getHighestRole(role, props.selfUserInfo.globalRole))
}()
}

runOnlyOnFirstRender {
getUsersFromGroup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ external interface SelectFormRequiredProps<D : Any> : Props {
fun <D : Any> selectFormRequired() = FC<SelectFormRequiredProps<D>> { props ->
val (elements, setElements) = useState(listOf<D>())

useRequest(arrayOf(), isDeferred = false) {
useRequest {
setElements((props.getData)())
}()
}

div {
className = ClassName("${props.classes} mt-1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fun ChildrenBuilder.showTestSuiteSourceCreationModal(
private fun testSuiteSourceCreationComponent() = FC<TestSuiteSourceCreationProps> { props ->
val (testSuiteSource, setTestSuiteSource) = useState(TestSuitesSourceDto.empty.copy(organizationName = props.organizationName))
val (saveStatus, setSaveStatus) = useState<SourceSaveStatus?>(null)
val fetchTestSuiteSource = useRequest {
val fetchTestSuiteSource = useDeferredRequest {
post(
url = "$apiUrl/test-suites-sources/${testSuiteSource.organizationName}/${testSuiteSource.name}/fetch",
headers = jsonHeaders,
Expand All @@ -116,7 +116,7 @@ private fun testSuiteSourceCreationComponent() = FC<TestSuiteSourceCreationProps
responseHandler = ::noopResponseHandler,
)
}
val onSubmitButtonPressed = useRequest {
val onSubmitButtonPressed = useDeferredRequest {
val response = post(
url = "/api/$v1/test-suites-sources/create",
headers = jsonHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private fun contestCreationComponent() = FC<ContestCreationComponentProps> { pro

val (conflictErrorMessage, setConflictErrorMessage) = useState<String?>(null)

val onSaveButtonPressed = useRequest {
val onSaveButtonPressed = useDeferredRequest {
val response = post(
"$apiUrl/${FrontendRoutes.CONTESTS.path}/create",
jsonHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ external interface ContestInfoMenuProps : Props {
@Suppress("TOO_LONG_FUNCTION", "LongMethod")
private fun contestInfoMenu() = FC<ContestInfoMenuProps> { props ->
val (contest, setContest) = useState<ContestDto?>(null)
useRequest(isDeferred = false) {
useRequest {
val contestDto = get(
"$apiUrl/contests/${props.contestName}",
headers = Headers().also {
Expand All @@ -49,7 +49,7 @@ private fun contestInfoMenu() = FC<ContestInfoMenuProps> { props ->
it.decodeFromJsonString<ContestDto>()
}
setContest(contestDto)
}()
}

div {
className = ClassName("d-flex justify-content-center")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ external interface ContestSummaryMenuProps : Props {
)
private fun contestSummaryMenu() = FC<ContestSummaryMenuProps> { props ->
val (results, setResults) = useState<List<ContestResult>>(emptyList())
useRequest(isDeferred = false) {
useRequest {
val projectResults = get(
url = "$apiUrl/contests/${props.contestName}/scores",
headers = Headers().also {
Expand All @@ -54,7 +54,7 @@ private fun contestSummaryMenu() = FC<ContestSummaryMenuProps> { props ->
}
.sortedByDescending { it.score }
setResults(projectResults)
}()
}
div {
className = ClassName("mb-3")
style = jso {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private fun publicTestComponent() = FC<PublicTestComponentProps> { props ->

val (avaliableTestSuites, setAvaliableTestSuites) = useState<List<TestSuiteDto>>(emptyList())
val (publicTest, setPublicTest) = useState<TestFilesContent?>(null)
useRequest(isDeferred = false) {
useRequest {
val response = get(
"$apiUrl/contests/${props.contestName}/test-suites",
jsonHeaders,
Expand All @@ -74,7 +74,7 @@ private fun publicTestComponent() = FC<PublicTestComponentProps> { props ->
setPublicTest(null)
setSelectedTestSuite(null)
}
}()
}

useRequest(dependencies = arrayOf(selectedTestSuite)) {
selectedTestSuite?.let { selectedTestSuite ->
Expand All @@ -91,7 +91,7 @@ private fun publicTestComponent() = FC<PublicTestComponentProps> { props ->
setPublicTest(TestFilesContent.empty)
}
}
}()
}

if (avaliableTestSuites.isEmpty()) {
h6 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ external interface ManageGitCredentialsCardProps : Props {
@Suppress("TOO_LONG_FUNCTION", "LongMethod")
fun manageGitCredentialsCardComponent() = FC<ManageGitCredentialsCardProps> { props ->
val (selfRole, setSelfRole) = useState(Role.NONE)
useRequest(isDeferred = false) {
useRequest {
val role = get(
"$apiUrl/organizations/${props.organizationName}/users/roles",
headers = jsonHeaders,
Expand All @@ -80,7 +80,7 @@ fun manageGitCredentialsCardComponent() = FC<ManageGitCredentialsCardProps> { pr
props.showGlobalRoleWarning()
}
setSelfRole(getHighestRole(role, props.selfUserInfo.globalRole))
}()
}

val (gitCredentials, _, fetchGitCredentialsRequest) = prepareFetchGitCredentials(props.organizationName)

Expand Down Expand Up @@ -194,7 +194,7 @@ fun manageGitCredentialsCardComponent() = FC<ManageGitCredentialsCardProps> { pr
@Suppress("TYPE_ALIAS")
private fun prepareFetchGitCredentials(organizationName: String): RequestWithDependency<List<GitDto>> {
val (gitCredentials, setGitCredentials) = useState(emptyList<GitDto>())
val fetchGitCredentialsRequest = useRequest {
val fetchGitCredentialsRequest = useDeferredRequest {
get(
"$apiUrl/organizations/$organizationName/list-git",
headers = jsonHeaders,
Expand All @@ -215,7 +215,7 @@ private fun prepareUpsertGitCredential(
fetchGitCredentialsRequest: () -> Unit
): RequestWithDependency<GitDto?> {
val (gitCredentialToUpsert, setGitCredentialToUpsert) = useState<GitDto?>(null)
val upsertGitCredentialRequest = useRequest(dependencies = arrayOf(gitCredentialToUpsert)) {
val upsertGitCredentialRequest = useDeferredRequest {
val headers = Headers().apply {
set("Accept", "application/json")
set("Content-Type", "application/json")
Expand All @@ -241,7 +241,7 @@ private fun prepareDeleteGitCredential(
fetchGitCredentialsRequest: () -> Unit
): RequestWithDependency<GitDto> {
val (gitCredentialToDelete, setGitCredentialToDelete) = useState(GitDto("N/A"))
val deleteGitCredentialRequest = useRequest(dependencies = arrayOf(gitCredentialToDelete)) {
val deleteGitCredentialRequest = useDeferredRequest {
val response = delete(
url = "$apiUrl/organizations/$organizationName/delete-git?url=${gitCredentialToDelete.url}",
headers = jsonHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private fun organizationTestsMenu() = FC<OrganizationTestsMenuProps> { props ->
val (isTestSuiteSourceCreationModalOpen, setIsTestSuitesSourceCreationModalOpen) = useState(false)
val (isSourceCreated, setIsSourceCreated) = useState(false)
val (testSuitesSources, setTestSuitesSources) = useState(emptyList<TestSuitesSourceDto>())
val fetchTestSuitesSources = useRequest(dependencies = arrayOf(props.organizationName, isSourceCreated)) {
useRequest(dependencies = arrayOf(props.organizationName, isSourceCreated)) {
val response = get(
url = "$apiUrl/test-suites-sources/${props.organizationName}/list",
headers = Headers().also {
Expand All @@ -66,9 +66,8 @@ private fun organizationTestsMenu() = FC<OrganizationTestsMenuProps> { props ->
setTestSuitesSources(emptyList())
}
}
fetchTestSuitesSources()
val (testSuiteSourceToFetch, setTestSuiteSourceToFetch) = useState<TestSuitesSourceDto?>(null)
val triggerFetchTestSuiteSource = useRequest(dependencies = arrayOf(testSuiteSourceToFetch)) {
val triggerFetchTestSuiteSource = useDeferredRequest {
testSuiteSourceToFetch?.let { testSuiteSource ->
post(
url = "$apiUrl/test-suites-sources/${testSuiteSource.organizationName}/${encodeURIComponent(testSuiteSource.name)}/fetch",
Expand All @@ -83,7 +82,7 @@ private fun organizationTestsMenu() = FC<OrganizationTestsMenuProps> { props ->

val (selectedTestSuitesSource, setSelectedTestSuitesSource) = useState<TestSuitesSourceDto?>(null)
val (testSuitesSourceSnapshotKeys, setTestSuitesSourceSnapshotKeys) = useState(emptyList<TestSuitesSourceSnapshotKey>())
val fetchTestSuitesSourcesSnapshotKeys = useRequest(dependencies = arrayOf(selectedTestSuitesSource)) {
val fetchTestSuitesSourcesSnapshotKeys = useDeferredRequest {
selectedTestSuitesSource?.let { testSuitesSource ->
val response = get(
url = "$apiUrl/test-suites-sources/${testSuitesSource.organizationName}/${encodeURIComponent(testSuitesSource.name)}/list-snapshot",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ external interface ProjectInfoMenuProps : Props {
)
private fun projectInfoMenu() = FC<ProjectInfoMenuProps> { props ->
val (usersInProject, setUsersInProject) = useState(emptyList<UserInfo>())
useRequest(isDeferred = false) {
useRequest {
val users: List<UserInfo> = get(
url = "$apiUrl/projects/${props.organizationName}/${props.projectName}/users",
headers = Headers().also {
Expand All @@ -70,10 +70,10 @@ private fun projectInfoMenu() = FC<ProjectInfoMenuProps> { props ->
it.decodeFromJsonString()
}
setUsersInProject(users)
}()
}

val (bestResults, setBestResults) = useState(emptyList<ContestResult>())
useRequest(isDeferred = false) {
useRequest {
val results: List<ContestResult> = get(
url = "$apiUrl/contests/${props.organizationName}/${props.projectName}/best",
headers = Headers().also {
Expand All @@ -85,10 +85,10 @@ private fun projectInfoMenu() = FC<ProjectInfoMenuProps> { props ->
it.decodeFromJsonString()
}
setBestResults(results)
}()
}

val (project, setProject) = useState(Project.stub(-1))
useRequest(isDeferred = false) {
useRequest {
val projectFromBackend: Project = get(
url = "$apiUrl/projects/get/organization-name?name=${props.projectName}&organizationName=${props.organizationName}",
headers = Headers().also {
Expand All @@ -100,7 +100,7 @@ private fun projectInfoMenu() = FC<ProjectInfoMenuProps> { props ->
it.decodeFromJsonString()
}
setProject(projectFromBackend)
}()
}

div {
className = ClassName("d-flex justify-content-center")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ external interface ProjectStatisticMenuProps : Props {
private fun projectStatisticMenu() = FC<ProjectStatisticMenuProps> { props ->
val (latestExecutionStatisticDtos, setLatestExecutionStatisticDtos) = useState(props.latestExecutionStatisticDtos)

useRequest(arrayOf(props.executionId, props.latestExecutionStatisticDtos, props.isOpen), isDeferred = false) {
useRequest(arrayOf(props.executionId, props.latestExecutionStatisticDtos, props.isOpen)) {
if (props.isOpen != true && props.executionId != null) {
val testLatestExecutions = get(
url = "$apiUrl/testLatestExecutions?executionId=${props.executionId}&status=${TestResultStatus.PASSED}",
Expand All @@ -95,7 +95,7 @@ private fun projectStatisticMenu() = FC<ProjectStatisticMenuProps> { props ->
}
setLatestExecutionStatisticDtos(testLatestExecutions)
}
}()
}

div {
className = ClassName("row justify-content-center")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private fun testSuiteSelectorBrowserMode() = FC<TestSuiteSelectorBrowserModeProp
val testSuitesSources: TestSuitesSourceDtoList = response.decodeFromJsonString()
setAvailableOrganizations(testSuitesSources.map { it.organizationName }.distinct())
setAvailableTestSuiteSources(testSuitesSources.map { it.name })
}()
}

val (availableTestSuitesVersions, setAvailableTestSuitesVersions) = useState<List<String>>(emptyList())
useRequest(dependencies = arrayOf(selectedTestSuiteSource)) {
Expand All @@ -196,7 +196,7 @@ private fun testSuiteSelectorBrowserMode() = FC<TestSuiteSelectorBrowserModeProp
setAvailableTestSuitesVersions(testSuiteSourcesVersions)
setSelectedTestSuiteVersion(testSuiteSourcesVersions.singleOrNull())
}
}()
}

val (availableTestSuites, setAvailableTestSuites) = useState<List<TestSuiteDto>>(emptyList())
useRequest(dependencies = arrayOf(selectedTestSuiteVersion)) {
Expand All @@ -223,7 +223,7 @@ private fun testSuiteSelectorBrowserMode() = FC<TestSuiteSelectorBrowserModeProp
}
}
}
}()
}

val (namePrefix, setNamePrefix) = useState("")
div {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ external interface TestSuiteSelectorManagerModeProps : Props {
private fun testSuiteSelectorManagerMode() = FC<TestSuiteSelectorManagerModeProps> { props ->
val (selectedTestSuites, setSelectedTestSuites) = useState<List<TestSuiteDto>>(emptyList())
val (preselectedTestSuites, setPreselectedTestSuites) = useState<List<TestSuiteDto>>(emptyList())
useRequest(isDeferred = false) {
useRequest {
val testSuitesFromBackend: List<TestSuiteDto> = post(
url = "$apiUrl/test-suites/get-by-ids",
headers = jsonHeaders,
Expand All @@ -52,7 +52,7 @@ private fun testSuiteSelectorManagerMode() = FC<TestSuiteSelectorManagerModeProp
.decodeFromJsonString()
setPreselectedTestSuites(testSuitesFromBackend)
setSelectedTestSuites(testSuitesFromBackend)
}()
}

if (preselectedTestSuites.isEmpty()) {
h6 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private fun ChildrenBuilder.buildInput(
private fun testSuiteSelectorSearchMode() = FC<TestSuiteSelectorSearchModeProps> { props ->
val (selectedTestSuites, setSelectedTestSuites) = useState<List<TestSuiteDto>>(emptyList())
val (filteredTestSuites, setFilteredTestSuites) = useState<List<TestSuiteDto>>(emptyList())
useRequest(isDeferred = false) {
useRequest {
val testSuitesFromBackend: List<TestSuiteDto> = post(
url = "$apiUrl/test-suites/get-by-ids",
headers = jsonHeaders,
Expand All @@ -69,11 +69,11 @@ private fun testSuiteSelectorSearchMode() = FC<TestSuiteSelectorSearchModeProps>
)
.decodeFromJsonString()
setSelectedTestSuites(testSuitesFromBackend)
}()
}

val (filters, setFilters) = useState(TestSuiteFilters.empty)
val getFilteredTestSuites = debounce(
useRequest(dependencies = arrayOf(filters)) {
useDeferredRequest {
if (filters.isNotEmpty()) {
val testSuitesFromBackend: List<TestSuiteDto> = get(
url = "$apiUrl/test-suites/filtered${
Expand Down
Loading

0 comments on commit ed22687

Please sign in to comment.