Wednesday, September 14, 2022

Could not get lock /var/lib/apt/lists/lock. It is held by process...

sudo apt update

[sudo] password for user: 

Reading package lists... Done

E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1420 (packagekitd)

N: Be aware that removing the lock file is not a solution and may break your system.

E: Unable to lock directory /var/lib/apt/lists/

ps aux | grep -i apt

sudo kill processid

sudo lsof /var/lib/dpkg/lock

sudo lsof /var/lib/apt/lists/lock

sudo lsof /var/cache/apt/archives/lock

Tuesday, September 13, 2022

React basic features and tricks

<></> // fragment div container which won't be included in HTML

<CardContainer></CardContainer>

{} // put JavaScript code inside {} in case of unreachable variable

 <React.StrictMode></React.StrictMode> => https://reactjs.org/docs/strict-mode.html

export default function App() {}

// when using function instead class => function App() {} => export default App()

import { function } from './shared/func-do-smth'; // add { } to import just function from component

export const funcName = (a, b, c, d) => { return ( <div></div> );};

// to convert string to number use + => +e.currentTarget.value

Each child in a list should have a unique "key" prop.

const items = [ <>{ el being created }</> ] // dustructure const [items] to get keys

https://stackoverflow.com/questions/32888728/correct-way-to-share-functions-between-components-in-react

https://www.makeuseof.com/must-follow-react-practices/

https://www.educative.io/blog/best-practices-react-developer#5

Using Functional Components and Hooks Instead of Classes

Avoid Using State (If Possible)

Opt for Fragments Instead of Divs Where Possible

Use Object Destructuring For Props

Extract reusable logic into custom hooks


Show more button in React

//

import Button from './shared/btn';
import { useState } from 'react';

export default function Func() {
// set show more state and onClick
const [showMore, setShowMore] = useState(false);
const moreClick = () => {
setShowMore(!showMore);
};
   const html = [];

return (
<>
{showMore ? html : !html}
      <Button
id={'show-more'}
className={'btn'}
value={'Show more'}
onClick={moreClick}
/>{
</>
);
}

     <button

        className='btn'

        onClick={() => setShowMore(!showMore)}

        id='showMore'

      >

        <b>Show More...</b>

      </button>

Sunday, September 11, 2022

Asynchronous JavaScript, AJAX, APIs

https://habr.com/ru/company/timeweb/blog/583588/

https://medium.com/codingthesmartway-com-blog/create-a-rest-api-with-json-server-36da8680136d

https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX

Synchronous code is executed line by line. Each line of code waits previous line finish.

Asynchronous code is executed after task that runs in the "background" finishes. Asynchronous code is non-blocking and do not wait for asynchronous task to finish its work => coordinates behavior of a program over a period of time.

Application programming interfaces allow applications to communicate with each other: DOM API, Geolocation API, Own Class API, free 3rd-party APIs... JSON - most popular API data format.

Saturday, September 10, 2022

Windows Ctrl+Alt+Del equivalent in Linux

Ctrl+Alt+Del in KDE Plasma => shutdown

Ctrl+Alt+F2 // 

login and killall appname

Ctrl+Alt+F1 // return to normal mode

Ctrl+Alt+Backspace => kill X (graphical interface)

sudo systemctl restart display-manager // restart desktop (graphical interface)

sudo /etc/init.d/gdm restart // restart X (graphical interface)

sudo shutdown -r now // reboot

sudo reboot

top // display processes

pstree // display processes tree

ps aux | grep proccessname

Ctrl+C // cancel === exit

Object-oriented programming in JavaScript

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming

Objects are self-contained blocks of code. Objects are building blocks of application and interact with one another. Interactions happen through application programming interface (API).

OOP paradigm is designed to organize and maintain code.

Class - blueprint for creating new objects - instances. Instance - object, created from class.

Class => (instantiation) => Instance

Abstraction - ignoring and hiding details that don't matter...

Encapsulation - keeping properties and methods private inside the class, so they aren't accessible outside. Some methods can be exposed as API.

Inheritance - all properties and methods of parent class available to child class.

Polymorphism - child class can overwrite method, inherited from parent class.

JavaScript Object => (prototypal inheritance) => Prototype.

JavaScript Object delegates methods to Prototype.

How to create prototypes? Constructor functions - poplar used technique, ES6 Classes - "syntactic sugar" using constructor functions, Object.create() - simple and straightforward way.

prototype basically is prototype of linked objects, NOT prototype of Person => Person.prototype

Person.prototype is prototype of all objects, created with Person constructor function.

const Person = function (firstName, birthYear) { // constructor function

  this.firstName = firstName;   // instance property

  this.birthYear = birthYear;  // instance property

  this.calcAge = function () {   // Never create methods in constructor functions!!!

   console.log(2022 - this.birthYear);

  };

};

const john = new Person('John', 1991);

console.log(john);

// 1. New {} is created

// 2. function is called, this = {}

// 3. {} linked to prototype => __proto__ for example console.log(john.__proto__);

// 4. function automatically return {}

Person.prototype.calcAge = function () { // prototypal inheritance

  console.log(2022 - this.birthYear);

};

john.calcAge();

isPrototypeOf , hasOwnProperty

Classes are: 1) not hoisted, 2) first-class citizens, 3) executed in strict mode.

Modal dialogue box or window with JavaScript and CSS

https://www.w3schools.com/howto/howto_css_modals.asp

'use strict';


const modal = document.querySelector('.modal');

const overlay = document.querySelector('.overlay');

const btnCloseModal = document.querySelector('.close-modal');

const btnsOpenModal = document.querySelectorAll('.show-modal');


const openModal = function () {

  modal.classList.remove('hidden');

  overlay.classList.remove('hidden');

};


const closeModal = function () {

  modal.classList.add('hidden');

  overlay.classList.add('hidden');

};


for (let i = 0; i < btnsOpenModal.length; i++)

  btnsOpenModal[i].addEventListener('click', openModal);


btnCloseModal.addEventListener('click', closeModal);

overlay.addEventListener('click', closeModal);


document.addEventListener('keydown', function (e) {


  if (e.key === 'Escape' && !modal.classList.contains('hidden')) {

    closeModal();

  }

});