NuxtN
Nuxt12mo ago
FearArt

State Management

Can i somehow shorten this code?
const TVstate = () => {
  if (openedRoom.value.hasTV === 'unknown') {
      openedRoom.value.hasTV = 'Yes'
      imageLibrary.value.TVImage = 'img/pngs/tv-green.png'
  }
  else if (openedRoom.value.hasTV === 'Yes') {
      openedRoom.value.hasTV = 'No'
      imageLibrary.value.TVImage = 'img/pngs/tv-red.png'
  }
  else {
      openedRoom.value.hasTV = 'unknown'
      imageLibrary.value.TVImage = 'img/pngs/tv-gray.png'
  }
}
const AcessPointstate = () => {
  if (openedRoom.value.hasAccessPoint === 'unknown') {
      openedRoom.value.hasAccessPoint = 'Yes'
      imageLibrary.value.AccessPointImage = 'img/pngs/wifi-green.png'
  }
  else if (openedRoom.value.hasAccessPoint === 'Yes') {
      openedRoom.value.hasAccessPoint = 'No'
      imageLibrary.value.AccessPointImage = 'img/pngs/wifi-red.png'
  }
  else {
      openedRoom.value.hasAccessPoint = 'unknown'
      imageLibrary.value.AccessPointImage = 'img/pngs/wifi-gray.png'
  }
}
const Phonestate = () => {
  if (openedRoom.value.hasPhone === 'unknown') {
      openedRoom.value.hasPhone = 'Yes'
      imageLibrary.value.PhoneImage = 'img/pngs/phone-green.png'
  }
  else if (openedRoom.value.hasPhone === 'Yes') {
      openedRoom.value.hasPhone = 'No'
      imageLibrary.value.PhoneImage = 'img/pngs/phone-red.png'
  }
  else {
      openedRoom.value.hasPhone = 'unknown'
      imageLibrary.value.PhoneImage = 'img/pngs/phone-gray.png'
  }
}
const BathPhonestate = () => {
  if (openedRoom.value.hasBathPhone === 'unknown') {
      openedRoom.value.hasBathPhone = 'Yes'
      imageLibrary.value.BathPhoneImage = 'img/pngs/bath-green.png'
  }
  else if (openedRoom.value.hasBathPhone === 'Yes') {
      openedRoom.value.hasBathPhone = 'No'
      imageLibrary.value.BathPhoneImage = 'img/pngs/bath-red.png'
  }
  else {
      openedRoom.value.hasBathPhone = 'unknown'
      imageLibrary.value.BathPhoneImage = 'img/pngs/bath-gray.png'
  }
}
Was this page helpful?