Skip to content

Commit

Permalink
Fix API fetching and posting by replacing node-fetch with Axios module
Browse files Browse the repository at this point in the history
  • Loading branch information
n1klaus committed Jul 15, 2023
1 parent 0c5b735 commit 46aba79
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 51 deletions.
8 changes: 4 additions & 4 deletions frontend/src/pages/history.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function HistoryPage() {
useEffect(() => {
const SERVER_ENDPOINT = import.meta.env.VITE_BACKEND_ENDPOINT;
const fetchVideos = async () => {
await axios.get(
`${SERVER_ENDPOINT}/users/${user?._id}/history`,
{ withCredentials: true },
)
await axios
.get(`${SERVER_ENDPOINT}/users/${user?._id}/history`, {
withCredentials: true,
})
.then((data) => {
setVideos(data.data);
});
Expand Down
19 changes: 10 additions & 9 deletions frontend/src/pages/profile.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ function ProfilePage() {
const fetchUser = async () => {
try {
store.setRequestLoading(true);
await axios.get(`${SERVER_ENDPOINT}/users/me`, {
withCredentials: true,
headers: {
'Content-Type': 'application/json',
},
})
await axios
.get(`${SERVER_ENDPOINT}/users/me`, {
withCredentials: true,
headers: {
'Content-Type': 'application/json',
},
})
.then((data) => {
if (data.status !== 200) {
throw new Error(data.statusText);
Expand Down Expand Up @@ -150,16 +151,16 @@ function ProfilePage() {
<p className="mb-3">
Subscriptions:
{' '}
{user?.subscriptions.length}
{user?.subscriptions?.length}
</p>
<p className="mb-3">
Channels:
{user.channels.length}
{user.channels?.length}
</p>
<p className="mb-3">
Default Channel:
{' '}
{user.channels.length > 0
{user.channels?.length > 0
? (user.channels[0] as unknown as IChannel)._id
: ''}
</p>
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/pages/subscriptions.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function SubscriptionsPage() {
useEffect(() => {
const SERVER_ENDPOINT = import.meta.env.VITE_BACKEND_ENDPOINT;
const fetchVideos = async () => {
await axios.get(
`${SERVER_ENDPOINT}/users/${user?._id}/subscriptions`,
{ withCredentials: true },
)
await axios
.get(`${SERVER_ENDPOINT}/users/${user?._id}/subscriptions`, {
withCredentials: true,
})
.then((data) => {
setChannels(data.data);
});
Expand Down
60 changes: 26 additions & 34 deletions frontend/src/pages/video.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,26 @@ function VideoPage() {
const currentVideo = useStore((state) => state.currentVideo);
const authUser = useStore((state) => state.authUser);
useEffect(() => {
const fetchData = async () => {
try {
const videoRes = await axios.get(
`${SERVER_ENDPOINT}/videos/${id}/view`,
)
.then((data: any) => {
store.setCurrentVideo(data.data);
return data.data;
});

await axios.get(
`${SERVER_ENDPOINT}/channels/${videoRes.channelId}/view`,
)
.then((data: any) => {
store.setCurrentChannel(data.data);
return data.data;
});
} catch (err) {
console.error(err);
}
};
fetchData();
}, []);
const fetchData = async () => {
try {
// Fetch video data
const videoResponse = await axios.get(`${SERVER_ENDPOINT}/videos/${id}/view`);
const videoData = videoResponse.data;
store.setCurrentVideo(videoData);

// Fetch channel data
const channelResponse = await axios.get(`${SERVER_ENDPOINT}/channels/${videoData.channelId}/view`);
const channelData = channelResponse.data;
store.setCurrentChannel(channelData);

} catch (error) {
console.error(error);
}
};

fetchData();
}, [id]);


const handleLike = async () => {
try {
Expand Down Expand Up @@ -357,12 +354,9 @@ function VideoPage() {
<ChannelDetail>
<ChannelName>{currentChannel?.name}</ChannelName>
<ChannelCounter>
{
currentChannel?.subscribers
&& currentChannel?.subscribers > 0
? currentChannel?.subscribers
: 0
}
{currentChannel?.subscribers && currentChannel?.subscribers > 0
? currentChannel?.subscribers
: 0}
{' '}
subscribers
</ChannelCounter>
Expand All @@ -378,11 +372,9 @@ function VideoPage() {
<Hr />
<Comments videoId={id} />
</Content>
{
currentVideo?.tags
? <Recommendation tags={Array.from(currentVideo?.tags)} />
: null
}
{currentVideo?.tags ? (
<Recommendation tags={Array.from(currentVideo?.tags)} />
) : null}
</Container>
);
}
Expand Down

0 comments on commit 46aba79

Please sign in to comment.