Skip to content

Commit

Permalink
refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
rvaidun committed Aug 20, 2023
1 parent cfa6ec0 commit 5841292
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 174 deletions.
19 changes: 16 additions & 3 deletions src/components/posts/uploadRealmoji.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export default {
return "success";
});
};
const postRealmoji = (d) => {
const postRealmoji = (d, postid, postuserid) => {
return fetch(
`${this.$store.state.proxyUrl}/https://mobile.bereal.com/api/content/realmojis/instant?postId=${this.postID}&postUserId=${this.postOwnerID}`,
`${this.$store.state.proxyUrl}/https://mobile.bereal.com/api/content/realmojis/instant?postId=${postid}&postUserId=${postuserid}`,
{
method: "PUT",
headers: {
Expand Down Expand Up @@ -119,7 +119,20 @@ export default {
uud = uploadUrlData;
})
.then(() => putPhoto(uud.data.url, file, uud.data.headers))
.then(() => postRealmoji(uud))
.then(() => {
if (this.postID) {
return postRealmoji(uud, this.postID, this.postOwnerID);
}
const promises = [];
this.$store.state.posts.forEach((post) => {
if (this.$store.state.user.id != post.user.id) {
post.posts.forEach((p) => {
promises.push(postRealmoji(uud, post.user.id, p.id));
});
}
});
return Promise.all(promises);
})
.then(() => {
resolve("Realmoji uploaded successfully!");
})
Expand Down
174 changes: 3 additions & 171 deletions src/components/ui/reactToAll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
<script>
import PopupModal from "../posts/PopupModal.vue";
import MyButton from "../ui/Button.vue";
import UploadRealmoji from "../posts/uploadRealmoji.vue";
export default {
name: "ReactToAll",
components: {
PopupModal,
MyButton,
UploadRealmoji,
},
data() {
return {
Expand All @@ -23,150 +25,6 @@ export default {
closePopup() {
this.showPopup = false;
},
onFileChange(e) {
console.log(e);
this.file = e.target.files[0];
this.imageurl = URL.createObjectURL(this.file);
},
async uploadRealmojis(file) {
const getUploadUrl = () => {
return fetch(
`${this.$store.state.proxyUrl}/https://mobile.bereal.com/api/content/realmojis/upload-url?mimeType=image/webp`,
{
headers: {
authorization: `Bearer ${localStorage.getItem("token")}`,
accept: "*/*",
"bereal-platform": "iOS",
"bereal-os-version": "14.7.1",
"accept-language": "en-US;q=1.0",
"user-agent":
"BeReal/0.28.2 (AlexisBarreyat.BeReal; build:8425; iOS 14.7.1) 1.0.0/BRApiKit",
"bereal-app-language": "en-US",
"bereal-timezone": "America/Los_Angeles",
"bereal-device-language": "en",
},
}
)
.then((res) => {
if (!res.ok) {
throw new Error("Failed to get upload url");
}
return res.json();
})
.then((data) => {
return data;
});
};
const putPhoto = (url, file, h) => {
return fetch(`${this.$store.state.proxyUrl}/${url}`, {
method: "PUT",
headers: h,
body: file,
}).then((res) => {
if (!res.ok) {
throw new Error("Failed to upload photo");
}
return "success";
});
};
const postRealmoji = (d, postOwnerID, postID) => {
return fetch(
`${this.$store.state.proxyUrl}/https://mobile.bereal.com/api/content/realmojis/instant?postId=${postID}&postUserId=${postOwnerID}`,
{
method: "PUT",
headers: {
"content-type": "application/json",
authorization: `Bearer ${localStorage.getItem("token")}`,
accept: "*/*",
"bereal-platform": "iOS",
"bereal-os-version": "14.7.1",
"accept-language": "en-US;q=1.0",
"bereal-app-language": "en-US",
"user-agent":
"BeReal/0.31.0 (AlexisBarreyat.BeReal; build:8586; iOS 14.7.1) 1.0.0/BRApiKit",
"bereal-timezone": "America/Los_Angeles",
"bereal-device-language": "en",
"bereal-app-version": "0.31.0-(8586)",
},
body: JSON.stringify({
media: {
path: d.data.path,
bucket: d.data.bucket,
width: 500,
height: 500,
},
}),
}
)
.then((res) => {
if (!res.ok) {
throw new Error("Failed to upload photo");
}
return res.json();
})
.then((data) => {
console.log(data);
return data;
});
};
return new Promise((resolve, reject) => {
let uud;
getUploadUrl()
.then((uploadUrlData) => {
console.log(uploadUrlData);
uud = uploadUrlData;
})
.then(() => putPhoto(uud.data.url, file, uud.data.headers))
.then(() => {
console.log("postsLoaded: ", this.postsLoaded);
const promises = [];
// for (const post in this.$store.state.posts) {
// if (this.$store.state.user.id != this.postsLoaded[post].ownerID) {
// promises.push(
// postRealmoji(
// uud,
// this.postsLoaded[post].ownerID,
// this.postsLoaded[post].id
// )
// );
// }
// }
this.$store.state.posts.forEach((post) => {
if (this.$store.state.user.id != post.user.id) {
post.posts.forEach((p) => {
promises.push(postRealmoji(uud, post.user.id, p.id));
});
}
});
return Promise.all(promises);
})
.then(() => {
resolve("Realmoji uploaded successfully!");
})
.catch((err) => {
reject(err);
});
});
},
submitRealMojis() {
if (this.file === undefined || this.file === null) {
this.$store.commit("error", "No image selected");
return;
}
this.loading = true;
this.uploadRealmojis(this.file)
.then(() => {
this.loading = false;
this.file = null;
this.imageurl = null;
this.$store.dispatch("getPosts");
})
.catch((e) => {
this.loading = false;
this.$store.commit("error", e);
});
},
},
};
</script>
Expand Down Expand Up @@ -201,33 +59,7 @@ export default {
<h3 class="font-bold">{{ popupTitle }}</h3>
<p class="pb-10">{{ popupContent }}</p>
</div>
<div class="flex items-center gap-3">
<label>
<div
class="border-white w-24 h-24 rounded-[50%] border-2 cursor-pointer">
<input
type="file"
style="display: none"
name="image"
@change="onFileChange" />
<div v-if="!file">
<img src="../../assets/add.svg" alt="plus" />
</div>
<div v-else class="cursor-pointer">
<img
:src="imageurl"
class="w-24 rounded-[50%]"
alt="realmoji to upload" />
</div>
</div>
</label>

<div>
<MyButton @clickedd="submitRealMojis" :loading="loading"
>Upload Realmojis</MyButton
>
</div>
</div>
<UploadRealmoji />
</template>
</PopupModal>
</template>

0 comments on commit 5841292

Please sign in to comment.