Skip to content
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
5 changes: 5 additions & 0 deletions src/api/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import axios from "axios";

export const apiClient = axios.create({
baseURL: "/api",
});
8 changes: 4 additions & 4 deletions src/components/header/header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";

import { apiClient } from "@/api/client";
import { LoginDialog } from "@/components/login-dialog";
import { Button } from "@/components/ui/button";
import { Me } from "@/types";
Expand All @@ -13,16 +13,16 @@ export function Header() {
const [hasError, setHasError] = useState(false);

useEffect(() => {
axios
.get<{ data: Me }>("/api/me")
apiClient
.get<{ data: Me }>("/me")
.then((response) => setMe(response.data.data))
.catch(() => setHasError(true))
.finally(() => setIsLoadingMe(false));
}, []);

async function logout() {
setIsLoadingLogout(true);
await axios.post(`/api/logout`);
await apiClient.post("/logout");
setIsLoadingLogout(false);
window.location.reload();
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/login-dialog/login-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { useState } from "react";

import { apiClient } from "@/api/client";
import { Button } from "@/components/ui/button";
import {
Dialog,
Expand Down Expand Up @@ -37,7 +37,7 @@ export function LoginDialog({ children }: LoginDialogProps) {
const username = event.currentTarget.elements.username.value;
const password = event.currentTarget.elements.password.value;

await axios.post(`/api/login`, { username, password });
await apiClient.post("/login", { username, password });

setIsLoading(false);
setOpen(false);
Expand Down
14 changes: 7 additions & 7 deletions src/components/shout/reply-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { useEffect, useState } from "react";

import { apiClient } from "@/api/client";
import { LoginDialog } from "@/components/login-dialog";
import { Button } from "@/components/ui/button";
import {
Expand Down Expand Up @@ -38,8 +38,8 @@ export function ReplyDialog({ children, shoutId }: ReplyDialogProps) {
const [hasError, setHasError] = useState(false);

useEffect(() => {
axios
.get<{ data: Me }>("/api/me")
apiClient
.get<{ data: Me }>("/me")
.then((response) => setIsAuthenticated(Boolean(response.data.data)))
.catch(() => setHasError(true))
.finally(() => setIsLoading(false));
Expand All @@ -59,17 +59,17 @@ export function ReplyDialog({ children, shoutId }: ReplyDialogProps) {
if (files?.length) {
const formData = new FormData();
formData.append("image", files[0]);
const imageResponse = await axios.post<{ data: Image }>(
"/api/image",
const imageResponse = await apiClient.post<{ data: Image }>(
"/image",
formData
);
imageId = imageResponse.data.data.id;
}
const newShoutResponse = await axios.post<{ data: Shout }>(`/api/shout`, {
const newShoutResponse = await apiClient.post<{ data: Shout }>(`/shout`, {
message,
imageId,
});
await axios.post(`/api/shout/${shoutId}/reply`, {
await apiClient.post(`/shout/${shoutId}/reply`, {
replyId: newShoutResponse.data.data.id,
});
setOpen(false);
Expand Down
6 changes: 3 additions & 3 deletions src/pages/feed/feed.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { useEffect, useState } from "react";

import { apiClient } from "@/api/client";
import { LoadingView } from "@/components/loading";
import { ShoutList } from "@/components/shout-list";
import { FeedResponse, Image, User } from "@/types";
Expand All @@ -10,8 +10,8 @@ export function Feed() {
const [hasError, setHasError] = useState(false);

useEffect(() => {
axios
.get<FeedResponse>("/api/feed")
apiClient
.get<FeedResponse>("/feed")
.then((response) => setFeed(response.data))
.catch(() => setHasError(true));
}, []);
Expand Down
10 changes: 5 additions & 5 deletions src/pages/user-profile/user-profile.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { useEffect, useState } from "react";
import { Navigate, useParams } from "react-router";

import { apiClient } from "@/api/client";
import { LoadingSpinner } from "@/components/loading";
import { ShoutList } from "@/components/shout-list";
import { UserResponse, UserShoutsResponse } from "@/types";
Expand All @@ -16,13 +16,13 @@ export function UserProfile() {
const [hasError, setHasError] = useState(false);

useEffect(() => {
axios
.get<UserResponse>(`/api/user/${handle}`)
apiClient
.get<UserResponse>(`/user/${handle}`)
.then((response) => setUser(response.data))
.catch(() => setHasError(true));

axios
.get<UserShoutsResponse>(`/api/user/${handle}/shouts`)
apiClient
.get<UserShoutsResponse>(`/user/${handle}/shouts`)
.then((response) => setUserShouts(response.data))
.catch(() => setHasError(true));
}, [handle]);
Expand Down