Skip to content

Commit

Permalink
Merge pull request #652 from stakwork/fix/update_profile
Browse files Browse the repository at this point in the history
PR: Refactored update profile
  • Loading branch information
elraphty authored Nov 18, 2024
2 parents da67575 + 13d8e3b commit 5eb068f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/people/main/AboutFocusView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const AboutFocusView = (props: FocusViewProps) => {

if (config && config.name === 'about') {
const requestData = formatAboutBody(newBody);
await main.saveProfile(requestData);
await main.updateProfile(requestData);
if (shouldCloseModal) {
closeModal();
}
Expand Down
103 changes: 76 additions & 27 deletions src/store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,39 +1464,68 @@ export class MainStore {

if (body.price_to_meet) body.price_to_meet = parseInt(body.price_to_meet); // must be an int
try {
if (this.lnToken) {
const r = await this.saveBountyPerson(body);
if (!r) return;
// first time profile makers will need this on first login
if (r.status === 200) {
const p = await r.json();
const updateSelf = { ...info, ...p };
uiStore.setMeInfo(updateSelf);
}
} else {
const [r, error] = await this.doCallToRelay('POST', 'profile', body);
if (error) throw error;
if (!r) return;
if (!r.ok) {
throw new Error('Update failed. Please try again.');
}

// first time profile makers will need this on first login
if (!body.id) {
const j = await r.json();
if (j.response && j.response.id) {
body.id = j.response.id;
const r = await this.saveBountyPerson(body);
if (!r) {
uiStore.setToasts([
{
id: '1',
title: 'Profile saving failed'
}
]);
return;
}
// first time profile makers will need this on first login
if (r.status === 200) {
const p = await r.json();
const updateSelf = { ...info, ...p };
uiStore.setMeInfo(updateSelf);
this.getSelf(updateSelf);

uiStore.setToasts([
{
id: '2',
title: 'Saved.'
}
]);
}
} catch (e) {
uiStore.setToasts([
{
id: '1',
title: 'Failed to save profile'
}
]);
console.log('Error saveProfile: ', e);
}
}

// save to tribes
await this.saveBountyPerson(body);
const updateSelf = { ...info, ...body };
await this.getSelf(updateSelf);
async updateProfile(body: any) {
if (!uiStore.meInfo) return null;
const info = uiStore.meInfo;
if (!body) return; // avoid saving bad state

if (body.price_to_meet) body.price_to_meet = parseInt(body.price_to_meet); // must be an int
try {
const r = await this.updateBountyPerson(body);
if (!r) {
uiStore.setToasts([
{
id: '1',
title: 'Profile update failed'
}
]);
return;
}
// first time profile makers will need this on first login
if (r.status === 200) {
const p = await r.json();
const updateSelf = { ...info, ...p };
uiStore.setMeInfo(updateSelf);
this.getSelf(updateSelf);

uiStore.setToasts([
{
id: '2',
title: 'Saved.'
}
]);
Expand All @@ -1508,7 +1537,7 @@ export class MainStore {
title: 'Failed to update profile'
}
]);
console.log('Error saveProfile: ', e);
console.log('Error updateProfile: ', e);
}
}

Expand All @@ -1532,6 +1561,26 @@ export class MainStore {
return r;
}

async updateBountyPerson(body: any): Promise<Response | undefined> {
if (!uiStore.meInfo) return undefined;
const info = uiStore.meInfo;
if (!body) return; // avoid saving bad state

const r = await fetch(`${TribesURL}/person`, {
method: 'PUT',
body: JSON.stringify({
...body
}),
mode: 'cors',
headers: {
'x-jwt': info.tribe_jwt,
'Content-Type': 'application/json'
}
});

return r;
}

async saveBounty(body: any): Promise<void> {
const info = uiStore.meInfo as any;
if (!info && !body) {
Expand Down

0 comments on commit 5eb068f

Please sign in to comment.