-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: autocomplete tags reusable component
- Loading branch information
1 parent
38ca69d
commit 41336c1
Showing
6 changed files
with
116 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
import { api, handleApiError } from '@/api' | ||
import { movies } from '@/data' | ||
import { sleep } from '@/lib/utils' | ||
|
||
export const getTrendingTags = async () => { | ||
try { | ||
const { data } = await api.get('/tags/trending') | ||
return data | ||
} catch (error) { | ||
try { | ||
const { data } = await api.get('/tags/trending') | ||
return data | ||
} catch (error) { | ||
throw handleApiError(error) | ||
} | ||
} | ||
} | ||
|
||
export const searchTags = async (searchTerm) => { | ||
try { | ||
// Filter movies based on search term | ||
return movies | ||
.filter((movie) => | ||
movie.title | ||
.toLowerCase() | ||
.includes(searchTerm?.toLowerCase() || '') | ||
) | ||
.map((movie) => movie.title) | ||
} catch (error) { | ||
throw handleApiError(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useSearchTags } from '@/hooks' | ||
import { AutoAwesome } from '@mui/icons-material' | ||
import { | ||
Autocomplete, | ||
CircularProgress, | ||
IconButton, | ||
InputAdornment, | ||
TextField | ||
} from '@mui/material' | ||
import { useState } from 'react' | ||
|
||
const TagsAutocompleteInput = ({ formData, setFormData, error }) => { | ||
const [input, setInput] = useState('') | ||
|
||
const { data: options = [], isLoading } = useSearchTags(input) | ||
|
||
const generateTags = () => { | ||
const aIGenerateTags = ['social', 'media', 'post'] | ||
setFormData({ | ||
...formData, | ||
tags: [ | ||
...new Set([...formData.tags, ...aIGenerateTags].slice(0, 8)) | ||
] | ||
}) | ||
} | ||
const handleChange = (_, value) => | ||
setFormData((prevData) => ({ | ||
...prevData, | ||
tags: value.length > 8 ? value.slice(-8) : value | ||
})) | ||
|
||
return ( | ||
<Autocomplete | ||
multiple | ||
freeSolo | ||
options={options} | ||
loading={isLoading} | ||
inputValue={input} | ||
value={formData.tags} | ||
onChange={handleChange} | ||
onInputChange={(_, newInput) => setInput(newInput)} | ||
disableClearable | ||
renderInput={(params) => ( | ||
<TextField | ||
{...params} | ||
label="Tags" | ||
error={error} | ||
slotProps={{ | ||
input: { | ||
...params.InputProps, | ||
type: 'search', | ||
endAdornment: ( | ||
<InputAdornment position="end"> | ||
{isLoading ? ( | ||
<CircularProgress size={20} /> | ||
) : ( | ||
<IconButton | ||
onClick={generateTags} | ||
disabled={formData.tags.length >= 8} | ||
edge="end" | ||
size="small" | ||
> | ||
<AutoAwesome /> | ||
</IconButton> | ||
)} | ||
</InputAdornment> | ||
) | ||
} | ||
}} | ||
/> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
export default TagsAutocompleteInput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { searchTags } from '@/api' | ||
|
||
export const useSearchTags = (input) => | ||
useQuery({ | ||
queryKey: ['tags', input], | ||
queryFn: () => searchTags(input), | ||
enabled: !!input, | ||
staleTime: 1000 * 60 * 5, | ||
gcTime: 1000 * 60 * 30 | ||
}) |