Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDP-949] State refactor: countries #42

Merged
merged 3 commits into from
Oct 31, 2023
Merged
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
18 changes: 0 additions & 18 deletions src/api/getAssetsByWallet.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/api/getCountries.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/apiQueries/useCountries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useQuery } from "@tanstack/react-query";
import { API_URL } from "constants/settings";
import { fetchApi } from "helpers/fetchApi";
import { ApiCountry, AppError } from "types";

export const useCountries = () => {
const query = useQuery<ApiCountry[], AppError>({
queryKey: ["countries"],
queryFn: async () => {
return await fetchApi(`${API_URL}/countries`);
},
// Keeping the fetched data for longer since it won't change that often
staleTime: 5 * 60 * 1000,
});

return query;
};
36 changes: 12 additions & 24 deletions src/components/DisbursementDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { useEffect } from "react";
import {
Card,
Input,
Select,
Title,
Notification,
} from "@stellar/design-system";
import { useDispatch } from "react-redux";

import { AppDispatch } from "store";
import { getCountriesAction } from "store/ducks/countries";

import { useWallets } from "apiQueries/useWallets";
import { useAssetsByWallet } from "apiQueries/useAssetsByWallet";
import { useCountries } from "apiQueries/useCountries";
import { InfoTooltip } from "components/InfoTooltip";
import { formatUploadedFileDisplayName } from "helpers/formatUploadedFileDisplayName";
import { useRedux } from "hooks/useRedux";
import {
ApiAsset,
ApiCountry,
Expand Down Expand Up @@ -61,8 +56,6 @@ export const DisbursementDetails: React.FC<DisbursementDetailsProps> = ({
onChange,
onValidate,
}: DisbursementDetailsProps) => {
const { countries } = useRedux("countries");

enum FieldId {
NAME = "name",
COUNTRY_CODE = "country_code",
Expand All @@ -76,23 +69,20 @@ export const DisbursementDetails: React.FC<DisbursementDetailsProps> = ({
isLoading: isWalletsLoading,
} = useWallets();

const {
data: countries,
error: countriesError,
isLoading: isCountriesLoading,
} = useCountries();

const {
data: walletAssets,
error: walletError,
isFetching: isWalletAssetsFetching,
} = useAssetsByWallet(details.wallet.id);

const dispatch: AppDispatch = useDispatch();

// Don't fetch again if we already have them in store
useEffect(() => {
if (!countries.status) {
dispatch(getCountriesAction());
}
}, [dispatch, countries.status]);

const apiErrors = [
countries.errorString,
countriesError?.message,
walletsError?.message,
walletError?.message,
];
Expand Down Expand Up @@ -141,9 +131,7 @@ export const DisbursementDetails: React.FC<DisbursementDetailsProps> = ({
switch (id) {
case FieldId.COUNTRY_CODE:
// eslint-disable-next-line no-case-declarations
const country = countries.items.find(
(c: ApiCountry) => c.code === value,
);
const country = countries?.find((c: ApiCountry) => c.code === value);

updateState({
country: {
Expand Down Expand Up @@ -244,10 +232,10 @@ export const DisbursementDetails: React.FC<DisbursementDetailsProps> = ({
fieldSize="sm"
onChange={updateDraftDetails}
value={details.country.code}
disabled={countries.status === "PENDING"}
disabled={isCountriesLoading}
>
{renderDropdownDefault(countries.status === "PENDING")}
{countries.items.map((country: ApiCountry) => (
{renderDropdownDefault(isCountriesLoading)}
{countries?.map((country: ApiCountry) => (
<option key={country.code} value={country.code}>
{country.name}
</option>
Expand Down
63 changes: 0 additions & 63 deletions src/store/ducks/countries.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import BigNumber from "bignumber.js";

import { RESET_STORE_ACTION_TYPE } from "constants/settings";

import { reducer as countries } from "store/ducks/countries";
import { reducer as disbursementDetails } from "store/ducks/disbursementDetails";
import { reducer as disbursementDrafts } from "store/ducks/disbursementDrafts";
import { reducer as disbursements } from "store/ducks/disbursements";
Expand All @@ -32,7 +31,6 @@ const isSerializable = (value: any) =>
BigNumber.isBigNumber(value) || isPlain(value);

const reducers = combineReducers({
countries,
disbursementDetails,
disbursementDrafts,
disbursements,
Expand Down
7 changes: 0 additions & 7 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ export type UserAccountInitialState = {
restoredPathname?: string;
};

export type CountriesInitialState = {
items: ApiCountry[];
status: ActionStatus | undefined;
errorString?: string;
};

export type DisbursementDraftsInitialState = {
items: DisbursementDraft[];
status: ActionStatus | undefined;
Expand Down Expand Up @@ -119,7 +113,6 @@ export type ProfileInitialState = {
};

export interface Store {
countries: CountriesInitialState;
disbursementDetails: DisbursementDetailsInitialState;
disbursementDrafts: DisbursementDraftsInitialState;
disbursements: DisbursementsInitialState;
Expand Down