Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: VotesModal into hooks (#1001)

Benjamin Levesque6c9b5d2a1633
diff --git a/public/pages/ShowPost/components/VotesModal.tsx b/public/pages/ShowPost/components/VotesModal.tsx
index 16403baaa..12c801124 100644
--- a/public/pages/ShowPost/components/VotesModal.tsx
+++ b/public/pages/ShowPost/components/VotesModal.tsx
@@ -1,7 +1,8 @@
-import React from "react"
+import React, { useEffect, useState } from "react"
import { Post, Vote } from "@fider/models"
import { Modal, Button, Loader, Avatar, UserName, Moment, Input } from "@fider/components"
-import { actions, Fider } from "@fider/services"
+import { actions } from "@fider/services"
+import { useFider } from "@fider/hooks"
import IconSearch from "@fider/assets/images/heroicons-search.svg"
import IconX from "@fider/assets/images/heroicons-x.svg"
import { HStack, VStack } from "@fider/components/layout"
@@ -13,103 +14,88 @@ interface VotesModalProps {
onClose?: () => void
}
-interface VotesModalState {
- searchText: string
- allVotes: Vote[]
- filteredVotes: Vote[]
- isLoading: boolean
- query: string
-}
+export const VotesModal: React.FC<VotesModalProps> = (props) => {
+ const [isLoading, setIsLoading] = useState(false)
+ const [query, setQuery] = useState("")
+ const [allVotes, setAllVotes] = useState<Vote[]>([])
+ const [filteredVotes, setFilteredVotes] = useState<Vote[]>([])
-export class VotesModal extends React.Component<VotesModalProps, VotesModalState> {
- constructor(props: VotesModalProps) {
- super(props)
- this.state = {
- searchText: "",
- query: "",
- allVotes: [],
- filteredVotes: [],
- isLoading: true,
- }
- }
+ const fider = useFider()
- public componentDidUpdate(prevProps: VotesModalProps) {
- if (this.props.isOpen && !prevProps.isOpen) {
- actions.listVotes(this.props.post.number).then((response) => {
+ useEffect(() => {
+ if (props.isOpen) {
+ actions.listVotes(props.post.number).then((response) => {
if (response.ok) {
- this.setState({
- allVotes: response.data,
- filteredVotes: response.data,
- isLoading: false,
- })
+ setAllVotes(response.data)
+ setFilteredVotes(response.data)
+ setIsLoading(false)
}
})
}
- }
+ }, [props.isOpen])
- private closeModal = async () => {
- if (this.props.onClose) {
- this.props.onClose()
+ const closeModal = async () => {
+ if (props.onClose) {
+ props.onClose()
}
}
- private clearSearch = () => {
- this.handleSearchFilterChanged("")
+ const clearSearch = () => {
+ handleSearchFilterChanged("")
}
- private handleSearchFilterChanged = (query: string) => {
- const votes = this.state.allVotes.filter((x) => x.user.name.toLowerCase().indexOf(query.toLowerCase()) >= 0)
- this.setState({ query, filteredVotes: votes })
+ const handleSearchFilterChanged = (query: string) => {
+ const votes = allVotes.filter((x) => x.user.name.toLowerCase().indexOf(query.toLowerCase()) >= 0)
+ setQuery(query)
+ setFilteredVotes(votes)
}
- public render() {
- return (
- <Modal.Window isOpen={this.props.isOpen} center={false} onClose={this.closeModal}>
- <Modal.Content>
- {this.state.isLoading && <Loader />}
- {!this.state.isLoading && (
- <>
- <Input
- field="query"
- icon={this.state.query ? IconX : IconSearch}
- onIconClick={this.state.query ? this.clearSearch : undefined}
- placeholder={t({ id: "modal.showvotes.query.placeholder", message: "Search for users by name..." })}
- value={this.state.query}
- onChange={this.handleSearchFilterChanged}
- />
- <VStack spacing={2} className="h-max-5xl overflow-scroll">
- {this.state.filteredVotes.map((x) => (
- <HStack key={x.user.id} justify="between">
- <HStack>
- <Avatar user={x.user} />
- <VStack spacing={0}>
- <UserName user={x.user} />
- <span className="text-muted">{x.user.email}</span>
- </VStack>
- </HStack>
- <span className="text-muted">
- <Moment locale={Fider.currentLocale} date={x.createdAt} />
- </span>
+ return (
+ <Modal.Window isOpen={props.isOpen} center={false} onClose={closeModal}>
+ <Modal.Content>
+ {isLoading && <Loader />}
+ {!isLoading && (
+ <>
+ <Input
+ field="query"
+ icon={query ? IconX : IconSearch}
+ onIconClick={query ? clearSearch : undefined}
+ placeholder={t({ id: "modal.showvotes.query.placeholder", message: "Search for users by name..." })}
+ value={query}
+ onChange={handleSearchFilterChanged}
+ />
+ <VStack spacing={2} className="h-max-5xl overflow-scroll">
+ {filteredVotes.map((x) => (
+ <HStack key={x.user.id} justify="between">
+ <HStack>
+ <Avatar user={x.user} />
+ <VStack spacing={0}>
+ <UserName user={x.user} />
+ <span className="text-muted">{x.user.email}</span>
+ </VStack>
</HStack>
- ))}
- {this.state.filteredVotes.length === 0 && (
- <p className="text-muted">
- <Trans id="modal.showvotes.message.zeromatches">
- No users found matching <strong>{this.state.query}</strong>.
- </Trans>
- </p>
- )}
- </VStack>
- </>
- )}
- </Modal.Content>
+ <span className="text-muted">
+ <Moment locale={fider.currentLocale} date={x.createdAt} />
+ </span>
+ </HStack>
+ ))}
+ {filteredVotes.length === 0 && (
+ <p className="text-muted">
+ <Trans id="modal.showvotes.message.zeromatches">
+ No users found matching <strong>{query}</strong>.
+ </Trans>
+ </p>
+ )}
+ </VStack>
+ </>
+ )}
+ </Modal.Content>
- <Modal.Footer>
- <Button variant="tertiary" onClick={this.closeModal}>
- <Trans id="action.close">Close</Trans>
- </Button>
- </Modal.Footer>
- </Modal.Window>
- )
- }
+ <Modal.Footer>
+ <Button variant="tertiary" onClick={closeModal}>
+ <Trans id="action.close">Close</Trans>
+ </Button>
+ </Modal.Footer>
+ </Modal.Window>
+ )
}