The Vue.js feature that should alter the editing status of a block is malfunctioning

I've been working on creating a sleek note-taking app and currently, I am focusing on the functionality of the NoteItem component. This component consists of two buttons: one for editing a note and another for deleting it. The delete button is working perfectly fine, but I'm facing an issue with the edit button. When it triggers the startRedacting() function, the redacting variable remains unchanged, which means that the v-else block is not being rendered.

NoteItem.vue:

<script setup lang="ts">
import axios from "axios";
const props = defineProps(['title', 'content', 'note_id']);

let redacting = undefined;

let redTitle = props.title;
let redContent = props.content;

const deleteNote = async () => {
  await axios.delete(`http://localhost:3000/notes/${props.note_id}`)
      .then((res) => {
        if (res.status === 204) {
          location.reload()
        }
        else {
          console.log(`HTTP STATUS ${res.status}`)
        }
      })
}

const startRedacting = () => {
  redacting = true
}

const redactNote = async () => {
  await axios.put(`http://localhost:3000/notes/${props.note_id}`, {
    Title: redTitle,
    Content: redContent
  })
      .then((res) => {
        if (res.status === 200) {
          redacting = false
          location.reload()
        }
        else {
          console.log(`HTTP STATUS ${res.status}`)
        }
      })
}
</script>

<template>
<div class="flex flex-row justify-between border-2 border-r-6 border-gray-200 p-3 rounded-md">
  <div class="flex w-2.5 h-full bg-red-500 rounded-3xl resize-none flex-none" id="strip">
    <!--Poloska-->
  </div>
  <div v-if="!redacting" class="flex flex-col ml-8 text-wrap justify-self-start justify-start">
    <div id="title" class="text-xl justify-self-center text-left">
      {{ title }}
    </div>
    <div id="content" class="pt-4 text-pretty h-max truncate justify-self-start text-left">
      {{ content }}
    </div>
  </div>
  <div v-else>
    <div class="flex flex-col ml-8 text-wrap justify-self-start justify-start pr-2">
      <div class="text-xl justify-self-center text-left">
        <input type="text" v-model="redTitle" class="border-2 border-gray-200 rounded-md p-2 w-full">
      </div>
      <div id="content" class="pt-4 text-pretty h-max truncate justify-self-start text-left">
        <textarea v-model="redContent" class=" border-2 border-gray-200 rounded-md p-2 w-full"></textarea>
      </div>
      <button class="size-8  hover:shadow-neutral-300 hover:bg-green-700 rounded-lg justify-center w-full bg-green-600" @click="redactNote">
        <div class="text-white">Done</div>
      </button>
    </div>
  </div>
  <div class="flex flex-col">
    <button class="size-8 hover:shadow-neutral-300 hover:bg-neutral-200 rounded-lg" @click="startRedacting">
      <img src="../assets/pencil.svg" alt="" class="justify-self-center">
    </button>
    <button class="size-8 hover:shadow-neutral-300 hover:bg-red-400 rounded-lg mt-2" @click="deleteNote">
      <img src="../assets/trash-svgrepo-com.svg" alt="" class="pt-1">
    </button>
  </div>
</div>
</template>

<style scoped>
</style>

Answer №1

It appears that your state is not reactive as it has not been declared with either the ref or reactive helpers. To make your state variable reactive, define it using ref and then mutate it using .value:

<script setup lang="ts">
import { ref } from 'vue'

//....

let redacting = ref();

//...

const deleteNote = async () => {
  await axios.delete(`http://localhost:3000/notes/${props.note_id}`)
      .then((res) => {
        if (res.status === 204) {
          location.reload()
        }
        else {
          console.log(`HTTP STATUS ${res.status}`)
        }
      })
}

const startRedacting = () => {
  redacting.value = true
}

const redactNote = async () => {
  await axios.put(`http://localhost:3000/notes/${props.note_id}`, {
    Title: redTitle,
    Content: redContent
  })
      .then((res) => {
        if (res.status === 200) {
          redacting.value  = false
          location.reload()
        }
        else {
          console.log(`HTTP STATUS ${res.status}`)
        }
      })
}
</script>

It is unnecessary to use .value in the template:

  <div v-if="!redacting" class="flex flex-col ml-8 text-wrap justify-self-start justify-start">
    <div id="title" class="text-xl justify-self-center text-left">
      {{ title }}
    </div>
    <div id="content" class="pt-4 text-pretty h-max truncate justify-self-start text-left">
      {{ content }}
    </div>
  </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining numerical values with similar string values in a multi-dimensional array to calculate the sum

I'm currently creating a JavaScript cash register application that calculates the exact change based on the available funds in the register. This project is part of a challenge on freeCodeCamp: https://www.freecodecamp.org/challenges/exact-change Alt ...

Building unique queries using TypeScript and React testing-library

I am currently working on creating a custom query specifically for Testing Library in TypeScript to be used with a React project. The custom query is designed to retrieve the first th cells within the tbody of the container: // all-table-headings.ts - cust ...

Python/Selenium Issue: JS/AJAX Content Fails to Load when URL is Accessed

Currently, I am attempting to gather data from the following URL: The data that I am looking to extract is loaded dynamically, such as the Center Bore information. When accessing the link through a standard web browser, the content loads without any issue ...

Tips for implementing ng-click to work with the function result in href

Is there a way to pass the result of the ng-click function into the href attribute? Let's say I have: <a href="{{value}}" target="_blank" ng-click="myFunction()">Click Here</a> Where myFunction() dynamically generates a unique link from ...

Using Vue js to Specify the Type of a Prop

I am currently working on a Vue.js component that requires a prop named idFieldType The goal is to only allow this prop to accept values of type Number or String To achieve this, I implemented the following code: idFieldType: { Type: Function, d ...

What is the proper way to include jQuery script in HTML document?

I am facing an issue with the banners on my website. When viewed on mobile devices, the SWF banner does not show up. In this situation, I want to display an <img> tag instead, but the jQuery code is not functioning correctly. My template structure l ...

Continue moving the division to the left

Below is the HTML structure that I am working with: <div class="container"> <div class="menu"></div> </div> Accompanied by jQuery: $(document).ready(function() { $(".container").click(function() { $(".menu").css("l ...

Exploring Interactive Designs with Vue.js

In order to dynamically construct a series of CSS style classes based on the toolName property in the JSON data using Vue 2, I tried to use a computed property to bind them to the existing span with a class of panel-icon. However, when attempting to save t ...

Leveraging the 'require' method in Node.js for linking with external JavaScript files

Recently, I've been experimenting with using the require function in nodejs to access JavaScript files containing simple scripts. My objective is to require the script and then output its return value to the console. Here's an example of what I c ...

Guidelines for passing input values from a custom directive using $resource in Angular

I have been researching how to use ngModel within a custom directive, and while I grasp the concept, I am struggling with implementing it when using $resource. Currently, I am successfully injecting the "file" scope into my directive and making the API ca ...

"Implementing a call and waiting at intervals by utilizing the subscribe function in Angular 6

In my code, I have a method that is called every 10000 times. Now, I want to modify this so that the function getAllNotificationsActed0() is invoked every 10 seconds. If the data does not arrive within this interval, I do not want the function to be called ...

Optimizing performance with ng-if for 500 watchers

When repeating data with ng repeat, I encountered an issue where some of the data.image (src) values were null and I did not want them to be included in the repeat process. To solve this issue, I implemented a simple ng-if statement. <div ng-repeat="d ...

Having trouble testing firebase messaging on my local server

I've encountered an issue while trying to retrieve a token from firebase/messaging for use in notifications. The error message "messaging/unsupported-browser" (FirebaseError: Messaging: This browser doesn't support the API's required to use ...

What is the best way to invoke a JavaScript function that utilizes the jQuery library?

Recently I came across this interesting function: function squarifyMe(element) { squareItUp() window.onresize = function(element) { squareItUp(); } function squareItUp() { $(element).height($(element).width()); } } and its usage is as ...

JavaScript first, middle, and last names

When working with Javascript, I have encountered a challenge. I am attempting to extract the First, Middle, and Last names from a full name input into three separate fields - Character Length, Middle Name, and Initials. At this point, I have successfull ...

Is this Firebase regulation accurate and suitable for PUT and GET requests in an Angular and Firebase environment?

Creating a system where users can only see their own posts and no one else can access them is my main goal. Authentication along with posting functionality is already in place and working, but I want to implement this without using Firebase restrictions. ...

What are the reasons that execCommand does not function correctly when attempting to justify text?

Why isn't justify with execCommand working properly? Take a look at the code below: $('#JustifyLeft').click(function(){ document.execCommand("JustifyLeft", false, ""); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2 ...

TypeScript Dilemma: Redundant Function Declaration and Block-Scoped Variable Cannot be Redefined

Trying to get the hang of 'TypeScript in 5 Minutes' but running into some issues: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html Despite my efforts, I keep encountering one error after another, which is not ideal when ...

How can you achieve a seamless or infinite scrolling effect on a webpage?

My idea is as follows: Imagine a long web page where, instead of the scrolling coming to an abrupt stop when the user reaches the end, I want the page to reload from the bottom and allow the scrolling to continue seamlessly. Specifics Picture this - as ...

Connecting Node.js and Express with MySQL database

Today is my first time working with Node (Express) js, and I'm attempting to connect to a MySQL database. Here is the code snippet I found for my app.js file. app.js var express = require('express'), mysql = require('mysql'); // ...