Skip to content

Commit

Permalink
using createProject state to handle background and profile images
Browse files Browse the repository at this point in the history
  • Loading branch information
wpdas committed Jul 3, 2024
1 parent daff81b commit 914c3dd
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/app/_store/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Models } from "@rematch/core";

import { auth } from "@/modules/auth/state";
import { core } from "@/modules/core/state";
import { createProject } from "@/modules/create-project/state";
import { donationModel } from "@/modules/donation";
import { navModel, profilesModel } from "@/modules/profile/models";

Expand All @@ -11,6 +12,7 @@ export interface RootModel extends Models<RootModel> {
profiles: typeof profilesModel;
nav: typeof navModel;
core: typeof core;
createProject: typeof createProject;
}

export const models: RootModel = {
Expand All @@ -19,4 +21,5 @@ export const models: RootModel = {
profiles: profilesModel,
nav: navModel,
core,
createProject,
};
43 changes: 19 additions & 24 deletions src/modules/create-project/components/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/* eslint-disable @next/next/no-img-element */
import { useEffect, useState } from "react";
import { useEffect } from "react";

// https://www.npmjs.com/package/react-files
import Files from "react-files";

import { dispatch, useTypedSelector } from "@/app/_store";
import CameraIcon from "@/common/assets/svgs/CameraIcon";
import { IPFS_NEAR_SOCIAL_URL } from "@/common/constants";
import { Button } from "@/common/ui/components";
import Spinner from "@/modules/core/components/Spinner";
import useStatus from "@/modules/core/hooks/usStatus";
import uploadFileToIPFS from "@/modules/core/services/uploadFileToIPFS";
import useProfileData from "@/modules/profile/hooks/useProfileData";

type Props = {
Expand All @@ -18,44 +17,40 @@ type Props = {

const Profile = ({ accountId }: Props) => {
const profileData = useProfileData(accountId);
const [backgroundImage, setBackgroundImage] = useState("");
const [profileImage, setProfileImage] = useState("");
const bgImageStatus = useStatus();
const profileImageStatus = useStatus();

const { backgroundImage, profileImage } = useTypedSelector(
(state) => state.createProject,
);

// Bg and Profile Images Effect
useEffect(() => {
setProfileImage(profileData.profileImages.image);
setBackgroundImage(profileData.profileImages.backgroundImage);
}, [profileData.profileImages]);
if (!backgroundImage) {
dispatch.createProject.setProfileImage(profileData.profileImages.image);
dispatch.createProject.setBackgroundImage(
profileData.profileImages.backgroundImage,
);
}
}, [profileData.profileImages, backgroundImage]);

if (!accountId) {
return "";
}

const onBgImageChange = (files: File[]) => {
const onBgImageChange = async (files: File[]) => {
if (files) {
bgImageStatus.setStatus("loading");
uploadFileToIPFS(files[0], async (res) => {
bgImageStatus.setStatus("ready");
if (res.ok) {
const data = await res.json();
setBackgroundImage(`${IPFS_NEAR_SOCIAL_URL}${data.cid}`);
}
});
await dispatch.createProject.uploadBackgroundImage(files);
bgImageStatus.setStatus("ready");
}
};

const onAvatarImageChange = (files: File[]) => {
const onAvatarImageChange = async (files: File[]) => {
if (files) {
profileImageStatus.setStatus("loading");
uploadFileToIPFS(files[0], async (res) => {
profileImageStatus.setStatus("ready");
if (res.ok) {
const data = await res.json();
setProfileImage(`${IPFS_NEAR_SOCIAL_URL}${data.cid}`);
}
});
await dispatch.createProject.uploadProfileImage(files);
profileImageStatus.setStatus("ready");
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/modules/create-project/models/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export const createProjectSchema = z.object({
isDao: z.boolean().default(false),
daoAddress: z.string().min(3, "Invalid NEAR account ID"),
projectName: z.string().min(3, "Name must be at least 3 characters"),
backgroundImage: z.string(),
profileImage: z.string(),
});
77 changes: 77 additions & 0 deletions src/modules/create-project/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { createModel } from "@rematch/core";

import { RootModel } from "@/app/_store/models";
import { IPFS_NEAR_SOCIAL_URL } from "@/common/constants";

import { CreateProjectInputs } from "./models/types";
import uploadFileToIPFS from "../core/services/uploadFileToIPFS";

type CreateProjectState = CreateProjectInputs;

/**
* Create Project State
*/
const initialState: CreateProjectState = {
isDao: false,
daoAddress: "",
projectName: "",
backgroundImage: "",
profileImage: "",
};

export const createProject = createModel<RootModel>()({
state: initialState,

reducers: {
UPDATE_BACKGROUND_IMAGE(state: CreateProjectState, backgroundUrl: string) {
state.backgroundImage = backgroundUrl;
},

UPDATE_PROFILE_IMAGE(state: CreateProjectState, profileImageUrl: string) {
state.profileImage = profileImageUrl;
},

// Rese t to the initial state
RESET() {
return initialState;
},
},

effects: (dispatch) => ({
uploadBackgroundImage(files: File[]) {
return new Promise((resolve) => {
uploadFileToIPFS(files[0], async (res) => {
if (res.ok) {
const data = await res.json();
dispatch.createProject.UPDATE_BACKGROUND_IMAGE(
`${IPFS_NEAR_SOCIAL_URL}${data.cid}`,
);
resolve(`${IPFS_NEAR_SOCIAL_URL}${data.cid}`);
}
});
});
},

setBackgroundImage(backgroundUrl: string) {
dispatch.createProject.UPDATE_BACKGROUND_IMAGE(backgroundUrl);
},

async uploadProfileImage(files: File[]) {
return new Promise((resolve) => {
uploadFileToIPFS(files[0], async (res) => {
if (res.ok) {
const data = await res.json();
dispatch.createProject.UPDATE_PROFILE_IMAGE(
`${IPFS_NEAR_SOCIAL_URL}${data.cid}`,
);
resolve(`${IPFS_NEAR_SOCIAL_URL}${data.cid}`);
}
});
});
},

setProfileImage(profileImageUrl: string) {
dispatch.createProject.UPDATE_PROFILE_IMAGE(profileImageUrl);
},
}),
});

0 comments on commit 914c3dd

Please sign in to comment.