Skip to content

Commit

Permalink
fix: refactor labels
Browse files Browse the repository at this point in the history
  • Loading branch information
ankormoreankor authored and 3em committed Feb 21, 2025
1 parent 31c14db commit eb19077
Show file tree
Hide file tree
Showing 51 changed files with 718 additions and 1,140 deletions.
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
3 changes: 1 addition & 2 deletions apps/design-system/src/subjects/views/labels/labels-form.tsx
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-[812px]"
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-[570px] 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 @@ -27,16 +27,21 @@ export const useGetProjectLabelAndValuesData = ({
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,19 @@ 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(() => {
if (enabled) {
return setIsLoading(isLoadingSpaceLabels)
}

setIsLoading(isLoadingValues)
}, [isLoadingSpaceLabels, isLoadingValues, setIsLoading, query, enabled])

return { space_ref }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,7 @@ export const ProjectLabelFormContainer = () => {
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 } = useGetProjectLabelAndValuesData({ 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 @@ -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
useGetProjectLabelAndValuesData({ 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-[812px]"
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
4 changes: 3 additions & 1 deletion apps/gitness/src/pages-v2/project/stores/labels-store.tsx
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 @@ -21,11 +21,11 @@ export const usePrConversationLabels = ({ repoRef, prId, refetchData }: UsePrCon
setSearchLabel(data)
}, [])

const { labels, values: labelsValues } = useGetRepoLabelAndValuesData({
query: searchLabel,
inherited: true,
limit: 100
})
const {
labels,
values: labelsValues,
refetchLabels
} = useGetRepoLabelAndValuesData({ query: searchLabel, inherited: true, limit: 100 })

const { data: { body: prLabels } = {}, refetch: refetchPRLabels } = useListLabelsQuery({
repo_ref: repoRef,
Expand All @@ -35,27 +35,18 @@ export const usePrConversationLabels = ({ repoRef, prId, refetchData }: UsePrCon

const handleOnSuccess = () => {
refetchPRLabels()
refetchLabels()
refetchData()
}

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 @@ -495,6 +495,8 @@ export const CreatePullRequest = () => {
setReviewers(newReviewers)
}

const handleEditLabels = () => navigate(routes.toRepoLabels({ spaceId, repoId }))

const handleAddLabel = (labelToAdd: HandleAddLabelType) => {
const findLabel = labelsList.find(label => label.id === labelToAdd.label_id)
if (!findLabel) return
Expand Down Expand Up @@ -601,6 +603,7 @@ export const CreatePullRequest = () => {
PRLabels={labels}
addLabel={handleAddLabel}
removeLabel={handleDeleteLabel}
editLabels={handleEditLabels}
searchLabelQuery={searchLabel}
setSearchLabelQuery={setSearchLabel}
/>
Expand Down
Loading

0 comments on commit eb19077

Please sign in to comment.