Skip to content

Commit

Permalink
Switched jQuery to Axios and added an AbortController
Browse files Browse the repository at this point in the history
  • Loading branch information
lzach83 committed Jan 4, 2024
1 parent 954fdce commit 5fb796a
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import 'components/Profile/Profile.scss';
import $ from 'jquery';
import React, { useEffect, useState } from 'react';
import app from 'state';
import AddressInfo from '../../../models/AddressInfo';
Expand Down Expand Up @@ -34,16 +34,22 @@ const Profile = ({ profileId }: ProfileProps) => {
const [threads, setThreads] = useState<Thread[]>([]);
const [isOwner, setIsOwner] = useState<boolean>();

const getProfileData = async (query: string) => {
const getProfileData = async (query: string, signal: AbortSignal) => {
setLoading(true);
try {
const { result } = await $.get(`${app.serverUrl()}/profile/v2`, {
profileId: query,
jwt: app.user.jwt,
const response = await axios.get(`${app.serverUrl()}/profile/v2`, {
params: {
profileId: query,
jwt: app.user.jwt,
},
signal,
});

const { result } = response.data;

setProfile(new NewProfile(result.profile));
setThreads(result.threads.map((t) => new Thread(t)));

const responseComments = result.comments.map((c) => new Comment(c));
const commentsWithAssociatedThread = responseComments.map((c) => {
const thread = result.commentThreads.find(
Expand All @@ -52,6 +58,7 @@ const Profile = ({ profileId }: ProfileProps) => {
return { ...c, thread };
});
setComments(commentsWithAssociatedThread);

setAddresses(
result.addresses.map((a) => {
try {
Expand All @@ -70,6 +77,7 @@ const Profile = ({ profileId }: ProfileProps) => {
}
}),
);

setIsOwner(result.isOwner);
} catch (err) {
if (
Expand All @@ -83,8 +91,12 @@ const Profile = ({ profileId }: ProfileProps) => {
};

useEffect(() => {
getProfileData(profileId);
}, []);
const abortController = new AbortController();
const signal = abortController.signal;
getProfileData(profileId, signal);

return () => abortController.abort();
}, [profileId]);

if (loading)
return (
Expand Down

0 comments on commit 5fb796a

Please sign in to comment.