-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7176fb
commit 6ac952f
Showing
7 changed files
with
121 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,38 @@ | ||
"use server" | ||
"use server"; | ||
|
||
import connectToDB from "../mongoose" | ||
import { revalidatePath } from "next/cache"; | ||
import User from "../models/user.model"; | ||
import connectToDB from "../mongoose"; | ||
|
||
export async function updateUser( | ||
userId: string, | ||
username: string , | ||
name: string, | ||
bio: string, | ||
image: string, | ||
path: string | ||
): Promise<void> { | ||
connectToDB(); | ||
|
||
export async function updateUser(): Promise<void> { | ||
connectToDB(); | ||
try { | ||
await User.findOneAndUpdate( | ||
{ id: userId }, | ||
{ | ||
username: username.toLowerCase(), | ||
name, | ||
bio, | ||
image, | ||
onboarded: true, | ||
}, | ||
// upsert => update if exist , create if not exist | ||
{ upsert: true } | ||
); | ||
|
||
} | ||
// update cached data at profile/edit path (without waiting revalidation period) | ||
if (path === "profile/edit") { | ||
revalidatePath(path); | ||
} | ||
} catch (error: any) { | ||
throw new Error(error.message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import mongoose from "mongoose"; | ||
|
||
|
||
const userSchema = new mongoose.Schema({ | ||
id: { type:String , require: true}, | ||
username : { type:String , require: true}, | ||
name : { type:String }, | ||
bio : { type:String }, | ||
image : { type:String }, | ||
posts: [{ type: mongoose.Schema.Types.ObjectId, ref: "Post" }], | ||
onboarded : { type:Boolean , default: false} | ||
|
||
}); | ||
|
||
// from database || create new model | ||
const User = mongoose.models.User || mongoose.model("User" , userSchema); | ||
export default User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as z from "zod"; | ||
|
||
export const UserValidation = z.object({ | ||
image: z.string().url().nonempty(), | ||
name: z | ||
.string() | ||
.min(3, { message: "Minimum 3 characters." }) | ||
.max(30, { message: "Maximum 30 caracters." }), | ||
username: z | ||
.string() | ||
.min(3, { message: "Minimum 3 characters." }) | ||
.max(30, { message: "Maximum 30 caracters." }), | ||
bio: z | ||
.string() | ||
.min(3, { message: "Minimum 3 characters." }) | ||
.max(1000, { message: "Maximum 1000 caracters." }), | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters