diff --git a/src/App.jsx b/src/App.jsx
index 1f766f1..2744b03 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -44,6 +44,7 @@ export default function App() {
// Estado para criação de Looks
const [selectedForLook, setSelectedForLook] = useState([]);
+ const [editingLook, setEditingLook] = useState(null);
// Perfil do Utilizador
const [userProfile, setUserProfile] = useState({});
@@ -295,19 +296,27 @@ export default function App() {
}
};
- const createLook = async (e) => {
+ const saveLook = async (e) => {
e.preventDefault();
if (selectedForLook.length < 2) return;
setLoading(true);
const fd = new FormData(e.target);
+ const lookData = {
+ name: fd.get('lookName'),
+ items: selectedForLook,
+ updatedAt: new Date().getTime()
+ };
try {
- const looksCol = collection(db, 'artifacts', appId, 'users', user.uid, 'looks');
- await addDoc(looksCol, {
- name: fd.get('lookName'),
- items: selectedForLook,
- createdAt: new Date().getTime()
- });
+ if (editingLook) {
+ const docRef = doc(db, 'artifacts', appId, 'users', user.uid, 'looks', editingLook.id);
+ await updateDoc(docRef, lookData);
+ } else {
+ lookData.createdAt = new Date().getTime();
+ const looksCol = collection(db, 'artifacts', appId, 'users', user.uid, 'looks');
+ await addDoc(looksCol, lookData);
+ }
setSelectedForLook([]);
+ setEditingLook(null);
setView('outfits');
} catch (e) { console.error(e); }
finally { setLoading(false); }
@@ -370,6 +379,49 @@ export default function App() {
finally { setLoading(false); }
};
+ const handleProfileImageUpload = (e) => {
+ const file = e.target.files[0];
+ if (!file || !user) return;
+
+ const reader = new FileReader();
+ reader.onload = (event) => {
+ const img = new Image();
+ img.onload = async () => {
+ const canvas = document.createElement('canvas');
+ const MAX_SIZE = 400;
+ let width = img.width;
+ let height = img.height;
+
+ if (width > height) {
+ if (width > MAX_SIZE) {
+ height *= MAX_SIZE / width;
+ width = MAX_SIZE;
+ }
+ } else {
+ if (height > MAX_SIZE) {
+ width *= MAX_SIZE / height;
+ height = MAX_SIZE;
+ }
+ }
+
+ canvas.width = width;
+ canvas.height = height;
+ const ctx = canvas.getContext('2d');
+ ctx.drawImage(img, 0, 0, width, height);
+ const base64Data = canvas.toDataURL('image/jpeg', 0.8);
+
+ try {
+ const profileDoc = doc(db, 'artifacts', appId, 'users', user.uid, 'profile', 'data');
+ await setDoc(profileDoc, { avatar: base64Data }, { merge: true });
+ } catch (err) {
+ console.error("Error uploading image:", err);
+ }
+ };
+ img.src = event.target.result;
+ };
+ reader.readAsDataURL(file);
+ };
+
const saveProfile = async (e) => {
e.preventDefault();
setSavingProfile(true);
@@ -478,8 +530,12 @@ export default function App() {