Commit 7dc8252b authored by Max Alekseenko's avatar Max Alekseenko

add try-catch

parent fbc0ac74
...@@ -12,7 +12,7 @@ import * as mixpanel from 'lib/mixpanel/index'; ...@@ -12,7 +12,7 @@ import * as mixpanel from 'lib/mixpanel/index';
import { ADDRESS_COUNTERS } from 'stubs/address'; import { ADDRESS_COUNTERS } from 'stubs/address';
const feature = config.features.marketplace; const feature = config.features.marketplace;
const base = (feature.isEnabled && feature.rating) ? const airtable = (feature.isEnabled && feature.rating) ?
new Airtable({ apiKey: feature.rating.airtableApiKey }).base(feature.rating.airtableBaseId) : new Airtable({ apiKey: feature.rating.airtableApiKey }).base(feature.rating.airtableBaseId) :
undefined; undefined;
...@@ -57,13 +57,21 @@ export default function useRatings() { ...@@ -57,13 +57,21 @@ export default function useRatings() {
const [ canRate, setCanRate ] = useState<boolean | undefined>(undefined); const [ canRate, setCanRate ] = useState<boolean | undefined>(undefined);
const fetchRatings = useCallback(async() => { const fetchRatings = useCallback(async() => {
if (!base) { if (!airtable) {
return; return;
} }
const data = await base('apps_ratings').select({ fields: [ 'appId', 'rating' ] }).all(); try {
const ratings = formatRatings(data); const data = await airtable('apps_ratings').select({ fields: [ 'appId', 'rating' ] }).all();
setRatings(ratings); const ratings = formatRatings(data);
}, []); setRatings(ratings);
} catch (error) {
toast({
status: 'error',
title: 'Error loading ratings',
description: 'Please try again later',
});
}
}, [ toast ]);
useEffect(() => { useEffect(() => {
async function fetch() { async function fetch() {
...@@ -78,18 +86,26 @@ export default function useRatings() { ...@@ -78,18 +86,26 @@ export default function useRatings() {
async function fetchUserRatings() { async function fetchUserRatings() {
setIsUserRatingLoading(true); setIsUserRatingLoading(true);
let userRatings = {} as Record<string, AppRating>; let userRatings = {} as Record<string, AppRating>;
if (address && base) { if (address && airtable) {
const data = await base('users_ratings').select({ try {
filterByFormula: `address = "${ address }"`, const data = await airtable('users_ratings').select({
fields: [ 'appId', 'rating' ], filterByFormula: `address = "${ address }"`,
}).all(); fields: [ 'appId', 'rating' ],
userRatings = formatRatings(data); }).all();
userRatings = formatRatings(data);
} catch (error) {
toast({
status: 'error',
title: 'Error loading user ratings',
description: 'Please try again later',
});
}
} }
setUserRatings(userRatings); setUserRatings(userRatings);
setIsUserRatingLoading(false); setIsUserRatingLoading(false);
} }
fetchUserRatings(); fetchUserRatings();
}, [ address ]); }, [ address, toast ]);
useEffect(() => { useEffect(() => {
// TODO: uncomment validation after testing // TODO: uncomment validation after testing
...@@ -108,12 +124,12 @@ export default function useRatings() { ...@@ -108,12 +124,12 @@ export default function useRatings() {
setIsSending(true); setIsSending(true);
try { try {
if (!address || !base) { if (!address || !airtable) {
throw new Error('Address is missing'); throw new Error('Address is missing');
} }
if (!appRecordId) { if (!appRecordId) {
const records = await base('apps_ratings').create([ { fields: { appId } } ]); const records = await airtable('apps_ratings').create([ { fields: { appId } } ]);
appRecordId = records[0].id; appRecordId = records[0].id;
if (!appRecordId) { if (!appRecordId) {
throw new Error('Record ID is missing'); throw new Error('Record ID is missing');
...@@ -121,7 +137,7 @@ export default function useRatings() { ...@@ -121,7 +137,7 @@ export default function useRatings() {
} }
if (!userRecordId) { if (!userRecordId) {
const userRecords = await base('users_ratings').create([ const userRecords = await airtable('users_ratings').create([
{ {
fields: { fields: {
address, address,
...@@ -132,7 +148,7 @@ export default function useRatings() { ...@@ -132,7 +148,7 @@ export default function useRatings() {
]); ]);
userRecordId = userRecords[0].id; userRecordId = userRecords[0].id;
} else { } else {
await base('users_ratings').update(userRecordId, { rating }); await airtable('users_ratings').update(userRecordId, { rating });
} }
setUserRatings({ setUserRatings({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment