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
13 changes: 13 additions & 0 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Credentials } from "@/types";

import { apiClient } from "./client";

async function login(credentials: Credentials) {
await apiClient.post("/login", credentials);
}

async function logout() {
await apiClient.post("/logout");
}

export default { login, logout };
10 changes: 10 additions & 0 deletions src/api/feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FeedResponse } from "@/types";

import { apiClient } from "./client";

async function getFeed() {
const response = await apiClient.get<FeedResponse>("/feed");
return response.data;
}

export default { getFeed };
10 changes: 10 additions & 0 deletions src/api/media.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Image } from "@/types";

import { apiClient } from "./client";

async function uploadImage(formData: FormData) {
const response = await apiClient.post<{ data: Image }>("/image", formData);
return response.data;
}

export default { uploadImage };
18 changes: 18 additions & 0 deletions src/api/shout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CreateShoutInput, CreateShoutReplyInput, Shout } from "@/types";

import { apiClient } from "./client";

async function createShout(input: CreateShoutInput) {
const response = await apiClient.post<{ data: Shout }>(`/shout`, input);
return response.data;
}

async function createReply({ shoutId, replyId }: CreateShoutReplyInput) {
const response = await apiClient.post<{ data: Shout }>(
`/shout/${shoutId}/reply`,
{ replyId }
);
return response.data;
}

export default { createShout, createReply };
22 changes: 22 additions & 0 deletions src/api/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Me, User, UserShoutsResponse } from "@/types";

import { apiClient } from "./client";

async function getMe() {
const response = await apiClient.get<{ data: Me }>("/me");
return response.data;
}

async function getUser(handle: string) {
const response = await apiClient.get<{ data: User }>(`/user/${handle}`);
return response.data;
}

async function getUserShouts(handle: string) {
const response = await apiClient.get<UserShoutsResponse>(
`/user/${handle}/shouts`
);
return response.data;
}

export default { getMe, getUser, getUserShouts };
10 changes: 5 additions & 5 deletions src/components/header/header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";

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

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

async function logout() {
setIsLoadingLogout(true);
await apiClient.post("/logout");
await AuthApi.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 { useState } from "react";

import { apiClient } from "@/api/client";
import AuthApi from "@/api/auth";
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 apiClient.post("/login", { username, password });
await AuthApi.login({ username, password });

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

import { apiClient } from "@/api/client";
import MediaApi from "@/api/media";
import ShoutApi from "@/api/shout";
import UserApi from "@/api/user";
import { LoginDialog } from "@/components/login-dialog";
import { Button } from "@/components/ui/button";
import {
Expand All @@ -15,7 +17,6 @@ import {
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Image, Me, Shout } from "@/types";

interface ReplyFormElements extends HTMLFormControlsCollection {
message: HTMLTextAreaElement;
Expand All @@ -38,9 +39,8 @@ export function ReplyDialog({ children, shoutId }: ReplyDialogProps) {
const [hasError, setHasError] = useState(false);

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

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

useEffect(() => {
apiClient
.get<FeedResponse>("/feed")
.then((response) => setFeed(response.data))
FeedApi.getFeed()
.then((feed) => setFeed(feed))
.catch(() => setHasError(true));
}, []);

Expand Down
16 changes: 9 additions & 7 deletions src/pages/user-profile/user-profile.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import { Navigate, useParams } from "react-router";

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

useEffect(() => {
apiClient
.get<UserResponse>(`/user/${handle}`)
.then((response) => setUser(response.data))
if (!handle) {
return;
}

UserApi.getUser(handle)
.then((response) => setUser(response))
.catch(() => setHasError(true));

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

Expand Down