Skip to content

Commit

Permalink
Merge pull request #14 from spknetwork/feat/user-points
Browse files Browse the repository at this point in the history
Feat/user points
  • Loading branch information
igormuba authored Dec 26, 2023
2 parents 69571e1 + 3e1b1d6 commit adc9168
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 69 deletions.
79 changes: 78 additions & 1 deletion src/common/api/breakaway.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import axios from "axios"
import axios, { AxiosResponse } from "axios"
import * as ls from "../util/local-storage";

// const baUrl = "http://localhost:4000"
const baUrl = "https://breakaway-points-system-api.onrender.com"
const accessToken = ls.get("ba_access_token")

export const createBreakawayUser = async (username: string, community: string, referral: string, email: string)=> {
try {
Expand Down Expand Up @@ -36,3 +38,78 @@ export const createSolanaUser = async (email: string, password: string, solanaWa
return { err }
}
};

export const processLogin = async (username: string, ts: string, sig: string, community: string) => {
try {
const response: any = await axios.get(`${baUrl}/auth/login`, {
params: { username, ts, sig, community },
});

const { token, ...user } = response.data.response;

console.log('Login Successful', response);
return response;

} catch (error) {
console.error('Login Failed: ', error);
}
};

export const claimBaPoints = async (username: string, community: string) => {
try {
const response = await axios.post(`${baUrl}/points/claim`, {
username: username,
community: community,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
console.log(response)

return response;
} catch (error) {
console.error('Error claiming points:', error);
throw error;
}
};

export const getBaUserPoints = async (username: string, community: string): Promise<any[] | undefined> => {

try {
const response: AxiosResponse | any = await axios.get(`${baUrl}/points?username=${username}&community=${community}`);

return response;
} catch (error) {
console.error('Error fetching user points:', error);
throw error;
}
};

export const updateUserPoints = async (username: string, community: string, pointType: string) => {
console.log(username)
try {
const requestData = {
username,
community,
pointType,
};

const response = await axios.post(`${baUrl}/points`,
requestData,
{
headers: {
Authorization: `Bearer ${accessToken}` ,
},
}
);

console.log(response.data);
return response;
} catch (error) {
console.log('Error updating user points:', error);
throw error;
}
};
21 changes: 17 additions & 4 deletions src/common/components/comment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ setProxyBase(defaults.imageServer);
import {_t} from "../../i18n";
import {Global} from '../../store/global/types';
import * as ls from "../../util/local-storage";
import { updateUserPoints } from "../../api/breakaway";

interface PreviewProps {
text: string;
Expand Down Expand Up @@ -127,10 +128,17 @@ export class Comment extends Component<Props, State> {
}, 500);
};

submit = () => {
submit = async () => {
const {text} = this.state;
const {onSubmit} = this.props;
onSubmit(text);
const {onSubmit, activeUser} = this.props;
try {
onSubmit(text);
const res = await updateUserPoints(activeUser!.username, "Hive Rally", "comments")
console.log("commented")
console.log(res);
} catch (error) {

}
}

cancel = () => {
Expand Down Expand Up @@ -164,7 +172,12 @@ export class Comment extends Component<Props, State> {
)}
{LoginRequired({
...this.props,
children: <Button className="btn-submit" size="sm" disabled={inProgress} onClick={this.submit}>
children: <Button
className="btn-submit"
size="sm"
disabled={inProgress}
onClick={this.submit}
>
{inProgress && (<Spinner animation="grow" variant="light" size="sm" style={{marginRight: "6px"}}/>)} {submitText}
</Button>
})}
Expand Down
6 changes: 5 additions & 1 deletion src/common/components/entry-reblog-btn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {_t} from "../../i18n";
import _c from "../../util/fix-class-names";

import {repeatSvg} from "../../img/svg";
import { updateUserPoints } from "../../api/breakaway";

interface Props {
entry: Entry;
Expand Down Expand Up @@ -67,8 +68,11 @@ export class EntryReblogBtn extends BaseComponent<Props> {

this.stateSet({inProgress: true});
reblog(activeUser?.username!, entry.author, entry.permlink)
.then(() => {
.then(async () => {
addReblog(entry.author, entry.permlink);
const baResponse = await updateUserPoints(activeUser!.username, "Hive Rally", "reblog")
console.log("Reblogged")
console.log(baResponse);
success(_t("entry-reblog.success"));
})
.catch((e) => {
Expand Down
7 changes: 6 additions & 1 deletion src/common/components/entry-vote-btn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import _c from "../../util/fix-class-names";
import { chevronDownSvgForSlider, chevronUpSvgForSlider, chevronUpSvgForVote } from "../../img/svg";
import ClickAwayListener from "../clickaway-listener";
import { _t } from "../../i18n";
import { updateUserPoints } from "../../api/breakaway";

const setVoteValue = (type: "up" | "down" | "downPrevious" | "upPrevious", username: string, value: number) => {
ls.set(`vote-value-${type}-${username}`, value);
Expand Down Expand Up @@ -381,14 +382,18 @@ export class EntryVoteBtn extends BaseComponent<Props, State> {
const username = activeUser?.username!;

vote(username, entry.author, entry.permlink, weight)
.then(() => {
.then(async () => {
const votes: EntryVote[] = [
...entry.active_votes.filter((x) => x.voter !== username),
{ rshares: weight, voter: username },
];

afterVote(votes, estimated);
updateActiveUser(); // refresh voting power

const baResponse = await updateUserPoints(activeUser!.username, "Hive Rally", "upvote")
console.log("Voted")
console.log(baResponse);
})
.catch((e) => {
error(formatError(e));
Expand Down
31 changes: 28 additions & 3 deletions src/common/components/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import _c from "../../util/fix-class-names";

import {deleteForeverSvg} from "../../img/svg";
import { setupConfig } from "../../../setup";
import { processLogin } from "../../api/breakaway";

declare var window: AppWindow;

Expand Down Expand Up @@ -129,9 +130,20 @@ export class LoginKc extends BaseComponent<LoginKcProps, LoginKcState> {

this.stateSet({ inProgress: true });

const signer = (message: string): Promise<string> =>
signBuffer(username, message, "Active").then((r) => r.result);

const signer = async (message: string): Promise<string> => {
console.log("message", JSON.parse(message))
const ts: any = Date.now();
const sign = await signBuffer(username, message, "Active").then((r) => r.result);
const signBa = await signBuffer(username, `${username}${ts}`, "Active").then((r) => r.result);
console.log("signBa", signBa)
if (sign) {
const login = await processLogin(username, ts, signBa, "Hive Rally");
const baToken = login?.data?.response?.token
ls.set("ba_access_token", baToken)
console.log(username, login?.data?.response?.token)
}
return sign;
}
let code: string;
try {
code = await makeHsCode(username, signer);
Expand Down Expand Up @@ -320,6 +332,18 @@ export class Login extends BaseComponent<LoginProps, State> {
toggleUIProp("login");
};

signer = async (username: string): Promise<string> => {
const ts: any = Date.now();
const signBa = await signBuffer(username, `${username}${ts}`, "Active").then((r) => r.result);
console.log("signBa", signBa)
if (signBa) {
const login = await processLogin(username, ts, signBa, "Hive Rally");
const baToken = login?.data?.response?.token
ls.set("ba_access_token", baToken)
}
return signBa;
}

userSelect = (user: User) => {
const { doLogin } = this.props;

Expand All @@ -331,6 +355,7 @@ export class Login extends BaseComponent<LoginProps, State> {
if (!token) {
error(`${_t("login.error-user-not-found-cache")}`);
}
this.signer(user.username)
return token
? doLogin(token, user.postingKey, account)
: this.userDelete(user);
Expand Down
2 changes: 2 additions & 0 deletions src/common/components/user-nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import HiveWallet from "../../helper/hive-wallet";
import {creditCardSvg, gifCardSvg, bellSvg, bellOffSvg, chevronUpSvg} from "../../img/svg";

import {votingPower, downVotingPower} from "../../api/hive";
import * as ls from "../../util/local-storage";

class WalletBadge extends Component<{
activeUser: ActiveUser;
Expand Down Expand Up @@ -205,6 +206,7 @@ export default class UserNav extends Component<Props, State> {
onClick: () => {
const {setActiveUser} = this.props;
setActiveUser(null);
ls.remove("ba_access_token")
},
}
];
Expand Down
9 changes: 9 additions & 0 deletions src/common/components/wallet-ecency/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,12 @@
}
}
}

.login-points{
display: flex;
flex-direction: column;
gap: 20px;
justify-content: center;
align-items: center;
width: 100%;
}
Loading

0 comments on commit adc9168

Please sign in to comment.