Skip to content

Commit

Permalink
fix(client): fixed export and import for zustand stores
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoSkorJjj committed Jan 4, 2024
1 parent 475c200 commit 276390f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@chakra-ui/react";

import PublicRoute from "~shared/PublicRoute";
import useAuthStore from "~shared/store/AuthStore";
import { useAuthStore } from "~shared/store";

import { useAuth } from "~features/auth";

Expand Down
2 changes: 1 addition & 1 deletion client/src/features/auth/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useToast } from "@chakra-ui/react";
import { CodeResponse, useGoogleLogin } from "@react-oauth/google";
import { AxiosError } from "axios";

import useAuthStore from "~shared/store/AuthStore";
import { useAuthStore } from "~shared/store";

import {
api,
Expand Down
21 changes: 14 additions & 7 deletions client/src/features/pages/posts/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
useReactTable,
} from "@tanstack/react-table";

import { usePostStore } from "~shared/store";

import makeData from "~features/components/data/MakeData";
import { Pagination } from "~features/components/pagination";
import { ColumnFilter, ColumnSorter } from "~features/components/table";
Expand All @@ -35,19 +37,24 @@ import { IPost } from "~features/interfaces";
import { usePostColumns } from "./index";

export const PostList: React.FC = () => {
const { posts, setPosts } = usePostStore();
const [isLoading, setIsLoading] = useState(true);
const [posts, setPosts] = useState<IPost[]>([]);

const columns: ColumnDef<IPost>[] = usePostColumns();

const fetchAllPosts = async () => {
// Replace the following with actual API call
const data: IPost[] = makeData(100);
setPosts(data);
setIsLoading(false);
};

useEffect(() => {
const fetchAllPosts = async () => {
// Replace the following with actual API call
const data: IPost[] = makeData(1000);
setPosts(data);
if (posts.length === 0) {
fetchAllPosts();
} else {
setIsLoading(false);
};
fetchAllPosts();
}
}, []);

const table = useReactTable({
Expand Down
28 changes: 17 additions & 11 deletions client/src/features/pages/posts/PostColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import {
} from "@chakra-ui/react";
import { ColumnDef, FilterFn } from "@tanstack/react-table";

import { useCategoryStore, usePostStore } from "~shared/store";

import { getAllCategories } from "~features/components/data";
import { FilterElementProps, ICategory, IPost } from "~features/interfaces";
import { FilterElementProps, IPost } from "~features/interfaces";

export const usePostColumns = (): ColumnDef<IPost>[] => {
const navigate = useNavigate();
const toast = useToast();
const [activePopoverId, setActivePopoverId] = useState<number | null>(null);
const [categories, setCategories] = useState<ICategory[]>([]);
const { removePost } = usePostStore();
const { categories, setCategories } = useCategoryStore();

const handleEdit = (id: number) => {
navigate(`/posts/edit/${id}`);
};
Expand All @@ -40,7 +44,8 @@ export const usePostColumns = (): ColumnDef<IPost>[] => {
};

const onDelete = () => {
// actual deletion logic here
// TODO: Add actual API call to remove post from database

Check warning on line 47 in client/src/features/pages/posts/PostColumns.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: Add actual API call to remove post...'
removePost(activePopoverId as number);
toast({ title: `Post deleted ${activePopoverId}`, status: "error" });
setActivePopoverId(null);
};
Expand All @@ -51,15 +56,16 @@ export const usePostColumns = (): ColumnDef<IPost>[] => {
return rowValue === filterNumber;
};

// Mock function to simulate loading categories - replace with actual API call
useEffect(() => {
// Simulate fetching categories data
const fetchAllCategories = async () => {
// Replace with actual API call
const fetchAllCategories = async () => {
// TODO: Replace with actual API call

Check warning on line 60 in client/src/features/pages/posts/PostColumns.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: Replace with actual API call'

setCategories(getAllCategories);
};
fetchAllCategories();
setCategories(getAllCategories);
};

useEffect(() => {
if (categories.length === 0) {
fetchAllCategories();
}
}, []);

return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import applicationPermissions from "~shared/models/Permissions";
import useAuthStore from "~shared/store/AuthStore";
import { useAuthStore } from "~shared/store";

class PermissionService {
// TODO: singleton instance

Check warning on line 5 in client/src/shared/services/permission/Permission.service.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: singleton instance'
Expand Down
4 changes: 1 addition & 3 deletions client/src/shared/store/AuthStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const customSessionStorage: ZustandStorageType = {
},
};

const useAuthStore = create<AuthStateType>()(
export const useAuthStore = create<AuthStateType>()(
persist(
(set) => ({
isAuthenticated: false,
Expand All @@ -65,5 +65,3 @@ const useAuthStore = create<AuthStateType>()(
},
),
);

export default useAuthStore;

0 comments on commit 276390f

Please sign in to comment.