all repos — caroster @ 9e700046837e1356f38ebf38303313c053368149

[Octree] Group carpool to your event https://caroster.io

refactor: 🏷️ Improve frontend code typing
Simon Mulquin simon@octree.ch
Mon, 24 Jan 2022 13:46:16 +0000
commit

9e700046837e1356f38ebf38303313c053368149

parent

e5e05d4f020e8e0eff917468ce530cdbc16f7e40

M frontend/containers/GenericMenu/Action.tsxfrontend/containers/GenericMenu/Action.tsx

@@ -1,13 +1,12 @@

-import {ReactNode} from 'react'; +import {isValidElement} from 'React'; import Divider from '@material-ui/core/Divider'; import Typography from '@material-ui/core/Typography'; import MenuItem from '@material-ui/core/MenuItem'; import {makeStyles} from '@material-ui/core/styles'; -type ActionType = { +export type ActionType = { divider?: boolean; - isComponentLabel?: boolean; - label: ReactNode | string; + label: JSX.Element | string; id: string; onClick: () => void; };

@@ -16,21 +15,14 @@ interface Props {

action: ActionType; } -const Action = (props: Props) => { +const Action = (props: Props): JSX.Element => { const {action} = props; - const { - divider, - onClick, - id, - label, - isComponentLabel, - ...menuItemProps - } = action; + const {divider, onClick, id, label, ...menuItemProps} = action; const classes = useStyles(); if (divider) return <Divider variant="fullWidth" className={classes.divider} />; - else if (isComponentLabel) return action.label; + else if (isValidElement(action.label)) return action.label; else if (onClick) return ( <MenuItem id={id} onClick={onClick} {...menuItemProps}>
M frontend/containers/GenericMenu/index.jsfrontend/containers/GenericMenu/index.tsx

@@ -30,7 +30,7 @@ },

id: 'AboutTabs', }; const languageMenuItem = { - label: <Languages />, + label: Languages, isComponentLabel: true, id: 'LanguageSelection', };

@@ -58,7 +58,7 @@ open={!!anchorEl}

onClose={() => setAnchorEl(null)} > {validActions?.map((action, index) => ( - <Action action={action} key={index} setAnchorEl={setAnchorEl} /> + <Action action={action} key={index} /> ))} </Menu> );
M frontend/containers/GenericToolbar/index.jsfrontend/containers/GenericToolbar/index.tsx

@@ -9,8 +9,17 @@ import Avatar from '@material-ui/core/Avatar';

import Icon from '@material-ui/core/Icon'; import useProfile from '../../hooks/useProfile'; import GenericMenu from '../GenericMenu'; +import {ActionType} from '../GenericMenu/Action'; -const GenericToolbar = ({title, actions = [], goBack = null}) => { +const GenericToolbar = ({ + title, + actions = [], + goBack = null, +}: { + title: string; + actions: Array<ActionType>; + goBack: () => void | null; +}) => { const router = useRouter(); const [anchorEl, setAnchorEl] = useState(null); const classes = useStyles();

@@ -37,7 +46,7 @@ <IconButton

edge="start" className={classes.goBack} onClick={() => - router.length > 2 ? router.goBack() : router.push('/dashboard') + router.basePath.split('/').length > 2 ? router.back() : router.push('/dashboard') } > <Icon>arrow_back</Icon>
M frontend/containers/Languages/index.tsxfrontend/containers/Languages/index.tsx

@@ -1,4 +1,5 @@

import {useState, useEffect} from 'react'; +import moment from 'moment'; import MenuList from '@material-ui/core/MenuList'; import MenuItem from '@material-ui/core/MenuItem'; import {makeStyles} from '@material-ui/core/styles';

@@ -9,7 +10,6 @@ import {

useUpdateMeMutation, Enum_Userspermissionsuser_Lang, } from '../../generated/graphql'; -import moment from 'moment'; const Languages = () => { const {t, i18n} = useTranslation();
M frontend/hooks/useDebounce.jsfrontend/hooks/useDebounce.ts

@@ -7,7 +7,7 @@ * @param {*} value Value to debounce

* @param {number} delay Debounce time * @return {*} */ -function useDebounce(value, delay) { +function useDebounce<T extends unknown>(value: T, delay: number) { const [debouncedValue, setDebouncedValue] = useState(value); useEffect(() => {
M frontend/hooks/useProfile.jsfrontend/hooks/useProfile.ts

@@ -1,11 +1,11 @@

import {useState, useEffect} from 'react'; import useAuthStore from '../stores/useAuthStore'; -import {useProfileLazyQuery} from '../generated/graphql'; +import {useProfileLazyQuery, ProfileQuery} from '../generated/graphql'; const useProfile = () => { const token = useAuthStore(s => s.token); const user = useAuthStore(s => s.user); - const [profile, setProfile] = useState(undefined); + const [profile, setProfile] = useState<ProfileQuery['me']['profile']>(undefined); const [fetchProfile, {data}] = useProfileLazyQuery(); useEffect(() => {
M frontend/hooks/useSettings.tsfrontend/hooks/useSettings.ts

@@ -1,8 +1,12 @@

import {useEffect} from 'react'; -import {useSettingLazyQuery} from '../generated/graphql'; +import {useSettingLazyQuery, SettingQuery} from '../generated/graphql'; const useSettings = () => { - const [fetchSettings, {data: {setting} = {}}] = useSettingLazyQuery(); + const defaulData: SettingQuery = + {}; + + const [fetchSettings, {data: {setting} = defaulData}] = + useSettingLazyQuery(); useEffect(() => { fetchSettings();
M frontend/layouts/Default.tsxfrontend/layouts/Default.tsx

@@ -2,12 +2,13 @@ import {ReactNode} from 'react';

import {Helmet} from 'react-helmet'; import useGTM from '../hooks/useGTM'; import GenericToolbar from '../containers/GenericToolbar'; +import {ActionType} from '../containers/GenericMenu/Action'; interface Props { children: ReactNode; className?: string; menuTitle?: string; - menuActions?: any; + menuActions?: Array<ActionType>; pageTitle?: string; displayMenu?: boolean; goBack?: () => void;
M frontend/stores/useAuthStore.tsfrontend/stores/useAuthStore.ts

@@ -4,8 +4,10 @@

type State = { token: string | null; setToken: (token?: string) => void; - user: UsersPermissionsUser | null; - setUser: (user?: UsersPermissionsUser) => void; + user: Omit<UsersPermissionsUser, 'created_at' | 'updated_at' | '__typename'> | null; + setUser: ( + user?: Omit<UsersPermissionsUser, 'created_at' | 'updated_at' | '__typename'> + ) => void; logout: () => void; };
M frontend/stores/useOnboardingStore.tsfrontend/stores/useOnboardingStore.ts

@@ -3,10 +3,10 @@ import {persist} from 'zustand/middleware';

const STORAGE_KEY = 'caroster-onboarding'; -type State = { - onboardingUser: boolean; - onboardingCreator: boolean; - setOnboarding: (onboarding: any) => void; +type OnBoarding = {onboardingUser: boolean; onboardingCreator: boolean}; + +type State = OnBoarding & { + setOnboarding: (onboarding: Partial<OnBoarding>) => void; }; const useOnboardingStore = create<State>(
M frontend/stores/useTourStore.tsfrontend/stores/useTourStore.ts

@@ -1,12 +1,15 @@

import create from 'zustand'; -type State = { +type Tour = { showWelcome: boolean; isCreator: boolean | null; run: boolean; step: number; prev: number; - setTour: (tour: any) => void; +} + +type State = Tour & { + setTour: (tour: Partial<Tour>) => void; }; const useTourStore = create<State>(set => ({