@@ -35,80 +35,11 @@ export async function addPostToDb(post: AddedPostToApi) {
3535 }
3636}
3737
38- export async function editPostOnDb ( ) {
39- console . log ( "executou editPostOnDb" )
40- }
41-
42- const usernameSchema = z . string ( )
43-
44- export async function setUsernameAction ( username : unknown ) {
45- try {
46- const validatedUsername = usernameSchema . safeParse ( username )
47- if ( ! validatedUsername . success ) {
48- return { error : true , errorMessage : "Invalid username." }
49- }
50-
51- return { username : validatedUsername . data }
52- } catch {
53- return {
54- error : true ,
55- errorMessage : "Could not set username. Please, try again in a few minutes."
56- }
57- }
58- }
59-
60- // rename this file
61- // move this file
62- /*
63- "use server"
64-
65- import { z } from "zod"
66- import { revalidatePath } from "next/cache"
67- import { postFormSchema } from "@/lib/types"
68- import { baseUrl, delay } from "@/lib/utils"
69-
70- const addedPostToApiSchema = postFormSchema.extend({ username: z.string() })
71-
72- export async function addPostAction(newPost: unknown) {
73- const failMessage = { message: "Could not add post. Please, try again in a few minutes." }
74- try {
75- const validatedNewPost = addedPostToApiSchema.safeParse(newPost)
76- if (!validatedNewPost.success) {
77- return { message: "Invalid post data." }
78- }
79-
80- const response = await fetch(baseUrl, {
81- method: "POST",
82- headers: { "content-type": "application/json" },
83- body: JSON.stringify(validatedNewPost.data)
84- })
85-
86- if (!response.ok) {
87- return failMessage
88- }
89-
90- const data = await response.json()
91- console.log("response:", response)
92- console.log("data:", data)
93- // at the time of this writing, a post object that wasn't created on db don't have these properties
94- const postWasNotReallyCreatedOnDb = !data.id || !data.created_datetime
95- if (postWasNotReallyCreatedOnDb) {
96- console.log("postWasNotReallyCreatedOnDb:", postWasNotReallyCreatedOnDb)
97- return failMessage
98- }
99- } catch {
100- return failMessage
101- }
102-
103- revalidatePath("/feed", "layout")
104- }
105-
10638const postIdSchema = z . number ( )
10739
108- export async function editPostAction (editedData: unknown, postId: unknown) {
109- const failMessage = { message: "Could not edit post. Please, try again in a few minutes." }
40+ export async function editPostOnDb ( editedData : unknown , postId : unknown ) {
41+ const failMessage = "Could not edit post. Please, try again in a few minutes."
11042 try {
111- await delay(1000)
11243 const validatedEditedData = postFormSchema . safeParse ( editedData )
11344 const validatedPostId = postIdSchema . safeParse ( postId )
11445 if ( ! validatedEditedData . success || ! validatedPostId . success ) {
@@ -122,37 +53,35 @@ export async function editPostAction(editedData: unknown, postId: unknown) {
12253 } )
12354
12455 if ( ! response . ok ) {
125- return failMessage
56+ return { message : failMessage }
12657 }
58+
59+ const editedPostOnDb : unknown = await response . json ( )
60+ const validatedEditedPostOnDb = postSchema . safeParse ( editedPostOnDb )
61+ if ( ! validatedEditedPostOnDb . success ) {
62+ return { message : "Post was not edited on db" }
63+ }
64+
65+ return { editedPostOnDb : validatedEditedPostOnDb . data }
12766 } catch {
128- return failMessage
67+ return { message : failMessage }
12968 }
130-
131- revalidatePath("/feed", "layout")
13269}
13370
134- export async function deletePostAction(postId: unknown) {
135- const failMessage = { message: "Could not delete post. Please, try again in a few minutes." }
71+ const usernameSchema = z . string ( )
72+
73+ export async function setUsernameAction ( username : unknown ) {
13674 try {
137- await delay(1000)
138- const validatedPostId = postIdSchema.safeParse(postId)
139- if (!validatedPostId.success) {
140- return { message: "Invalid post id." }
75+ const validatedUsername = usernameSchema . safeParse ( username )
76+ if ( ! validatedUsername . success ) {
77+ return { error : true , errorMessage : "Invalid username." }
14178 }
14279
143- const response = await fetch(`${baseUrl}${validatedPostId.data}/`, {
144- method: "DELETE",
145- headers: { "content-type": "application/json" },
146- body: JSON.stringify({})
147- })
148-
149- if (!response.ok) {
150- return failMessage
151- }
80+ return { username : validatedUsername . data }
15281 } catch {
153- return failMessage
82+ return {
83+ error : true ,
84+ errorMessage : "Could not set username. Please, try again in a few minutes."
85+ }
15486 }
155-
156- revalidatePath("/feed", "layout")
15787}
158- */
0 commit comments