Skip to content

Commit

Permalink
mobx migration 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Miskamyasa committed Oct 5, 2024
1 parent 7bdebc3 commit faf7de5
Show file tree
Hide file tree
Showing 64 changed files with 503 additions and 549 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
}
},
"rules": {
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/space-infix-ops": "error",
"@typescript-eslint/type-annotation-spacing": "error",
Expand Down
4 changes: 3 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import type {ReactElement} from "react"

import {initialWindowMetrics, SafeAreaProvider} from "react-native-safe-area-context"
import {Provider} from "react-redux"
import {PersistGate} from "redux-persist/integration/react"
Expand All @@ -9,7 +11,7 @@ import {Navigation} from "./navigation/Navigation"
import {persistor, store} from "./store"
import {StoresProvider} from "./store/StoresProvider"

export function App() {
export function App(): ReactElement | null {
const isLoadingComplete = useBootstrapApp()

if (!isLoadingComplete) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/ActionSheet/Submit.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {ReactElement} from "react"
import {StyleSheet} from "react-native"

import {primaryColors} from "../../colors/colors"
Expand All @@ -17,7 +18,7 @@ const styles = StyleSheet.create({
export function Submit({onPress, text}: {
text: string
onPress: () => void
}) {
}): ReactElement {
return (
<Div
style={styles.root}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ActionSheet/Title.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Fragment} from "react"
import {Fragment, type ReactElement} from "react"
import {StyleSheet, TextStyle, TouchableOpacity} from "react-native"

import MaterialIcons from "@expo/vector-icons/MaterialIcons"
Expand Down Expand Up @@ -28,7 +28,7 @@ export const colors: Record<"divider", ThemeProps> = {
export function Title({children, onClosePress}: {
children: string
onClosePress?: () => void
}) {
}): ReactElement {
const textColor = useThemeColor("text")
const dividerColor = useThemeColor("text", colors.divider)

Expand Down
8 changes: 3 additions & 5 deletions src/components/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {memo, useMemo} from "react"
import {memo, useMemo, type ReactElement} from "react"
import {StyleSheet, View, ActivityIndicator, ViewStyle} from "react-native"

import {useThemeColor} from "../colors/useThemeColor"
Expand All @@ -13,7 +13,7 @@ const container: ViewStyle = {

const staticStyles = StyleSheet.create({container})

function Loader() {
export const Loader = memo(function Loader(): ReactElement {
const backgroundColor = useThemeColor("loaderBackground")
const loaderColor = useThemeColor("loaderColor")

Expand All @@ -28,6 +28,4 @@ function Loader() {
size="large" />
</View>
)
}

export default memo(Loader)
})
8 changes: 3 additions & 5 deletions src/components/OverlayLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {memo, useMemo} from "react"
import {memo, useMemo, type ReactElement} from "react"
import {StyleSheet, View, ActivityIndicator, ViewStyle} from "react-native"

import {useThemeColor} from "../colors/useThemeColor"
Expand All @@ -16,7 +16,7 @@ const container: ViewStyle = {

const staticStyles = StyleSheet.create({container})

function OverlayLoader() {
export const OverlayLoader = memo(function OverlayLoader(): ReactElement {
const color = useThemeColor("text")
const backgroundColor = useThemeColor("overlayBackground")

Expand All @@ -31,6 +31,4 @@ function OverlayLoader() {
size="large" />
</View>
)
}

export default memo(OverlayLoader)
})
3 changes: 2 additions & 1 deletion src/components/PageTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {ReactElement} from "react"
import {TextStyle, ViewStyle} from "react-native"

import {layout} from "../constants/layout"
Expand Down Expand Up @@ -28,7 +29,7 @@ const styles = createStaticStyles({
export function PageTitle({title, icon}: {
title: string
icon?: string
}) {
}): ReactElement {
return (
<Div style={styles.container}>
{icon && (
Expand Down
4 changes: 2 additions & 2 deletions src/components/SkillImage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useMemo} from "react"
import {useMemo, type ReactElement} from "react"
import {ImageRequireSource} from "react-native"

import {Image} from "expo-image"
Expand Down Expand Up @@ -31,7 +31,7 @@ const styles = createStaticStyles({
export default function SkillImage({name, banner}: {
name: string
banner?: boolean
}) {
}): ReactElement {
const source = useMemo(() => {
try {
// eslint-disable-next-line import/namespace
Expand Down
10 changes: 4 additions & 6 deletions src/components/Toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {memo, useState} from "react"
import {memo, useState, type ReactElement} from "react"
import {Switch} from "react-native"

import {memoize} from "lodash"
Expand All @@ -10,10 +10,10 @@ const inactiveColor = "rgba(155,166,171,0.38)"
const trackColor = memoize((activeColor: string) => ({false: inactiveColor, true: activeColor}))
const thumbColor = memoize((state, activeColor: string) => state ? activeColor : "rgb(122,122,122)")

function Toggle({onToggle, defaultValue = false}: {
export const Toggle = memo(function Toggle({onToggle, defaultValue = false}: {
onToggle?: (state: boolean) => void
defaultValue?: boolean
}) {
}): ReactElement {
const [state, setState] = useState(defaultValue)
const trackThemeColor = useThemeColor("buttonBackground", primaryColors.background)
const thumbThemeColor = useThemeColor("buttonText", primaryColors.color)
Expand All @@ -33,6 +33,4 @@ function Toggle({onToggle, defaultValue = false}: {
value={state}
onValueChange={handleToggle} />
)
}

export default memo(Toggle)
})
8 changes: 3 additions & 5 deletions src/features/skills/NewSkillForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useState, useRef, useCallback} from "react"
import {useState, useRef, useCallback, type ReactElement} from "react"
import {TextStyle, ViewStyle, StyleSheet, View, TextInput} from "react-native"

import {Container} from "../../components/ActionSheet/Container"
Expand Down Expand Up @@ -30,10 +30,10 @@ const staticStyles = StyleSheet.create({
view,
})

function NewSkillForm(props: {
export function NewSkillForm(props: {
submit: (value: string) => void
dismiss: () => void
}) {
}): ReactElement {
const [value, setValue] = useState<string>("")

const handleSubmit = (): void => props.submit(value)
Expand Down Expand Up @@ -69,5 +69,3 @@ function NewSkillForm(props: {
</Container>
)
}

export default NewSkillForm
4 changes: 2 additions & 2 deletions src/features/skills/SelectedSkillProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createContext, Component, PropsWithChildren} from "react"
import {createContext, Component, PropsWithChildren, type ReactElement} from "react"

import {Skill} from "../../store/skills/types"

Expand All @@ -22,7 +22,7 @@ class SelectedSkillProvider extends Component<PropsWithChildren> {
setSelected: this.setSelected,
}

render() {
render(): ReactElement {
return (
<SelectedSkillContext.Provider
value={this.state}>
Expand Down
29 changes: 17 additions & 12 deletions src/features/skills/SkillsList.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {memo, useCallback} from "react"
import {ListRenderItemInfo, FlatList} from "react-native"
import {memo, type ReactElement} from "react"
import {FlatList} from "react-native"

import {flatList} from "../../constants/defaultStyles"
import {__t} from "../../helpers/i18n"
import {Categories} from "../../store/skills/types"

import {SelectedSkillProvider} from "./SelectedSkillProvider"
import SkillsListHeader from "./SkillsListHeader"
import SkillsListLoader from "./SkillsListLoader"
import SkillsListSectionCard from "./SkillsListSectionCard"
import {SkillsListHeader} from "./SkillsListHeader"
import {SkillsListLoader} from "./SkillsListLoader"
import {SkillsListSectionCard} from "./SkillsListSectionCard"

type SkillsSection = {
key: Categories
Expand All @@ -21,14 +21,19 @@ const sections: Array<SkillsSection> = [
{key: "other", title: __t("exercises.sections.other")},
]

const renderItem = ({item}: ListRenderItemInfo<SkillsSection>) => (
<SkillsListSectionCard
category={item.key}
title={item.title} />
)
function renderItem({item}: {item: SkillsSection}): ReactElement {
return (
<SkillsListSectionCard
category={item.key}
title={item.title} />
)
}

function keyExtractor(item: SkillsSection): string {
return item.key
}

export const SkillsList = memo(function SkillsList() {
const keyExtractor = useCallback((item: SkillsSection): string => item.key, [])
export const SkillsList = memo(function SkillsList(): ReactElement {
return (
<SelectedSkillProvider>
<FlatList
Expand Down
29 changes: 11 additions & 18 deletions src/features/skills/SkillsListHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Fragment, memo, useCallback, useContext} from "react"
import {Fragment, useCallback, useContext, type ReactElement} from "react"
import {StyleSheet, TextStyle, View, ViewStyle} from "react-native"

import {isEmpty} from "lodash"
import {observer} from "mobx-react"

import {primaryColors, secondaryColors} from "../../colors/colors"
import {Div} from "../../components/Div"
Expand All @@ -15,10 +16,10 @@ import {useBoolean} from "../../hooks/useBoolean"
import {useKeyboard} from "../../hooks/useKeyboard"
import {navigation} from "../../navigation/config"
import {useAppDispatch, useAppSelector} from "../../store"
import {addCustomSkill} from "../../store/skills/actions"
import {useStores} from "../../store/useStores"
import {addSkillToWorkout} from "../../store/workouts/actions"

import NewSkillForm from "./NewSkillForm"
import {NewSkillForm} from "./NewSkillForm"
import {SelectedSkillContext} from "./SelectedSkillProvider"

const container: ViewStyle = {
Expand Down Expand Up @@ -54,9 +55,10 @@ const staticStyles = StyleSheet.create({
text,
})

function SkillsListHeader() {
export const SkillsListHeader = observer(function SkillsListHeader(): ReactElement | null {
const {skillsStore} = useStores()

const workout = useAppSelector(state => state.workouts.current)
const skills = useAppSelector(state => state.skills.store)

const {selected, setSelected} = useContext(SelectedSkillContext)

Expand All @@ -77,9 +79,9 @@ function SkillsListHeader() {
closeModal()
if (text.length > 0) {
analytics.sendEvent("new_skill_form_submit", {title: text})
dispatch(addCustomSkill(text))
skillsStore.addCustomSkill(text)
}
}, [closeModal, dispatch])
}, [closeModal, skillsStore])

const openNewSkillModal = useCallback(() => {
analytics.sendEvent("new_skill_form_open")
Expand All @@ -94,7 +96,6 @@ function SkillsListHeader() {
return (
<Fragment>
<View style={staticStyles.container}>

<Div
style={staticStyles.card}
theme={primaryColors.background}
Expand All @@ -106,15 +107,13 @@ function SkillsListHeader() {
{__t("exercises.create")}
</Span>
</Div>

<Modal
closeModal={closeModal}
visible={visible}>
<NewSkillForm
dismiss={closeModal}
submit={handleSubmitNewSkill} />
</Modal>

<Div
style={staticStyles.card}
theme={secondaryColors.background}>
Expand All @@ -123,10 +122,9 @@ function SkillsListHeader() {
style={staticStyles.text}>
{__t("exercises.selected")}
{":\n"}
{selected ? skills[selected.id].title[__locale()] : "-"}
{selected ? skillsStore.registry.get(selected.id)!.title[__locale()] : "-"}
</Span>
</Div>

<Div
disabled={!selected}
style={staticStyles.card}
Expand All @@ -139,13 +137,8 @@ function SkillsListHeader() {
{__t("workouts.addToWorkout")}
</Span>
</Div>

</View>

<PageTitle title={__t("exercises.title")} />

</Fragment>
)
}

export default memo(SkillsListHeader)
})
Loading

0 comments on commit faf7de5

Please sign in to comment.