Skip to content

Commit

Permalink
feat: re-fetch projects locally when changing api key
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rbit committed Jan 31, 2025
1 parent 328bcea commit 466c1d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 13 additions & 6 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface PostContext {
tags: string[],
setTags: Dispatch<SetStateAction<string[]>>,
projects: ProjectsResponse,
refetchProjects: () => Promise<void>
}
export interface AuthContext {
user: any | null,
Expand All @@ -33,7 +34,7 @@ export const API_URL: string = import.meta.env.VITE_API_URL;
const VERSION = "v0.5.0";
console.log("Version: " + VERSION);

export const PostContext = React.createContext<PostContext>({ posts: {} as PostsResponse, setPosts: () => { }, categories: {} as CategoryResponse, setCategories: () => { }, tags: [], setTags: () => { }, projects: []});
export const PostContext = React.createContext<PostContext>({ posts: {} as PostsResponse, setPosts: () => { }, categories: {} as CategoryResponse, setCategories: () => { }, tags: [], setTags: () => { }, projects: [], refetchProjects: async () => { } });
export const AuthContext = React.createContext<AuthContext>({ user: null });

function App() {
Expand Down Expand Up @@ -76,6 +77,14 @@ function MainContent() {
const [projects, setProjects] = useState({} as ProjectsResponse);
const [loading, setLoading] = useState(true);

const refetchProjects = async () => {
setProjects([]);
const project_res = await fetch(`${API_URL}/projects`, { credentials: "include" });
if (!project_res.ok) throw new Error("Couldn't fetch projects");
setProjects(SCHEMA.PROJECTS_RESPONSE.parse(await project_res.json()));
}


useEffect(() => {
(async () => {
const response = await fetch(`${API_URL}/posts?limit=-1`, { credentials: "include" });
Expand All @@ -90,10 +99,8 @@ function MainContent() {
const tag_res = await fetch(`${API_URL}/tags`, { credentials: "include" });
const tag_result = await tag_res.json();
setTags(tag_result);

const project_res = await fetch(`${API_URL}/projects`, { credentials: "include" });
if (!project_res.ok) throw new Error("Couldn't fetch projects");
setProjects(SCHEMA.PROJECTS_RESPONSE.parse(await project_res.json()));

await refetchProjects();

setLoading(false);
})();
Expand All @@ -102,7 +109,7 @@ function MainContent() {
if (loading) return <LoadingPage />

return (
<PostContext.Provider value={{ posts, setPosts, categories, setCategories, tags, setTags, projects }}>
<PostContext.Provider value={{ posts, setPosts, categories, setCategories, tags, setTags, projects, refetchProjects }}>
<nav>
<Sidebar page={page} setPage={setPage} />
</nav>
Expand Down
4 changes: 3 additions & 1 deletion client/src/pages/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Check, Edit, Link, Palette, Plus, RefreshCw, Save, Search, Shield, Trash, Unlink, User, X } from "lucide-react";
import { useContext, useEffect, useState } from "react";
import { API_URL, AuthContext } from "../App";
import { API_URL, AuthContext, PostContext } from "../App";
import { AccessKey } from "../../schema";
import { Oval } from "react-loader-spinner";
import { BuildingPage } from "../components/Building";
Expand Down Expand Up @@ -306,6 +306,7 @@ function TokenRow({ token, save, remove }: { token: TokenCreation, save: (token:
}

function DevpadCard({ refetch }: { refetch: () => Promise<void> }) {
const { refetchProjects } = useContext(PostContext);
const { devpad_key, last_cache } = useContext(IntegrationsContext);
const [open, setOpen] = useState(false);

Expand All @@ -324,6 +325,7 @@ function DevpadCard({ refetch }: { refetch: () => Promise<void> }) {
return;
}
await refetch();
await refetchProjects();
setLoading(false);
};

Expand Down

0 comments on commit 466c1d3

Please sign in to comment.