Skip to content

Commit

Permalink
Update - redux filter
Browse files Browse the repository at this point in the history
  • Loading branch information
0147123 committed Aug 27, 2024
1 parent 75385c1 commit 19d2598
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
38 changes: 38 additions & 0 deletions src/pages/testing/testing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { SendEmailLinkContent, useGetLoginLinkMutation, useAccessMutation, useLo
import { couponUserSlice, selectCurrentEmail } from "../../redux/couponUser";
import { useGetCouponsByUserMutation } from "../../redux/couponAPI";
import { couponSlice, selectCurrentCouponList } from "../../redux/coupon";
import { useGetFilterOptionMutation } from "../../redux/filterAPI";
import { FilterOption, filterSlice, selectCurrentFilterOption } from "../../redux/filter";

interface TestingProps {}

Expand All @@ -23,8 +25,11 @@ const Testing: React.FunctionComponent<TestingProps> = (props) => {
const [getLoginLink, { isSuccess: isSendLoginLinkSuccess, data, error }] = useGetLoginLinkMutation();
const [login, { isSuccess: isLoginSuccess, isLoading: isLoginLoading, data: loginData, error: loginError }] = useAccessMutation();
const [getCouponsByUser, { isSuccess: isGetCouponsByUser, error: getCouponError }] = useGetCouponsByUserMutation();
const [getFilterOption, { isSuccess: isGetFilterOptionSuccess, error: getFilterOptionError }] = useGetFilterOptionMutation();
const loginedEmail = useSelector(selectCurrentEmail);
const couponList = useSelector(selectCurrentCouponList);
const filterOption: FilterOption = useSelector(selectCurrentFilterOption);


const [logout, { isSuccess: isLogoutSuccess, error: logoutError }] = useLogoutMutation();
const location = useLocation();
Expand Down Expand Up @@ -87,14 +92,29 @@ const Testing: React.FunctionComponent<TestingProps> = (props) => {
});
};

const getFilter = async () => {
getFilterOption()
.then((res) => {
console.log(res);
if ('data' in res) {
dispatch(filterSlice.actions.setFilterOption(res.data));
} else {
console.error('Failed to fetch filter options:', res.error);
}
})

}

const content: JSX.Element = (
<>
<button onClick={emailLogin}>testing get email login</button>
<input type="text" value={email} onChange={(e) => setEmail(e.target.value)} />
<button onClick={userLogout}>Logout</button>
<button onClick={getCoupon}>Get Coupon</button>
<button onClick={getFilter}>Get Filter</button>
{isSendLoginLinkSuccess && <p style={{ color: "white" }}>send the email link</p>}
{isLoginSuccess && <p style={{ color: "white" }}>login success</p>}
{isLogoutSuccess && <p style={{ color: "white" }}>logout success</p>}
{couponList.map((coupon, index) => {
return (
<div key={index}>
Expand All @@ -103,6 +123,24 @@ const Testing: React.FunctionComponent<TestingProps> = (props) => {
</div>
);
})}
{isGetFilterOptionSuccess && <p style={{ color: "white" }}>get filter option</p>}
{isGetFilterOptionSuccess && <p style={{ color: "white" }}><br/>industry:</p>}
{filterOption.industry.map((option, index) => {
return (
<div key={index}>
<div style={{ color: "white" }}>{option.industry_name}</div>
</div>
);
})}
{isGetFilterOptionSuccess && <p style={{ color: "white" }}><br/>merchant</p>}
{filterOption.merchant.map((option, index) => {
return (
<div key={index}>
<div style={{ color: "white" }}>{option.merchant_name}</div>
</div>
);
})}

</>
);

Expand Down
15 changes: 9 additions & 6 deletions src/redux/filterAPI.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { apiSlice } from "./couponSystemAPISlice";
import { FilterOption } from "./filter";

export const filterAPI = apiSlice.injectEndpoints({
endpoints: builder => ({
getFilterOption: builder.mutation({
query: credentials => ({
url: '/filter/getCouponFilterOption',
method: 'POST',
body: { ...credentials }
})
getFilterOption: builder.mutation<FilterOption, void>({
query: () => ({
url: '/coupon/getCouponFilterOption',
method: 'GET',
}),
transformResponse: (response: FilterOption) => response,
}),
})
})

export const { useGetFilterOptionMutation } = filterAPI;

0 comments on commit 19d2598

Please sign in to comment.