From 0218cdced5f179cc0739b47cc7cd7f72e28200f9 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sat, 15 Jul 2023 12:55:25 +0300 Subject: [PATCH] Code styling Added brackets for const lambdas Converted const lambdas with multiple instructions to functions --- front/src/api/announcement/types.ts | 32 ++++++++++++----------- front/src/api/announcements/index.ts | 3 ++- front/src/api/announcements/types.ts | 10 ++++--- front/src/api/osmAddress/index.ts | 6 +++-- front/src/api/osmAddress/types.ts | 8 +++--- front/src/api/trashbox/index.ts | 3 ++- front/src/api/trashbox/types.ts | 18 ++++++++----- front/src/assets/category.ts | 4 ++- front/src/assets/metro.ts | 4 +-- front/src/components/AuthForm.tsx | 3 ++- front/src/components/LocationMarker.tsx | 2 +- front/src/components/TrashboxMarkers.tsx | 2 +- front/src/hooks/api/useAddAnnouncement.ts | 8 +++--- front/src/hooks/api/useAnnouncements.ts | 4 +-- front/src/hooks/api/useAuth.ts | 12 +++++---- front/src/hooks/api/useBook.ts | 8 +++--- front/src/hooks/api/useFetch.ts | 6 +++-- front/src/hooks/api/useOsmAddress.ts | 3 ++- front/src/hooks/api/useTrashboxes.ts | 4 +-- front/src/utils/auth.ts | 2 +- front/src/utils/filters.ts | 10 ++++--- front/src/utils/index.ts | 6 +++-- front/src/utils/types.ts | 8 ++++-- front/utils/addFetchApiRoute.sh | 9 ++++--- 24 files changed, 106 insertions(+), 69 deletions(-) diff --git a/front/src/api/announcement/types.ts b/front/src/api/announcement/types.ts index a750c37..ea04b23 100644 --- a/front/src/api/announcement/types.ts +++ b/front/src/api/announcement/types.ts @@ -17,21 +17,23 @@ type AnnouncementResponse = { booked_by: number } -const isAnnouncementResponse = (obj: unknown): obj is AnnouncementResponse => isObject(obj, { - 'id': 'number', - 'user_id': 'number', - 'name': 'string', - 'category': isCategory, - 'best_by': 'number', - 'address': 'string', - 'longtitude': 'number', - 'latitude': 'number', - 'description': 'string', - 'src': 'string?', - 'metro': 'string', - 'trashId': 'number?', - 'booked_by': 'number' -}) +const isAnnouncementResponse = (obj: unknown): obj is AnnouncementResponse => ( + isObject(obj, { + 'id': 'number', + 'user_id': 'number', + 'name': 'string', + 'category': isCategory, + 'best_by': 'number', + 'address': 'string', + 'longtitude': 'number', + 'latitude': 'number', + 'description': 'string', + 'src': 'string?', + 'metro': 'string', + 'trashId': 'number?', + 'booked_by': 'number' + }) +) type Announcement = { id: number, diff --git a/front/src/api/announcements/index.ts b/front/src/api/announcements/index.ts index 2b8bfa5..2e7c3d7 100644 --- a/front/src/api/announcements/index.ts +++ b/front/src/api/announcements/index.ts @@ -5,8 +5,9 @@ import { AnnouncementsResponse } from './types' const initialAnnouncements: Announcement[] = [] -const composeAnnouncementsURL = (filters: FiltersType) => +const composeAnnouncementsURL = (filters: FiltersType) => ( API_URL + '/announcements?' + new URLSearchParams(URLEncodeFilters(filters)).toString() +) const processAnnouncements = (data: AnnouncementsResponse): Announcement[] => { const annList = data.list_of_announcements diff --git a/front/src/api/announcements/types.ts b/front/src/api/announcements/types.ts index 34119e0..fefd5f2 100644 --- a/front/src/api/announcements/types.ts +++ b/front/src/api/announcements/types.ts @@ -6,10 +6,12 @@ type AnnouncementsResponse = { Success: boolean } -const isAnnouncementsResponse = (obj: unknown): obj is AnnouncementsResponse => isObject(obj, { - 'list_of_announcements': obj => isArrayOf(obj, isAnnouncementResponse), - 'Success': 'boolean' -}) +const isAnnouncementsResponse = (obj: unknown): obj is AnnouncementsResponse => ( + isObject(obj, { + 'list_of_announcements': obj => isArrayOf(obj, isAnnouncementResponse), + 'Success': 'boolean' + }) +) export type { AnnouncementsResponse, diff --git a/front/src/api/osmAddress/index.ts b/front/src/api/osmAddress/index.ts index 90df60d..ec7ef9e 100644 --- a/front/src/api/osmAddress/index.ts +++ b/front/src/api/osmAddress/index.ts @@ -3,10 +3,12 @@ import { OsmAddressResponse } from './types' const initialOsmAddress = '' -const composeOsmAddressURL = (addressPosition: LatLng) => +const composeOsmAddressURL = (addressPosition: LatLng) => ( `${location.protocol}//nominatim.openstreetmap.org/reverse?format=json&accept-language=ru&lat=${addressPosition.lat}&lon=${addressPosition.lng}` +) -const processOsmAddress = (data: OsmAddressResponse): string => +const processOsmAddress = (data: OsmAddressResponse): string => ( data.display_name +) export { initialOsmAddress, composeOsmAddressURL, processOsmAddress } diff --git a/front/src/api/osmAddress/types.ts b/front/src/api/osmAddress/types.ts index 8d58a89..88ba31b 100644 --- a/front/src/api/osmAddress/types.ts +++ b/front/src/api/osmAddress/types.ts @@ -4,9 +4,11 @@ type OsmAddressResponse = { display_name: string } -const isOsmAddressResponse = (obj: unknown): obj is OsmAddressResponse => isObject(obj, { - 'display_name': 'string', -}) +const isOsmAddressResponse = (obj: unknown): obj is OsmAddressResponse => ( + isObject(obj, { + 'display_name': 'string', + }) +) export type { OsmAddressResponse, diff --git a/front/src/api/trashbox/index.ts b/front/src/api/trashbox/index.ts index 20244c1..acf4b1d 100644 --- a/front/src/api/trashbox/index.ts +++ b/front/src/api/trashbox/index.ts @@ -2,10 +2,11 @@ import { LatLng } from 'leaflet' import { API_URL } from '../../config' -const composeTrashboxURL = (position: LatLng) => +const composeTrashboxURL = (position: LatLng) => ( API_URL + '/trashbox?' + new URLSearchParams({ lat: position.lat.toString(), lng: position.lng.toString() }).toString() +) export { composeTrashboxURL } diff --git a/front/src/api/trashbox/types.ts b/front/src/api/trashbox/types.ts index 1f60bdb..b2a17c0 100644 --- a/front/src/api/trashbox/types.ts +++ b/front/src/api/trashbox/types.ts @@ -7,16 +7,20 @@ type Trashbox = { Categories: string[] } -const isTrashbox = (obj: unknown): obj is Trashbox => isObject(obj, { - 'Lat': 'number', - 'Lng': 'number', - 'Address': 'string', - 'Categories': obj => isArrayOf(obj, isString) -}) +const isTrashbox = (obj: unknown): obj is Trashbox => ( + isObject(obj, { + 'Lat': 'number', + 'Lng': 'number', + 'Address': 'string', + 'Categories': obj => isArrayOf(obj, isString) + }) +) type TrashboxResponse = Trashbox[] -const isTrashboxResponse = (obj: unknown): obj is Trashbox[] => isArrayOf(obj, isTrashbox) +const isTrashboxResponse = (obj: unknown): obj is Trashbox[] => ( + isArrayOf(obj, isTrashbox) +) export type { Trashbox, TrashboxResponse } export { isTrashbox, isTrashboxResponse } diff --git a/front/src/assets/category.ts b/front/src/assets/category.ts index 79dfb7c..5ceda7f 100644 --- a/front/src/assets/category.ts +++ b/front/src/assets/category.ts @@ -4,7 +4,9 @@ const categories = ['PORRIDGE', 'conspects', 'milk', 'bred', 'wathing', 'cloth', 'fruits_vegatables', 'soup', 'dinner', 'conserves', 'pens', 'other_things'] as const type Category = typeof categories[number] -const isCategory = (obj: unknown): obj is Category => isLiteralUnion(obj, categories) +const isCategory = (obj: unknown): obj is Category => ( + isLiteralUnion(obj, categories) +) const categoryGraphics: Record = { 'PORRIDGE': 'static/PORRIDGE.jpg', diff --git a/front/src/assets/metro.ts b/front/src/assets/metro.ts index 0264cd6..76168a5 100644 --- a/front/src/assets/metro.ts +++ b/front/src/assets/metro.ts @@ -101,9 +101,9 @@ const lineNames: Record = { violet: 'Фиолетовая', } -const lineByName = (name: string) => +const lineByName = (name: string) => ( lines.find(line => stations[line].has(name)) - +) export type { Lines } export { lines, stations, colors, lineNames, lineByName } diff --git a/front/src/components/AuthForm.tsx b/front/src/components/AuthForm.tsx index b27395f..bd5f129 100644 --- a/front/src/components/AuthForm.tsx +++ b/front/src/components/AuthForm.tsx @@ -8,8 +8,9 @@ type AuthFormProps = { error: string } -const AuthForm = ({ handleAuth, register, loading, error }: AuthFormProps) => { +function AuthForm ({ handleAuth, register, loading, error }: AuthFormProps) { const buttonText = loading ? 'Загрузка...' : (error || (register ? 'Зарегистрироваться' : 'Войти')) + return (
diff --git a/front/src/components/LocationMarker.tsx b/front/src/components/LocationMarker.tsx index 8335997..b4a1db5 100644 --- a/front/src/components/LocationMarker.tsx +++ b/front/src/components/LocationMarker.tsx @@ -10,7 +10,7 @@ type LocationMarkerProps = { setPosition: SetState } -const LocationMarker = ({ address, position, setPosition }: LocationMarkerProps) => { +function LocationMarker({ address, position, setPosition }: LocationMarkerProps) { const map = useMapEvents({ dragend: () => { diff --git a/front/src/components/TrashboxMarkers.tsx b/front/src/components/TrashboxMarkers.tsx index 2478566..a1a625a 100644 --- a/front/src/components/TrashboxMarkers.tsx +++ b/front/src/components/TrashboxMarkers.tsx @@ -8,7 +8,7 @@ type TrashboxMarkersProps = { selectTrashbox: ({ index, category }: { index: number, category: string }) => void } -const TrashboxMarkers = ({ trashboxes, selectTrashbox }: TrashboxMarkersProps) => { +function TrashboxMarkers({ trashboxes, selectTrashbox }: TrashboxMarkersProps) { return ( <>{trashboxes.map((trashbox, index) => ( diff --git a/front/src/hooks/api/useAddAnnouncement.ts b/front/src/hooks/api/useAddAnnouncement.ts index 805cf04..fb610aa 100644 --- a/front/src/hooks/api/useAddAnnouncement.ts +++ b/front/src/hooks/api/useAddAnnouncement.ts @@ -7,7 +7,9 @@ import { handleHTTPErrors } from '../../utils' const addErrors = ['Не удалось опубликовать объявление', 'Неверный ответ от сервера', 'Неизвестная ошибка'] as const type AddError = typeof addErrors[number] -const isAddError = (obj: unknown): obj is AddError => isLiteralUnion(obj, addErrors) +const isAddError = (obj: unknown): obj is AddError => ( + isLiteralUnion(obj, addErrors) +) const buttonStates = ['Опубликовать', 'Загрузка...', 'Опубликовано', 'Отменено'] as const type ButtonState = typeof buttonStates[number] | AddError @@ -16,9 +18,9 @@ type AddResponse = { Answer: boolean } -const isAddResponse = (obj: unknown): obj is AddResponse => +const isAddResponse = (obj: unknown): obj is AddResponse => ( typeof obj === 'object' && obj !== null && typeof Reflect.get(obj, 'Answer') === 'boolean' - +) const useAddAnnouncement = () => { const [status, setStatus] = useState('Опубликовать') diff --git a/front/src/hooks/api/useAnnouncements.ts b/front/src/hooks/api/useAnnouncements.ts index 1c73b6d..cdea808 100644 --- a/front/src/hooks/api/useAnnouncements.ts +++ b/front/src/hooks/api/useAnnouncements.ts @@ -4,7 +4,7 @@ import { composeAnnouncementsURL, initialAnnouncements, processAnnouncements } f import { isAnnouncementsResponse } from '../../api/announcements/types' -const useAnnouncements = (filters: FiltersType) => +const useAnnouncements = (filters: FiltersType) => ( useFetch( composeAnnouncementsURL(filters), 'GET', @@ -13,6 +13,6 @@ const useAnnouncements = (filters: FiltersType) => processAnnouncements, initialAnnouncements ) - +) export default useAnnouncements diff --git a/front/src/hooks/api/useAuth.ts b/front/src/hooks/api/useAuth.ts index c6f40af..afcebd7 100644 --- a/front/src/hooks/api/useAuth.ts +++ b/front/src/hooks/api/useAuth.ts @@ -38,16 +38,18 @@ interface LogInResponse { token_type: 'bearer' } -const isLogInResponse = (obj: unknown): obj is LogInResponse => isObject(obj, { - 'access_token': 'string', - 'token_type': isConst('bearer') -}) +const isLogInResponse = (obj: unknown): obj is LogInResponse => ( + isObject(obj, { + 'access_token': 'string', + 'token_type': isConst('bearer') + }) +) function useAuth() { const [loading, setLoading] = useState(false) const [error, setError] = useState('') - const doAuth = async (data: AuthData, newAccount: boolean) => { + async function doAuth(data: AuthData, newAccount: boolean) { setLoading(true) if (newAccount) { diff --git a/front/src/hooks/api/useBook.ts b/front/src/hooks/api/useBook.ts index 9c4dc5b..71fe6ed 100644 --- a/front/src/hooks/api/useBook.ts +++ b/front/src/hooks/api/useBook.ts @@ -10,9 +10,11 @@ type BookResponse = { Success: boolean } -const isBookResponse = (obj: unknown): obj is BookResponse => isObject(obj, { - 'Success': 'boolean' -}) +const isBookResponse = (obj: unknown): obj is BookResponse => ( + isObject(obj, { + 'Success': 'boolean' + }) +) type BookStatus = '' | 'Загрузка...' | 'Забронировано' | 'Ошибка бронирования' diff --git a/front/src/hooks/api/useFetch.ts b/front/src/hooks/api/useFetch.ts index cfb65f2..38e00c6 100644 --- a/front/src/hooks/api/useFetch.ts +++ b/front/src/hooks/api/useFetch.ts @@ -20,11 +20,13 @@ type UseFetchErrored = { data: undefined } & UseFetchShared -const gotError = (res: UseFetchErrored | UseFetchSucced): res is UseFetchErrored => +const gotError = (res: UseFetchErrored | UseFetchSucced): res is UseFetchErrored => ( typeof res.error === 'string' +) -const fallbackError = (res: UseFetchSucced | UseFetchErrored) => +const fallbackError = (res: UseFetchSucced | UseFetchErrored) => ( gotError(res) ? res.error : res.data +) type UseFetchReturn = ({ error: null, diff --git a/front/src/hooks/api/useOsmAddress.ts b/front/src/hooks/api/useOsmAddress.ts index 17d5f5b..2c50553 100644 --- a/front/src/hooks/api/useOsmAddress.ts +++ b/front/src/hooks/api/useOsmAddress.ts @@ -4,7 +4,7 @@ import useFetch from './useFetch' import { composeOsmAddressURL, processOsmAddress } from '../../api/osmAddress' import { isOsmAddressResponse } from '../../api/osmAddress/types' -const useOsmAddresses = (addressPosition: LatLng) => +const useOsmAddresses = (addressPosition: LatLng) => ( useFetch( composeOsmAddressURL(addressPosition), 'GET', @@ -13,5 +13,6 @@ const useOsmAddresses = (addressPosition: LatLng) => processOsmAddress, '' ) +) export default useOsmAddresses diff --git a/front/src/hooks/api/useTrashboxes.ts b/front/src/hooks/api/useTrashboxes.ts index 9c23d5d..5b96c07 100644 --- a/front/src/hooks/api/useTrashboxes.ts +++ b/front/src/hooks/api/useTrashboxes.ts @@ -4,7 +4,7 @@ import useFetch from './useFetch' import { composeTrashboxURL } from '../../api/trashbox' import { isTrashboxResponse } from '../../api/trashbox/types' -const useTrashboxes = (position: LatLng) => +const useTrashboxes = (position: LatLng) => ( useFetch( composeTrashboxURL(position), 'GET', @@ -13,6 +13,6 @@ const useTrashboxes = (position: LatLng) => (data) => data, [] ) - +) export default useTrashboxes diff --git a/front/src/utils/auth.ts b/front/src/utils/auth.ts index d00847c..a380ee4 100644 --- a/front/src/utils/auth.ts +++ b/front/src/utils/auth.ts @@ -6,7 +6,7 @@ const getToken = () => { return token } -const setToken = (token: string) => { +function setToken(token: string) { localStorage.setItem('Token', token) } diff --git a/front/src/utils/filters.ts b/front/src/utils/filters.ts index cc81c3c..30a821f 100644 --- a/front/src/utils/filters.ts +++ b/front/src/utils/filters.ts @@ -7,10 +7,12 @@ type FiltersType = Partial> const defaultFilters: FiltersType = { userId: undefined, category: undefined, metro: undefined, bookedBy: undefined } -const URLEncodeFilters = (filters: FiltersType) => Object.fromEntries( - filterNames.map( - fName => [fName, filters[fName]?.toString()] - ).filter((p): p is [string, string] => typeof p[1] !== 'undefined') +const URLEncodeFilters = (filters: FiltersType) => ( + Object.fromEntries( + filterNames.map( + fName => [fName, filters[fName]?.toString()] + ).filter((p): p is [string, string] => typeof p[1] !== 'undefined') + ) ) export type { FilterNames, FiltersType } diff --git a/front/src/utils/index.ts b/front/src/utils/index.ts index b1a0db2..e173552 100644 --- a/front/src/utils/index.ts +++ b/front/src/utils/index.ts @@ -1,6 +1,8 @@ -const isAborted = (err: Error) => err.name === 'AbortError' +const isAborted = (err: Error) => ( + err.name === 'AbortError' +) -const handleHTTPErrors = (res: Response) => { +function handleHTTPErrors(res: Response) { if (!res.ok) { switch (res.status) { case 401: diff --git a/front/src/utils/types.ts b/front/src/utils/types.ts index 93c1096..a74a6f5 100644 --- a/front/src/utils/types.ts +++ b/front/src/utils/types.ts @@ -20,7 +20,9 @@ const isObject = (obj: unknown, properties: PropertiesGuards): obj is T => ( Object.entries(properties).every(([name, guard]) => { const param: unknown = Reflect.get(obj, name) - console.log(name, param, guard) + if (import.meta.env.DEV) { + console.log(name, param, guard) + } if (typeof guard === 'function') { return guard(param) @@ -54,7 +56,9 @@ const isArrayOf = (obj: unknown, itemGuard: ((obj: unknown) => obj is T)): ob obj.every(itemGuard) ) -const isString = (obj: unknown): obj is string => typeof obj === 'string' +const isString = (obj: unknown): obj is string => ( + typeof obj === 'string' +) type SetState = React.Dispatch> diff --git a/front/utils/addFetchApiRoute.sh b/front/utils/addFetchApiRoute.sh index 57bac31..7576e04 100644 --- a/front/utils/addFetchApiRoute.sh +++ b/front/utils/addFetchApiRoute.sh @@ -9,8 +9,9 @@ import { ${NAME}Response, ${NAME} } from './types' const initial${NAME}: ${NAME} = {} -const compose${NAME}URL = () => +const compose${NAME}URL = () => ( API_URL + '/${NAME,}?' +) const process${NAME} = (data: ${NAME}Response): ${NAME} => { return data @@ -26,19 +27,21 @@ type ${NAME}Response = { } -const is${NAME}Response = (obj: unknown): obj is ${NAME}Response => +const is${NAME}Response = (obj: unknown): obj is ${NAME}Response => ( isObject(obj, { }) +) type ${NAME} = { } -const is${NAME} = (obj: unknown): obj is ${NAME} => +const is${NAME} = (obj: unknown): obj is ${NAME} => ( isObject(obj, { }) +) export type { ${NAME}Response, ${NAME} }