Skip to content

Commit

Permalink
chore: refactor categories
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojsnunes committed Jul 7, 2024
1 parent 663e3af commit db5f30a
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 260 deletions.
50 changes: 0 additions & 50 deletions src/pages/Howto/Category/CategoriesSelect.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions src/pages/Howto/Content/Common/Howto.form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ vi.mock('src/common/hooks/useCommonStores', () => {
return {
useCommonStores: () => ({
stores: {
categoriesStore: {
allCategories: [],
},
howtoStore: {
uploadStatus: {
Start: false,
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Howto/Content/Common/Howto.form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const FormContainer = styled.form`
`
export const HowtoForm = observer((props: IProps) => {
const { howtoStore } = useCommonStores().stores

const [state, setState] = useState<IState>({
formSaved: false,
_toDocsList: false,
Expand Down Expand Up @@ -195,7 +196,7 @@ export const HowtoForm = observer((props: IProps) => {
store={howtoStore}
_id={formValues._id}
/>
<HowtoFieldCategory category={values.category} />
<HowtoFieldCategory />
<HowtoFieldTags />
<HowtoFieldTime />
<HowtoFieldDifficulty />
Expand Down
46 changes: 30 additions & 16 deletions src/pages/Howto/Content/Common/HowtoFieldCategory.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
import { useEffect, useState } from 'react'
import { Field } from 'react-final-form'
import { CategoriesSelect } from 'src/pages/Howto/Category/CategoriesSelect'
import { CategoriesSelectV2 } from 'src/pages/common/Category/CategoriesSelectV2'
import {
FormFieldWrapper,
HowtoCategoryGuidance,
} from 'src/pages/Howto/Content/Common'
import { intro } from 'src/pages/Howto/labels'

import type { ICategory } from 'src/models/categories.model'
import { howtoService } from '../../howto.service'

interface IProps {
category: ICategory | undefined
}
import type { SelectValue } from 'src/pages/common/Category/CategoriesSelectV2'

export const HowtoFieldCategory = ({ category }: IProps) => {
export const HowtoFieldCategory = () => {
const { placeholder, title } = intro.category
const [options, setOptions] = useState<SelectValue[]>([])

useEffect(() => {
const getCategories = async () => {
const categories = await howtoService.getHowtoCategories()
setOptions(
categories
.filter((x) => !x._deleted)
.map((x) => ({ label: x.label, value: x })),
)
}

getCategories()
}, [])

return (
<FormFieldWrapper text={title} required>
<Field
name="category"
render={({ input, ...rest }) => (
<CategoriesSelect
{...rest}
isForm={true}
onChange={(category) => input.onChange(category)}
value={input.value}
placeholder={placeholder}
type="howto"
/>
render={({ input }) => (
<>
<CategoriesSelectV2
isForm={true}
onChange={(category) => input.onChange(category)}
value={input.value}
placeholder={placeholder || ''}
categories={options}
/>
<HowtoCategoryGuidance category={input.value} type="main" />
</>
)}
/>
<HowtoCategoryGuidance category={category} type="main" />
</FormFieldWrapper>
)
}
18 changes: 2 additions & 16 deletions src/pages/Research/Content/Common/Research.form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { SelectField } from 'src/common/Form/Select.field'
import { TagsSelectField } from 'src/common/Form/TagsSelect.field'
import { usePrompt } from 'src/common/hooks/usePrompt'
import { researchStatusOptions } from 'src/models/research.models'
import { CategoriesSelect } from 'src/pages/Howto/Category/CategoriesSelect'
import {
ResearchErrors,
ResearchPostingGuidelines,
Expand All @@ -41,6 +40,7 @@ import {
RESEARCH_TITLE_MIN_LENGTH,
} from '../../constants'
import { buttons, headings, overview } from '../../labels'
import ResearchFieldCategory from './ResearchCategorySelect'

import type { MainFormAction } from 'src/common/Form/types'
import type { IResearch } from 'src/models/research.models'
Expand Down Expand Up @@ -294,21 +294,7 @@ const ResearchForm = observer((props: IProps) => {
<ResearchFormLabel>
{categories.title}
</ResearchFormLabel>
<Field
name="researchCategory"
render={({ input, ...rest }) => (
<CategoriesSelect
{...rest}
isForm={true}
onChange={(category) =>
input.onChange(category)
}
value={input.value}
placeholder={categories.placeholder}
type="research"
/>
)}
/>
<ResearchFieldCategory />
</Flex>
<Flex sx={{ flexDirection: 'column' }} mb={3}>
<ResearchFormLabel>
Expand Down
42 changes: 42 additions & 0 deletions src/pages/Research/Content/Common/ResearchCategorySelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useEffect, useState } from 'react'
import { Field } from 'react-final-form'
import { CategoriesSelectV2 } from 'src/pages/common/Category/CategoriesSelectV2'

import { overview } from '../../labels'
import { researchService } from '../../research.service'

import type { SelectValue } from 'src/pages/common/Category/CategoriesSelectV2'

const ResearchFieldCategory = () => {
const [options, setOptions] = useState<SelectValue[]>([])

useEffect(() => {
const getCategories = async () => {
const categories = await researchService.getResearchCategories()
setOptions(
categories
.filter((x) => !x._deleted)
.map((x) => ({ label: x.label, value: x })),
)
}

getCategories()
}, [])

return (
<Field
name="researchCategory"
render={({ input }) => (
<CategoriesSelectV2
isForm={true}
onChange={(category) => input.onChange(category)}
value={input.value}
placeholder={overview.categories.placeholder || ''}
categories={options}
/>
)}
/>
)
}

export default ResearchFieldCategory
3 changes: 2 additions & 1 deletion src/pages/Research/Content/ResearchFilterHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { Flex, Input } from 'theme-ui'

import { CategoriesSelectV2 } from '../../common/Category/CategoriesSelectV2'
import { listing } from '../labels'
import { ResearchSearchParams, researchService } from '../research.service'
import { researchService } from '../research.service'
import { ResearchSortOptions } from '../ResearchSortOptions'
import { ResearchSearchParams } from './ResearchSearchParams'

import type { SelectValue } from '../../common/Category/CategoriesSelectV2'
import type { ResearchSortOption } from '../ResearchSortOptions'
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Research/Content/ResearchList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { Box, Flex, Heading } from 'theme-ui'

import { ITEMS_PER_PAGE, RESEARCH_EDITOR_ROLES } from '../constants'
import { listing } from '../labels'
import { ResearchSearchParams, researchService } from '../research.service'
import { researchService } from '../research.service'
import { ResearchFilterHeader } from './ResearchFilterHeader'
import ResearchListItem from './ResearchListItem'
import { ResearchSearchParams } from './ResearchSearchParams'

import type { DocumentData, QueryDocumentSnapshot } from 'firebase/firestore'
import type { ResearchStatus } from 'oa-shared'
Expand Down
6 changes: 6 additions & 0 deletions src/pages/Research/Content/ResearchSearchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ResearchSearchParams {
category = 'category',
q = 'q',
sort = 'sort',
status = 'status',
}
12 changes: 8 additions & 4 deletions src/pages/Research/research.routes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ import type { Mock } from 'vitest'
const Theme = testingThemeStyles
const mockActiveUser = FactoryUser()

vi.mock('src/pages/Research/research.service')
vi.mock('src/pages/Research/research.service', () => ({
researchService: {
search: vi.fn(),
getDraftCount: vi.fn(),
getDrafts: vi.fn(),
getResearchCategories: vi.fn(() => []),
},
}))

// Similar to issues in Academy.test.tsx - stub methods called in user store constructor
// TODO - replace with mock store or avoid direct call
Expand All @@ -49,9 +56,6 @@ vi.mock('src/common/hooks/useCommonStores', () => ({
HowtoAuthor: true,
},
},
researchCategoriesStore: {
allResearchCategories: [],
},
tagsStore: {},
},
}),
Expand Down
7 changes: 0 additions & 7 deletions src/pages/Research/research.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ import type { IResearch } from '../../models'
import type { ICategory } from '../../models/categories.model'
import type { ResearchSortOption } from './ResearchSortOptions'

export enum ResearchSearchParams {
category = 'category',
q = 'q',
sort = 'sort',
status = 'status',
}

const search = async (
words: string[],
category: string,
Expand Down
44 changes: 0 additions & 44 deletions src/stores/Categories/categories.store.tsx

This file was deleted.

54 changes: 0 additions & 54 deletions src/stores/QuestionCategories/questionCategories.store.tsx

This file was deleted.

Loading

0 comments on commit db5f30a

Please sign in to comment.