-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update Edit Series Modal * update change request * update missing todo message * update code to pass suffixes as a component no a render function * Update missing code --------- Co-authored-by: Harshith Mohan <[email protected]>
- Loading branch information
1 parent
123dcb9
commit 073b6f8
Showing
9 changed files
with
451 additions
and
42 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
121 changes: 121 additions & 0 deletions
121
src/components/Collection/Series/EditSeriesTabs/GroupTab.tsx
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,121 @@ | ||
import React, { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { mdiCheckUnderlineCircleOutline, mdiCloseCircleOutline, mdiMagnify, mdiPencilCircleOutline } from '@mdi/js'; | ||
import cx from 'classnames'; | ||
import { debounce } from 'lodash'; | ||
|
||
import Input from '@/components/Input/Input'; | ||
import { useLazyGetGroupInfiniteQuery } from '@/core/rtkQuery/splitV3Api/collectionApi'; | ||
import { useGetSeriesGroupQuery } from '@/core/rtkQuery/splitV3Api/seriesApi'; | ||
|
||
import type { CollectionGroupType } from '@/core/types/api/collection'; | ||
|
||
type Props = { | ||
seriesId: number; | ||
}; | ||
|
||
function GroupTab({ seriesId }: Props) { | ||
const [name, setName] = useState(''); | ||
const [search, setSearch] = useState(''); | ||
const [nameEditable, setNameEditable] = useState(false); | ||
|
||
const groupQuery = useGetSeriesGroupQuery({ seriesId: seriesId.toString(), topLevel: false }, { | ||
refetchOnMountOrArgChange: false, | ||
}); | ||
|
||
const [fetchGroup, groupResults] = useLazyGetGroupInfiniteQuery(); | ||
|
||
const getAniDbGroup = useMemo((): CollectionGroupType[] => { | ||
const pages = groupResults.data?.pages; | ||
if (!pages) return []; | ||
|
||
const keys = Object.keys(pages); | ||
if (!keys?.length) return []; | ||
|
||
return pages[1]; | ||
}, [groupResults]); | ||
|
||
const searchGroup = useMemo(() => | ||
debounce(async () => { | ||
await fetchGroup({ | ||
startsWith: search, | ||
pageSize: 5, | ||
}); | ||
}, 250), [search, fetchGroup]); | ||
|
||
useEffect(() => { | ||
if (!search) return; | ||
searchGroup()?.then()?.catch(console.error); | ||
}, [search, searchGroup]); | ||
|
||
useEffect(() => { | ||
const { data } = groupQuery; | ||
setName(data?.Name ?? ''); | ||
}, [groupQuery]); | ||
|
||
const renderTitle = useCallback((group: CollectionGroupType) => ( | ||
<div | ||
className="flex cursor-pointer justify-between" | ||
key={group.IDs.MainSeries} | ||
onClick={() => setName(group.Name)} | ||
> | ||
<div>{group.Name}</div> | ||
{group.IDs.MainSeries} | ||
</div> | ||
), []); | ||
|
||
const nameInputIcons = useCallback(() => { | ||
if (!nameEditable) { | ||
return [{ | ||
icon: mdiPencilCircleOutline, | ||
className: 'text-panel-text-primary', | ||
onClick: () => setNameEditable(_ => true), | ||
}]; | ||
} | ||
|
||
return [{ | ||
icon: mdiCloseCircleOutline, | ||
className: 'text-red-500', | ||
onClick: () => setName(_ => ''), | ||
}, { | ||
icon: mdiCheckUnderlineCircleOutline, | ||
className: 'text-panel-text-primary', | ||
onClick: () => {}, // TODO: Need endpoint to update series | ||
}]; | ||
}, [nameEditable]); | ||
|
||
return ( | ||
<div className="flex flex-col"> | ||
<Input | ||
id="name" | ||
type="text" | ||
onChange={e => setName(e.target.value)} | ||
value={name} | ||
label="Name" | ||
className="mb-4" | ||
endIcons={nameInputIcons()} | ||
disabled={!nameEditable} | ||
/> | ||
<Input | ||
id="search" | ||
type="text" | ||
value={search} | ||
onChange={e => setSearch(e.target.value)} | ||
startIcon={mdiMagnify} | ||
placeholder="Name Search..." | ||
disabled={!nameEditable} | ||
className={cx(!nameEditable && 'invisible')} | ||
/> | ||
<div | ||
className={cx( | ||
'mt-1 flex flex-col gap-y-2.5 rounded-md border border-panel-border bg-panel-background-alt p-4', | ||
!nameEditable && 'invisible', | ||
)} | ||
> | ||
{!search && groupQuery.isSuccess && renderTitle(groupQuery.data)} | ||
{search && groupResults.isSuccess && getAniDbGroup.map(renderTitle)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default GroupTab; |
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
Oops, something went wrong.