Skip to content

Commit

Permalink
Merge pull request #113 from gnolang/refactor/api-from-redux
Browse files Browse the repository at this point in the history
refactor: consuming the api from redux
  • Loading branch information
iuricmp authored Jul 5, 2024
2 parents 88e48fa + 3546d7d commit 2734889
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
6 changes: 2 additions & 4 deletions mobile/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export default function Root() {
return;
}

const bech32 = await gnonative.addressToBech32(keyInfo.address);
await dispatch(loggedIn({ keyInfo, bech32 }));
await dispatch(loggedIn({ keyInfo }));
setTimeout(() => route.replace("/home"), 500);
} catch (error: unknown | Error) {
setLoading(error?.toString());
Expand All @@ -65,8 +64,7 @@ export default function Root() {

const onCloseReenterPassword = async (sucess: boolean) => {
if (sucess && reenterPassword) {
const bech32 = await gnonative.addressToBech32(reenterPassword.address);
await dispatch(loggedIn({ keyInfo: reenterPassword, bech32 }));
await dispatch(loggedIn({ keyInfo: reenterPassword }));
setTimeout(() => route.replace("/home"), 500);
}
setReenterPassword(undefined);
Expand Down
3 changes: 1 addition & 2 deletions mobile/app/sign-up.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ export default function Page() {
await gnonative.selectAccount(name);
await gnonative.setPassword(password);
await onboarding.onboard(newAccount.name, newAccount.address);
const bech32 = await gnonative.addressToBech32(newAccount.address);
await dispatch(loggedIn({ keyInfo: newAccount, bech32 }));
await dispatch(loggedIn({ keyInfo: newAccount }));
router.push("/home");
} catch (error) {
RNAlert.alert("Error", "" + error);
Expand Down
15 changes: 12 additions & 3 deletions mobile/redux/features/accountSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { AsyncThunkOptions, createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { User } from "@gno/types";
import { KeyInfo } from "@buf/gnolang_gnonative.bufbuild_es/gnonativetypes_pb";
import { GnoNativeApi } from "@gnolang/gnonative";
import { ThunkExtra } from "redux/redux-provider";

export interface CounterState {
account?: User;
Expand All @@ -10,9 +12,16 @@ const initialState: CounterState = {
account: undefined,
};

export const loggedIn = createAsyncThunk("user/loggedIn", async (param: { keyInfo: KeyInfo; bech32: string }, _) => {
const { keyInfo, bech32 } = param;
interface LoginParam {
keyInfo: KeyInfo;
}

export const loggedIn = createAsyncThunk<User, LoginParam, ThunkExtra>("user/loggedIn", async (param, config) => {
const { keyInfo } = param;

const gnonative = config.extra.gnonative as GnoNativeApi;

const bech32 = await gnonative.addressToBech32(keyInfo.address);
const user: User = { address: bech32, name: keyInfo.name };

return user;
Expand Down
6 changes: 5 additions & 1 deletion mobile/redux/redux-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import React from "react";
import { Provider } from "react-redux";
import { configureStore } from "@reduxjs/toolkit";
import { accountSlice, profileSlice, replySlice } from "./features";
import { useGnoNativeContext } from "@gnolang/gnonative";
import { GnoNativeApi, useGnoNativeContext } from "@gnolang/gnonative";

interface Props {
children: React.ReactNode;
}

export interface ThunkExtra {
extra: { gnonative: GnoNativeApi };
}

const ReduxProvider: React.FC<Props> = ({ children }) => {
// Exposing GnoNative API to reduxjs/toolkit
const { gnonative } = useGnoNativeContext();
Expand Down

0 comments on commit 2734889

Please sign in to comment.