initial commit
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/* eslint-disable react-refresh/only-export-components */
|
||||
import { FC, useState, createContext, useContext, useMemo } from "react";
|
||||
import {
|
||||
ID,
|
||||
calculatedGroupingIsDisabled,
|
||||
calculateIsAllDataSelected,
|
||||
groupingOnSelect,
|
||||
initialListView,
|
||||
ListViewContextProps,
|
||||
groupingOnSelectAll,
|
||||
WithChildren,
|
||||
} from "../../../../../_digifi/helpers";
|
||||
import {
|
||||
useQueryResponse,
|
||||
useQueryResponseData,
|
||||
} from "./QueryResponseProvider";
|
||||
|
||||
const ListViewContext = createContext<ListViewContextProps>(initialListView);
|
||||
|
||||
const ListViewProvider: FC<WithChildren> = ({ children }) => {
|
||||
const [selected, setSelected] = useState<Array<ID>>(initialListView.selected);
|
||||
const [itemIdForUpdate, setItemIdForUpdate] = useState<ID>(
|
||||
initialListView.itemIdForUpdate
|
||||
);
|
||||
const { isLoading } = useQueryResponse();
|
||||
const data = useQueryResponseData();
|
||||
const disabled = useMemo(
|
||||
() => calculatedGroupingIsDisabled(isLoading, data),
|
||||
[isLoading, data]
|
||||
);
|
||||
const isAllSelected = useMemo(
|
||||
() => calculateIsAllDataSelected(data, selected),
|
||||
[data, selected]
|
||||
);
|
||||
|
||||
return (
|
||||
<ListViewContext.Provider
|
||||
value={{
|
||||
selected,
|
||||
itemIdForUpdate,
|
||||
setItemIdForUpdate,
|
||||
disabled,
|
||||
isAllSelected,
|
||||
onSelect: (id: ID) => {
|
||||
groupingOnSelect(id, selected, setSelected);
|
||||
},
|
||||
onSelectAll: () => {
|
||||
groupingOnSelectAll(isAllSelected, setSelected, data);
|
||||
},
|
||||
clearSelected: () => {
|
||||
setSelected([]);
|
||||
},
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</ListViewContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
const useListView = () => useContext(ListViewContext);
|
||||
|
||||
export { ListViewProvider, useListView };
|
||||
@@ -0,0 +1,29 @@
|
||||
/* eslint-disable react-refresh/only-export-components */
|
||||
import { FC, useState, createContext, useContext } from "react";
|
||||
import {
|
||||
QueryState,
|
||||
QueryRequestContextProps,
|
||||
initialQueryRequest,
|
||||
WithChildren,
|
||||
} from "../../../../../_digifi/helpers";
|
||||
|
||||
const QueryRequestContext =
|
||||
createContext<QueryRequestContextProps>(initialQueryRequest);
|
||||
|
||||
const QueryRequestProvider: FC<WithChildren> = ({ children }) => {
|
||||
const [state, setState] = useState<QueryState>(initialQueryRequest.state);
|
||||
|
||||
const updateState = (updates: Partial<QueryState>) => {
|
||||
const updatedState = { ...state, ...updates } as QueryState;
|
||||
setState(updatedState);
|
||||
};
|
||||
|
||||
return (
|
||||
<QueryRequestContext.Provider value={{ state, updateState }}>
|
||||
{children}
|
||||
</QueryRequestContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
const useQueryRequest = () => useContext(QueryRequestContext);
|
||||
export { QueryRequestProvider, useQueryRequest };
|
||||
@@ -0,0 +1,87 @@
|
||||
/* eslint-disable react-refresh/only-export-components */
|
||||
/* eslint-disable react-hooks/exhaustive-deps */
|
||||
import { FC, useContext, useState, useEffect, useMemo } from "react";
|
||||
import { useQuery } from "react-query";
|
||||
import {
|
||||
createResponseContext,
|
||||
initialQueryResponse,
|
||||
initialQueryState,
|
||||
PaginationState,
|
||||
QUERIES,
|
||||
stringifyRequestQuery,
|
||||
WithChildren,
|
||||
} from "../../../../../_digifi/helpers";
|
||||
import { getReadyUsers } from "../../core/_requests";
|
||||
import { User } from "../../core/_models";
|
||||
import { useQueryRequest } from "./QueryRequestProvider";
|
||||
|
||||
const QueryResponseContext = createResponseContext<User>(initialQueryResponse);
|
||||
const QueryResponseProvider: FC<WithChildren> = ({ children }) => {
|
||||
const { state } = useQueryRequest();
|
||||
const [query, setQuery] = useState<string>(stringifyRequestQuery(state));
|
||||
const updatedQuery = useMemo(() => stringifyRequestQuery(state), [state]);
|
||||
|
||||
useEffect(() => {
|
||||
if (query !== updatedQuery) {
|
||||
setQuery(updatedQuery);
|
||||
}
|
||||
}, [updatedQuery]);
|
||||
|
||||
const {
|
||||
isFetching,
|
||||
refetch,
|
||||
data: response,
|
||||
} = useQuery(
|
||||
`${QUERIES.USERS_LIST}-${query}`,
|
||||
() => {
|
||||
return getReadyUsers(query);
|
||||
},
|
||||
{ cacheTime: 0, keepPreviousData: true, refetchOnWindowFocus: false }
|
||||
);
|
||||
|
||||
return (
|
||||
<QueryResponseContext.Provider
|
||||
value={{ isLoading: isFetching, refetch, response, query }}
|
||||
>
|
||||
{children}
|
||||
</QueryResponseContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
const useQueryResponse = () => useContext(QueryResponseContext);
|
||||
|
||||
const useQueryResponseData = () => {
|
||||
const { response } = useQueryResponse();
|
||||
if (!response) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return response?.records || [];
|
||||
};
|
||||
|
||||
const useQueryResponsePagination = () => {
|
||||
const defaultPaginationState: PaginationState = {
|
||||
links: [],
|
||||
...initialQueryState,
|
||||
};
|
||||
|
||||
const { response } = useQueryResponse();
|
||||
if (!response || !response.payload || !response.payload.pagination) {
|
||||
return defaultPaginationState;
|
||||
}
|
||||
|
||||
return response.payload.pagination;
|
||||
};
|
||||
|
||||
const useQueryResponseLoading = (): boolean => {
|
||||
const { isLoading } = useQueryResponse();
|
||||
return isLoading;
|
||||
};
|
||||
|
||||
export {
|
||||
QueryResponseProvider,
|
||||
useQueryResponse,
|
||||
useQueryResponseData,
|
||||
useQueryResponsePagination,
|
||||
useQueryResponseLoading,
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
import { ID, Response } from "../../../../../_digifi/helpers";
|
||||
export type User = {
|
||||
id?: ID;
|
||||
name?: string;
|
||||
avatar?: string;
|
||||
// email?: string
|
||||
position?: string;
|
||||
role?: string;
|
||||
last_login?: string;
|
||||
two_steps?: boolean;
|
||||
joined_day?: string;
|
||||
online?: boolean;
|
||||
initials?: {
|
||||
label: string;
|
||||
state: string;
|
||||
};
|
||||
firstname?: string;
|
||||
lastname?: string;
|
||||
uid?: string;
|
||||
loan_amount?: string;
|
||||
payment_month?: string;
|
||||
sales_agent?: string;
|
||||
gender?: string | null;
|
||||
marital_status?: string;
|
||||
email?: string;
|
||||
address?: string;
|
||||
state?: string;
|
||||
country?: string;
|
||||
status?: string;
|
||||
added?: string;
|
||||
updated?: string;
|
||||
};
|
||||
|
||||
export type UsersQueryResponse = Response<Array<User>>;
|
||||
|
||||
export const initialUser: User = {
|
||||
avatar: "avatars/300-6.jpg",
|
||||
position: "Art Director",
|
||||
role: "Administrator",
|
||||
name: "",
|
||||
email: "",
|
||||
};
|
||||
@@ -0,0 +1,60 @@
|
||||
import axios, { AxiosResponse } from "axios";
|
||||
import { ID, Response } from "../../../../../_digifi/helpers";
|
||||
import { User, UsersQueryResponse } from "./_models";
|
||||
|
||||
const API_URL = import.meta.env.VITE_APP_THEME_API_URL;
|
||||
const USER_URL = `${API_URL}/user`;
|
||||
// const GET_USERS_URL = `${API_URL}/users/query`;
|
||||
|
||||
const NEW_USER_ENDPOINT = import.meta.env.VITE_APP_USER_ENDPOINT;
|
||||
|
||||
// const getStartedUsers = (query: string): Promise<UsersQueryResponse> => {
|
||||
// return axios
|
||||
// .get(`${GET_USERS_URL}?${query}`)
|
||||
// .then((d: AxiosResponse<UsersQueryResponse>) => d.data);
|
||||
// };
|
||||
const getStartedUsers = (query: string): Promise<UsersQueryResponse> => {
|
||||
// FUNCTION TO GET USERS THAT HAVE STARTED LOAN APPLICATION
|
||||
return axios
|
||||
.get(`${NEW_USER_ENDPOINT}/loan/started`)
|
||||
.then((d: AxiosResponse<UsersQueryResponse>) => d.data);
|
||||
};
|
||||
|
||||
const getUserById = (id: ID): Promise<User | undefined> => {
|
||||
return axios
|
||||
.get(`${USER_URL}/${id}`)
|
||||
.then((response: AxiosResponse<Response<User>>) => response.data)
|
||||
.then((response: Response<User>) => response.data);
|
||||
};
|
||||
|
||||
const createUser = (user: User): Promise<User | undefined> => {
|
||||
return axios
|
||||
.put(USER_URL, user)
|
||||
.then((response: AxiosResponse<Response<User>>) => response.data)
|
||||
.then((response: Response<User>) => response.data);
|
||||
};
|
||||
|
||||
const updateUser = (user: User): Promise<User | undefined> => {
|
||||
return axios
|
||||
.post(`${USER_URL}/${user.id}`, user)
|
||||
.then((response: AxiosResponse<Response<User>>) => response.data)
|
||||
.then((response: Response<User>) => response.data);
|
||||
};
|
||||
|
||||
const deleteUser = (userId: ID): Promise<void> => {
|
||||
return axios.delete(`${USER_URL}/${userId}`).then(() => {});
|
||||
};
|
||||
|
||||
const deleteSelectedUsers = (userIds: Array<ID>): Promise<void> => {
|
||||
const requests = userIds.map((id) => axios.delete(`${USER_URL}/${id}`));
|
||||
return axios.all(requests).then(() => {});
|
||||
};
|
||||
|
||||
export {
|
||||
getStartedUsers,
|
||||
deleteUser,
|
||||
deleteSelectedUsers,
|
||||
getUserById,
|
||||
createUser,
|
||||
updateUser,
|
||||
};
|
||||
Reference in New Issue
Block a user