Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show error #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/components/containers/FishingWeight.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useMutation, useQuery, useQueryClient } from 'react-query';
import { useNavigate } from 'react-router-dom';
import styled from 'styled-components';
import { FishingWeighType, handleErrorToastFromServer, useFishTypes } from '../../utils';
import {
FishingWeighType,
handleErrorToastFromServer,
ReactQueryError,
useFishTypes,
} from '../../utils';
import api from '../../utils/api';
import Button from '../buttons/Button';
import SwitchButton from '../buttons/SwitchButton';
Expand All @@ -28,6 +33,15 @@ const FishingWeight = ({ coordinates, isDisabled }: { coordinates: any; isDisabl
isLoading: fishingWeightsLoading,
} = useQuery(['fishingWeights'], api.getFishingWeights, { retry: false });

useEffect(() => {
const hasFishingWeights = fishingWeights?.total && !!Object.keys(fishingWeights.total).length;
const hasNoAmounts = !Object.keys(amounts).length;

if (hasFishingWeights && hasNoAmounts) {
setAmounts(fishingWeights.total as { [key: number]: number });
}
}, [fishingWeights, amounts]);

const hasFishOnBoat = !!Object.values(fishingWeights.preliminary || {}).length;

const initialValues = fishTypes
Expand All @@ -36,8 +50,7 @@ const FishingWeight = ({ coordinates, isDisabled }: { coordinates: any; isDisabl
)
.map((fishType) => ({
...fishType,
preliminaryAmount: fishingWeights.preliminary[fishType.id],
amount: fishingWeights.preliminary[fishType.id] || '',
preliminaryAmount: fishingWeights?.preliminary?.[fishType.id],
}));

const { mutateAsync: fishingWeightMutation, isLoading: fishingWeightLoading } = useMutation(
Expand All @@ -47,8 +60,8 @@ const FishingWeight = ({ coordinates, isDisabled }: { coordinates: any; isDisabl
queryClient.invalidateQueries(['fishingWeights']);
navigate(-1);
},
onError: () => {
handleErrorToastFromServer();
onError: ({ response }: { response: ReactQueryError }) => {
handleErrorToastFromServer(response);
},
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/other/FishRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const FishRow = ({ onChange, fish }: FishRowProp) => {
<Column>
<TextColumn>
<Title>{label.charAt(0).toUpperCase() + label.slice(1)}</Title>
{preliminaryAmount && <Caught>{`Sagauta ${preliminaryAmount} kg`}</Caught>}
{preliminaryAmount && <Caught>{`Sugauta ${preliminaryAmount} kg`}</Caught>}
</TextColumn>
<InnerRow>
<Button type="button" onClick={() => amount > 0 && onChange(amount - 1)}>
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum ServerErrors {
NO_TOOLS_IN_STORAGE = 'No tools in storage',
TOOL_WITH_THIS_SEAL_NUMBER_ALREADY_EXISTS = 'Tool with this seal number already exists',
FISH_MUST_BE_WEIGHTED = 'Fish must be weighted',
FISH_ALREADY_WEIGHTED = 'Fish already weighted',
}

export enum RoleTypes {
Expand Down
11 changes: 6 additions & 5 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import api from './api';
import { LOCATION_ERRORS, ToolTypeType } from './constants';
import { routes } from './routes';
import { validationTexts } from './texts';
import { Profile, ProfileId, ResponseProps, ToolsGroup } from './types';
import { Profile, ProfileId, ReactQueryError, ResponseProps, ToolsGroup } from './types';
const cookies = new Cookies();

interface UpdateTokenProps {
Expand All @@ -22,10 +22,10 @@ export const clearCookies = () => {
cookies.remove('profileId', { path: '/' });
};

export const getErrorMessage = (responseError: string) =>
validationTexts[responseError as keyof typeof validationTexts] || validationTexts.error;
export const getErrorMessage = (errorMessage: string) =>
validationTexts[errorMessage as keyof typeof validationTexts] || validationTexts.error;

export const handleErrorToastFromServer = (responseError: string = 'error') => {
export const handleErrorToastFromServer = (responseError?: ReactQueryError) => {
handleErrorToast(getErrorMessage(getReactQueryErrorMessage(responseError)));
};

Expand Down Expand Up @@ -222,7 +222,8 @@ export const getBuiltToolInfo = (toolsGroup: ToolsGroup) => {
};
};

export const getReactQueryErrorMessage = (response: any) => response?.data?.message;
export const getReactQueryErrorMessage = (response?: ReactQueryError) =>
response?.data?.message || 'error';

export const formatDate = (date?: Date | string) =>
date ? format(new Date(date), 'yyyy-MM-dd') : '-';
Expand Down
1 change: 1 addition & 0 deletions src/utils/texts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const validationTexts = {
'Įrankis su šiuo plombos numeriu jau egzistuoja',
[ServerErrors.FISH_MUST_BE_WEIGHTED]:
'Sužvejotos žuvys turi būti pasvertos krante, prieš užbaigiant žvejybą',
[ServerErrors.FISH_ALREADY_WEIGHTED]: 'Žuvis krante jau buvo pasverta',
badFileTypes: 'Blogi failų tipai',
fileSizesExceeded: 'Viršyti failų dydžiai',
personalCode: 'Neteisingas asmens kodo formatas',
Expand Down
7 changes: 7 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,10 @@ export interface FishingHistoryResponse {
export interface GenericObject {
[key: string]: any;
}

export interface ReactQueryError {
data: {
type: string;
message: string;
};
}
Loading