Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor labels #1145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/design-system/src/subjects/stores/labels-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const LabelsListStore: RepoLabelsListStore = {
}
]
},
isLoading: false,

getParentScopeLabels: false,

Expand All @@ -82,6 +83,7 @@ export const LabelsListStore: RepoLabelsListStore = {
addLabel: (_: ILabelType) => {},

deleteLabel: (_: string) => {},
setIsLoading: (_: boolean) => {},

setValues: (_: Record<string, LabelValueType[]>) => {},
setRepoSpaceRef: (_: SetRepoSpaceRefProps) => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ export const LabelsForm = () => {
<LabelFormPage
useLabelsStore={LabelsListStore.useLabelsStore}
useTranslationStore={useTranslationStore}
isLoading={false}
isSaving={false}
onSubmit={() => {}}
onFormCancel={() => {}}
error={''}
isDataLoading={false}
/>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ export const ProjectLabelsList = () => {
return (
<>
<LabelsListPage
className="mx-auto max-w-[1040px]"
useTranslationStore={useTranslationStore}
useLabelsStore={LabelsListStore.useLabelsStore}
createdIn={''}
handleEditLabel={() => {}}
handleDeleteLabel={() => setOpenAlertDeleteDialog(true)}
searchQuery={''}
setSearchQuery={() => {}}
isLoading={false}
setSearchQuery={noop}
labelsListViewProps={{ handleEditLabel: noop, handleDeleteLabel: () => setOpenAlertDeleteDialog(true) }}
/>
<DeleteAlertDialog
open={openAlertDeleteDialog}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ export const RepoLabelsList = () => {
return (
<>
<LabelsListPage
className="max-w-[772px] px-0"
useTranslationStore={useTranslationStore}
useLabelsStore={LabelsListStore.useLabelsStore}
createdIn={''}
handleEditLabel={() => {}}
handleDeleteLabel={() => setOpenAlertDeleteDialog(true)}
searchQuery={''}
setSearchQuery={() => {}}
isLoading={false}
setSearchQuery={noop}
isRepository
labelsListViewProps={{ handleEditLabel: noop, handleDeleteLabel: () => setOpenAlertDeleteDialog(true) }}
/>
<DeleteAlertDialog
open={openAlertDeleteDialog}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const PullRequestCompareWrapper: FC<Partial<PullRequestComparePageProps>> = prop
setSearchReviewersQuery={noop}
jumpToDiff=""
setJumpToDiff={noop}
editLabelsProps={{ to: '' }}
{...props}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ const PullRequestConversation: FC<PullRequestConversationProps> = ({ state }) =>
searchLabelQuery: searchLabel,
setSearchLabelQuery: noop,
addLabel: noop,
removeLabel: noop
removeLabel: noop,
editLabelsProps: { to: '' }
}}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,35 @@ const isDataResponse = (value: LabelValuesResponseResultType): value is { key: s
return 'data' in value
}

export interface UseGetProjectLabelAndValuesDataProps {
export interface UseFillLabelStoreWithProjectLabelValuesDataProps {
queryPage?: number
query?: string
enabled?: boolean
}

export const useGetProjectLabelAndValuesData = ({
export const useFillLabelStoreWithProjectLabelValuesData = ({
queryPage,
query,
enabled = true
}: UseGetProjectLabelAndValuesDataProps) => {
}: UseFillLabelStoreWithProjectLabelValuesDataProps) => {
const space_ref = useGetSpaceURLParam()
const [isLoadingValues, setIsLoadingValues] = useState(false)

const { labels: storeLabels, setLabels, setValues, setRepoSpaceRef, resetLabelsAndValues } = useLabelsStore()
const {
labels: storeLabels,
setLabels,
setValues,
setRepoSpaceRef,
resetLabelsAndValues,
setIsLoading
} = useLabelsStore()

const { data: { body: labels } = {}, isLoading: isLoadingSpaceLabels } = useListSpaceLabelsQuery(
{
space_ref: space_ref ?? '',
queryParams: { page: queryPage || 1, limit: 10, query: query ?? '' }
},
{
enabled
}
{ enabled }
)

/**
Expand Down Expand Up @@ -123,13 +128,15 @@ export const useGetProjectLabelAndValuesData = ({
* Set space_ref to store
*/
useEffect(() => {
setRepoSpaceRef({
space_ref: space_ref ?? ''
})
setRepoSpaceRef({ space_ref: space_ref ?? '' })
}, [space_ref, setRepoSpaceRef])

return {
isLoading: isLoadingSpaceLabels || isLoadingValues,
space_ref
}
/**
* Set loading state to store
*/
useEffect(() => {
setIsLoading(enabled ? isLoadingSpaceLabels : isLoadingValues)
}, [isLoadingSpaceLabels, isLoadingValues, setIsLoading, query, enabled])

return { space_ref }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,14 @@ import { useRoutes } from '../../../framework/context/NavigationContext'
import { useTranslationStore } from '../../../i18n/stores/i18n-store'
import { PathParams } from '../../../RouteDefinitions'
import { useLabelsStore } from '../stores/labels-store'
import { useGetProjectLabelAndValuesData } from './hooks/use-get-project-label-and-values-data'
import { useFillLabelStoreWithProjectLabelValuesData } from './hooks/use-fill-label-store-with-project-label-values-data'

export const ProjectLabelFormContainer = () => {
const routes = useRoutes()
const { spaceId, labelId } = useParams<PathParams>()
const navigate = useNavigate()

const { isLoading: isDataLoading, space_ref } = useGetProjectLabelAndValuesData({
query: labelId,
enabled: !!labelId
})

const {
mutate,
isLoading,
error: createError
} = useSaveSpaceLabelMutation(
{
space_ref: space_ref ?? ''
},
{
onSuccess: () => {
onFormCancel()
}
}
)
const { space_ref } = useFillLabelStoreWithProjectLabelValuesData({ query: labelId, enabled: !!labelId })

const onFormCancel = () => {
if (window.history.length > 1) {
Expand All @@ -42,28 +24,27 @@ export const ProjectLabelFormContainer = () => {
}
}

const {
mutate,
isLoading: isSaving,
error: createError
} = useSaveSpaceLabelMutation({ space_ref: space_ref ?? '' }, { onSuccess: onFormCancel })

const onSubmit = (data: CreateLabelFormFields) => {
const { values, ...rest } = data

mutate({
body: {
label: {
...rest
},
values
}
})
mutate({ body: { label: { ...rest }, values } })
}

return (
<LabelFormPage
className="mx-auto"
useLabelsStore={useLabelsStore}
useTranslationStore={useTranslationStore}
isLoading={isLoading}
isSaving={isSaving}
onSubmit={onSubmit}
onFormCancel={onFormCancel}
error={createError?.message}
isDataLoading={isDataLoading}
labelId={labelId}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useQueryState } from '../../../framework/hooks/useQueryState'
import usePaginationQueryStateWithStore from '../../../hooks/use-pagination-query-state-with-store'
import { useTranslationStore } from '../../../i18n/stores/i18n-store'
import { useLabelsStore } from '../stores/labels-store'
import { useGetProjectLabelAndValuesData } from './hooks/use-get-project-label-and-values-data.ts'
import { useFillLabelStoreWithProjectLabelValuesData } from './hooks/use-fill-label-store-with-project-label-values-data.ts'

export const ProjectLabelsList = () => {
const navigate = useNavigate()
Expand All @@ -24,17 +24,16 @@ export const ProjectLabelsList = () => {
const { queryPage } = usePaginationQueryStateWithStore({ page, setPage })
const [query, setQuery] = useQueryState('query')

const { isLoading } = useGetProjectLabelAndValuesData({ queryPage, query })
// To fetch labels/values and set isLoading state at useLabelsStore
useFillLabelStoreWithProjectLabelValuesData({ queryPage, query })

const handleOpenDeleteDialog = (identifier: string) => {
setOpenAlertDeleteDialog(true)
setIdentifier(identifier)
}

const { mutate: deleteSpaceLabel, isLoading: isDeletingSpaceLabel } = useDeleteSpaceLabelMutation(
{
space_ref: space_ref ?? ''
},
{ space_ref: space_ref ?? '' },
{
onSuccess: (_data, variables) => {
setOpenAlertDeleteDialog(false)
Expand All @@ -48,22 +47,19 @@ export const ProjectLabelsList = () => {
}

const handleDeleteLabel = (identifier: string) => {
deleteSpaceLabel({
key: identifier
})
deleteSpaceLabel({ key: identifier })
}

return (
<>
<LabelsListPage
className="mx-auto max-w-[1040px]"
useTranslationStore={useTranslationStore}
useLabelsStore={useLabelsStore}
createdIn={space_ref}
handleEditLabel={handleEditLabel}
handleDeleteLabel={handleOpenDeleteDialog}
searchQuery={query}
setSearchQuery={setQuery}
isLoading={isLoading}
labelsListViewProps={{ handleDeleteLabel: handleOpenDeleteDialog, handleEditLabel }}
/>
<DeleteAlertDialog
open={openAlertDeleteDialog}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const ProjectGeneralSettingsPageContainer = () => {
}

// delete API call here
const deleteSpaceMutation = useDeleteSpaceMutation(
const { mutate: deleteSpaceMutation, isLoading } = useDeleteSpaceMutation(
{ space_ref: space?.path },
{
onSuccess: ({ body: data }) => {
Expand All @@ -65,7 +65,7 @@ export const ProjectGeneralSettingsPageContainer = () => {
}
)

const handleDeleteProject = () => deleteSpaceMutation.mutate({}, {})
const handleDeleteProject = () => deleteSpaceMutation({})

return (
<>
Expand All @@ -85,7 +85,7 @@ export const ProjectGeneralSettingsPageContainer = () => {
deleteFn={handleDeleteProject}
type="Project"
error={deleteError}
isLoading={deleteSpaceMutation.isLoading}
isLoading={isLoading}
withForm
useTranslationStore={useTranslationStore}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const useLabelsStore = create<ILabelsStore>(set => ({
labels: [],
totalPages: 1,
page: 1,

isLoading: true,
values: {},
space_ref: null,
repo_ref: null,
Expand All @@ -18,6 +18,8 @@ export const useLabelsStore = create<ILabelsStore>(set => ({
deleteLabel: (key: string) => set(state => ({ labels: state.labels.filter(label => label.key !== key) })),
setPage: (page: number) => set({ page }),

setIsLoading: (isLoading: boolean) => set({ isLoading }),

setValues: (values: LabelValuesType) => set({ values }),
setRepoSpaceRef: ({ repo_ref, space_ref }: SetRepoSpaceRefProps) => set({ repo_ref, space_ref }),
setGetParentScopeLabels: (getParentScopeLabels: boolean) => set({ getParentScopeLabels }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ export const usePrConversationLabels = ({ repoRef, prId, refetchData }: UsePrCon
labels,
values: labelsValues,
refetchLabels
} = useGetRepoLabelAndValuesData({
query: searchLabel,
inherited: true,
limit: 100
})
} = useGetRepoLabelAndValuesData({ query: searchLabel, inherited: true, limit: 100 })

const { data: { body: prLabels } = {}, refetch: refetchPRLabels } = useListLabelsQuery({
repo_ref: repoRef,
Expand All @@ -44,23 +40,13 @@ export const usePrConversationLabels = ({ repoRef, prId, refetchData }: UsePrCon
}

const { mutate: addLabel } = useAssignLabelMutation(
{
repo_ref: repoRef,
pullreq_number: prId
},
{
onSuccess: handleOnSuccess
}
{ repo_ref: repoRef, pullreq_number: prId },
{ onSuccess: handleOnSuccess }
)

const { mutate: removeLabel } = useUnassignLabelMutation(
{
repo_ref: repoRef,
pullreq_number: prId
},
{
onSuccess: handleOnSuccess
}
{ repo_ref: repoRef, pullreq_number: prId },
{ onSuccess: handleOnSuccess }
)

const handleAddLabel = useCallback((body: HandleAddLabelType) => addLabel({ body }), [addLabel])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ export const CreatePullRequest = () => {
PRLabels={labels}
addLabel={handleAddLabel}
removeLabel={handleDeleteLabel}
editLabelsProps={{ to: routes.toRepoLabels({ spaceId, repoId }) }}
searchLabelQuery={searchLabel}
setSearchLabelQuery={setSearchLabel}
/>
Expand Down
Loading