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

fix: onboarding loop #775

Merged
merged 7 commits into from
Apr 11, 2023
6 changes: 3 additions & 3 deletions apps/app/contexts/user.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import userService from "services/user.service";
// constants
import { CURRENT_USER } from "constants/fetch-keys";
// types
import type { IUser } from "types";
import type { ICurrentUserResponse, IUser } from "types";

interface IUserContextProps {
user?: IUser;
isUserLoading: boolean;
mutateUser: KeyedMutator<IUser>;
mutateUser: KeyedMutator<ICurrentUserResponse>;
assignedIssuesLength?: number;
workspaceInvitesLength?: number;
}
Expand All @@ -21,7 +21,7 @@ export const UserContext = createContext<IUserContextProps>({} as IUserContextPr

export const UserProvider = ({ children }: { children: ReactElement }) => {
// API to fetch user information
const { data, error, mutate } = useSWR<IUser>(CURRENT_USER, () => userService.currentUser(), {
const { data, error, mutate } = useSWR(CURRENT_USER, () => userService.currentUser(), {
shouldRetryOnError: false,
});

Expand Down
17 changes: 16 additions & 1 deletion apps/app/pages/onboarding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Logo from "public/onboarding/logo.svg";
import type { NextPage } from "next";
// fetch-keys
import { CURRENT_USER } from "constants/fetch-keys";
import { ICurrentUserResponse } from "types";

const Onboarding: NextPage = () => {
const [step, setStep] = useState(1);
Expand Down Expand Up @@ -76,7 +77,21 @@ const Onboarding: NextPage = () => {
userService
.updateUserOnBoard({ userRole })
.then(() => {
mutate(CURRENT_USER);
mutate<ICurrentUserResponse>(
CURRENT_USER,
(prevData) => {
if (!prevData) return prevData;

return {
...prevData,
user: {
...prevData.user,
is_onboarded: true,
},
};
},
false
);
router.push("/");
})
.catch((err) => {
Expand Down
9 changes: 7 additions & 2 deletions apps/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import APIService from "services/api.service";
import trackEventServices from "services/track-event.service";

import type { IUser, IUserActivityResponse, IUserWorkspaceDashboard } from "types";
import type {
ICurrentUserResponse,
IUser,
IUserActivityResponse,
IUserWorkspaceDashboard,
} from "types";

const { NEXT_PUBLIC_API_BASE_URL } = process.env;

Expand All @@ -29,7 +34,7 @@ class UserService extends APIService {
});
}

async currentUser(): Promise<any> {
async currentUser(): Promise<ICurrentUserResponse> {
return this.get("/api/users/me/")
.then((response) => response?.data)
.catch((error) => {
Expand Down
7 changes: 7 additions & 0 deletions apps/app/types/users.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IUser {
last_location: readonly string;
created_location: readonly string;
is_email_verified: boolean;
is_onboarded: boolean;
token: string;
role: string;

Expand All @@ -26,6 +27,12 @@ export interface IUser {
[...rest: string]: any;
}

export interface ICurrentUserResponse {
assigned_issues: number;
user: IUser;
workspace_invites: number;
}

export interface IUserLite {
readonly id: string;
first_name: string;
Expand Down