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

add error message #242

Merged
merged 1 commit into from
Mar 17, 2024
Merged
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
43 changes: 33 additions & 10 deletions src/components/screens/Upload.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { AxiosError } from 'axios';
import { Attachments } from 'components/gadgets/Attachments';
import { URLShare } from 'components/gadgets/URLShare';
import { ShortenUrlTile } from 'components/gadgets/URLShortener/ShortenUrlTile';
import { FeedbackLink, FeedbackTemplate } from 'components/sections/FeedbackLink';
import { logEvent } from 'firebase/analytics';
import mixpanel from 'mixpanel-browser';
import { useState } from 'react';
import { useMemo, useState } from 'react';
import { useMutation } from 'react-query';
import { createFileRequest } from 'requests';
import shortenSlice from 'store/shortenSlice';
import { BASE_URL } from 'types/constants';
import { BASE_URL, LIMIT_FEATURE_HOUR, LIMIT_FILE_REQUEST } from 'types/constants';
import { EVENTS_STATUS, FIREBASE_ANALYTICS_EVENT, MIXPANEL_EVENT } from 'types/utils';
import { analytics } from 'utils/firebase';
import { linkWithLanguage, useTrans } from 'utils/i18next';
Expand Down Expand Up @@ -68,20 +69,41 @@ export const Upload = () => {
requestFile.mutate(payload);
}
};
const mutateError = (requestFile.error as AxiosError)?.message;
const requestErrorMessage = useMemo(() => {
let errorMessage;
switch (mutateError) {
case 'EXCEEDED_FILE':
errorMessage = t('reachedFeatureLimit', {
n: LIMIT_FILE_REQUEST,
feature: t('uploadFile'),
time: `${LIMIT_FEATURE_HOUR} ${t('hour')}`,
});
break;
default:
errorMessage = mutateError;
break;
}
return errorMessage;
}, [mutateError]);

const error = localError || requestErrorMessage;
const loading = requestFile.isLoading;
const hasData = !loading && !requestFile.isError && shortenUrl;

return (
<>
<h1 className="mb-4 flex gap-1 text-xl md:text-3xl">{t('uploadFile')}</h1>
<Attachments
generateFor="file"
onChange={(attachment) => {
const mediaId = attachment?.at(-1)?.id;
if (!mediaId) return;
handleCreateFile(mediaId);
}}
/>
{!error && (
<Attachments
generateFor="file"
onChange={(attachment) => {
const mediaId = attachment?.at(-1)?.id;
if (!mediaId) return;
handleCreateFile(mediaId);
}}
/>
)}
{hasData && (
<>
<p>🚀 {t('fileSuccess')}</p>
Expand All @@ -101,6 +123,7 @@ export const Upload = () => {
<URLShare />
</>
)}
<p className="mt-4 text-red-400">{error}</p>
<FeedbackLink template={FeedbackTemplate.UPLOAD} />
</>
);
Expand Down
Loading