App.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import './App.css';
  2. import { useEffect, useState } from 'react';
  3. let countries = {
  4. "AF": "Afghanistan",
  5. "AX": "Åland Islands",
  6. "AL": "Albania",
  7. "DZ": "Algeria",
  8. "AS": "American Samoa",
  9. "AD": "AndorrA",
  10. "AO": "Angola",
  11. "AI": "Anguilla",
  12. "AQ": "Antarctica",
  13. "AG": "Antigua and Barbuda",
  14. "AR": "Argentina",
  15. "AM": "Armenia",
  16. "AW": "Aruba",
  17. "AU": "Australia",
  18. "AT": "Austria",
  19. "AZ": "Azerbaijan",
  20. "BS": "Bahamas",
  21. "BH": "Bahrain",
  22. "BD": "Bangladesh",
  23. "BB": "Barbados",
  24. "BY": "Belarus",
  25. "BE": "Belgium",
  26. "BZ": "Belize",
  27. "BJ": "Benin",
  28. "BM": "Bermuda",
  29. "BT": "Bhutan",
  30. "BO": "Bolivia",
  31. "BA": "Bosnia and Herzegovina",
  32. "BW": "Botswana",
  33. "BV": "Bouvet Island",
  34. "BR": "Brazil",
  35. "IO": "British Indian Ocean Territory",
  36. "BN": "Brunei Darussalam",
  37. "BG": "Bulgaria",
  38. "BF": "Burkina Faso",
  39. "BI": "Burundi",
  40. "KH": "Cambodia",
  41. "CM": "Cameroon",
  42. "CA": "Canada",
  43. "CV": "Cape Verde",
  44. "KY": "Cayman Islands",
  45. "CF": "Central African Republic",
  46. "TD": "Chad",
  47. "CL": "Chile",
  48. "CN": "China",
  49. "CX": "Christmas Island",
  50. "CC": "Cocos (Keeling) Islands",
  51. "CO": "Colombia",
  52. "KM": "Comoros",
  53. "CG": "Congo",
  54. "CD": "Congo, Democratic Republic",
  55. "CK": "Cook Islands",
  56. "CR": "Costa Rica",
  57. "CI": "Cote D\"Ivoire",
  58. "HR": "Croatia",
  59. "CU": "Cuba",
  60. "CY": "Cyprus",
  61. "CZ": "Czech Republic",
  62. "DK": "Denmark",
  63. "DJ": "Djibouti",
  64. "DM": "Dominica",
  65. "DO": "Dominican Republic",
  66. "EC": "Ecuador",
  67. "EG": "Egypt",
  68. "SV": "El Salvador",
  69. "GQ": "Equatorial Guinea",
  70. "ER": "Eritrea",
  71. "EE": "Estonia",
  72. "ET": "Ethiopia",
  73. "FK": "Falkland Islands (Malvinas)",
  74. "FO": "Faroe Islands",
  75. "FJ": "Fiji",
  76. "FI": "Finland",
  77. "FR": "France",
  78. "GF": "French Guiana",
  79. "PF": "French Polynesia",
  80. "TF": "French Southern Territories",
  81. "GA": "Gabon",
  82. "GM": "Gambia",
  83. "GE": "Georgia",
  84. "DE": "Germany",
  85. "GH": "Ghana",
  86. "GI": "Gibraltar",
  87. "GR": "Greece",
  88. "GL": "Greenland",
  89. "GD": "Grenada",
  90. "GP": "Guadeloupe",
  91. "GU": "Guam",
  92. "GT": "Guatemala",
  93. "GG": "Guernsey",
  94. "GN": "Guinea",
  95. "GW": "Guinea-Bissau",
  96. "GY": "Guyana",
  97. "HT": "Haiti",
  98. "HM": "Heard Island and Mcdonald Islands",
  99. "VA": "Holy See (Vatican City State)",
  100. "HN": "Honduras",
  101. "HK": "Hong Kong",
  102. "HU": "Hungary",
  103. "IS": "Iceland",
  104. "IN": "India",
  105. "ID": "Indonesia",
  106. "IR": "Iran",
  107. "IQ": "Iraq",
  108. "IE": "Ireland",
  109. "IM": "Isle of Man",
  110. "IL": "Israel",
  111. "IT": "Italy",
  112. "JM": "Jamaica",
  113. "JP": "Japan",
  114. "JE": "Jersey",
  115. "JO": "Jordan",
  116. "KZ": "Kazakhstan",
  117. "KE": "Kenya",
  118. "KI": "Kiribati",
  119. "KP": "Korea (North)",
  120. "KR": "Korea (South)",
  121. "XK": "Kosovo",
  122. "KW": "Kuwait",
  123. "KG": "Kyrgyzstan",
  124. "LA": "Laos",
  125. "LV": "Latvia",
  126. "LB": "Lebanon",
  127. "LS": "Lesotho",
  128. "LR": "Liberia",
  129. "LY": "Libyan Arab Jamahiriya",
  130. "LI": "Liechtenstein",
  131. "LT": "Lithuania",
  132. "LU": "Luxembourg",
  133. "MO": "Macao",
  134. "MK": "Macedonia",
  135. "MG": "Madagascar",
  136. "MW": "Malawi",
  137. "MY": "Malaysia",
  138. "MV": "Maldives",
  139. "ML": "Mali",
  140. "MT": "Malta",
  141. "MH": "Marshall Islands",
  142. "MQ": "Martinique",
  143. "MR": "Mauritania",
  144. "MU": "Mauritius",
  145. "YT": "Mayotte",
  146. "MX": "Mexico",
  147. "FM": "Micronesia",
  148. "MD": "Moldova",
  149. "MC": "Monaco",
  150. "MN": "Mongolia",
  151. "MS": "Montserrat",
  152. "MA": "Morocco",
  153. "MZ": "Mozambique",
  154. "MM": "Myanmar",
  155. "NA": "Namibia",
  156. "NR": "Nauru",
  157. "NP": "Nepal",
  158. "NL": "Netherlands",
  159. "AN": "Netherlands Antilles",
  160. "NC": "New Caledonia",
  161. "NZ": "New Zealand",
  162. "NI": "Nicaragua",
  163. "NE": "Niger",
  164. "NG": "Nigeria",
  165. "NU": "Niue",
  166. "NF": "Norfolk Island",
  167. "MP": "Northern Mariana Islands",
  168. "NO": "Norway",
  169. "OM": "Oman",
  170. "PK": "Pakistan",
  171. "PW": "Palau",
  172. "PS": "Palestinian Territory, Occupied",
  173. "PA": "Panama",
  174. "PG": "Papua New Guinea",
  175. "PY": "Paraguay",
  176. "PE": "Peru",
  177. "PH": "Philippines",
  178. "PN": "Pitcairn",
  179. "PL": "Poland",
  180. "PT": "Portugal",
  181. "PR": "Puerto Rico",
  182. "QA": "Qatar",
  183. "RE": "Reunion",
  184. "RO": "Romania",
  185. "RU": "Russian Federation",
  186. "RW": "Rwanda",
  187. "SH": "Saint Helena",
  188. "KN": "Saint Kitts and Nevis",
  189. "LC": "Saint Lucia",
  190. "PM": "Saint Pierre and Miquelon",
  191. "VC": "Saint Vincent and the Grenadines",
  192. "WS": "Samoa",
  193. "SM": "San Marino",
  194. "ST": "Sao Tome and Principe",
  195. "SA": "Saudi Arabia",
  196. "SN": "Senegal",
  197. "RS": "Serbia",
  198. "ME": "Montenegro",
  199. "SC": "Seychelles",
  200. "SL": "Sierra Leone",
  201. "SG": "Singapore",
  202. "SK": "Slovakia",
  203. "SI": "Slovenia",
  204. "SB": "Solomon Islands",
  205. "SO": "Somalia",
  206. "ZA": "South Africa",
  207. "GS": "South Georgia and the South Sandwich Islands",
  208. "ES": "Spain",
  209. "LK": "Sri Lanka",
  210. "SD": "Sudan",
  211. "SR": "Suriname",
  212. "SJ": "Svalbard and Jan Mayen",
  213. "SZ": "Swaziland",
  214. "SE": "Sweden",
  215. "CH": "Switzerland",
  216. "SY": "Syrian Arab Republic",
  217. "TW": "Taiwan, Province of China",
  218. "TJ": "Tajikistan",
  219. "TZ": "Tanzania",
  220. "TH": "Thailand",
  221. "TL": "Timor-Leste",
  222. "TG": "Togo",
  223. "TK": "Tokelau",
  224. "TO": "Tonga",
  225. "TT": "Trinidad and Tobago",
  226. "TN": "Tunisia",
  227. "TR": "Turkey",
  228. "TM": "Turkmenistan",
  229. "TC": "Turks and Caicos Islands",
  230. "TV": "Tuvalu",
  231. "UG": "Uganda",
  232. "UA": "Ukraine",
  233. "AE": "United Arab Emirates",
  234. "GB": "United Kingdom",
  235. "US": "United States",
  236. "UM": "United States Minor Outlying Islands",
  237. "UY": "Uruguay",
  238. "UZ": "Uzbekistan",
  239. "VU": "Vanuatu",
  240. "VE": "Venezuela",
  241. "VN": "Viet Nam",
  242. "VG": "Virgin Islands, British",
  243. "VI": "Virgin Islands, U.S.",
  244. "WF": "Wallis and Futuna",
  245. "EH": "Western Sahara",
  246. "YE": "Yemen",
  247. "ZM": "Zambia",
  248. "ZW": "Zimbabwe"
  249. }
  250. const MySelect = ({ options = countries, value = 'YE', onChange }) => {
  251. return (
  252. <select
  253. defaultValue={value}
  254. onChange={(e) => onChange(e.target.value)}
  255. >
  256. {Object.entries(options).map(country => {
  257. return (
  258. <option
  259. value={country[0]}
  260. >
  261. {country[1]}
  262. </option>
  263. )
  264. })}
  265. </select>
  266. )
  267. }
  268. const PhoneBookEntry = ({ data: { name, phone } = { name: '', phone: '' },
  269. key='',
  270. onChange,
  271. onMoveUp,
  272. onMoveDown,
  273. onDelete,
  274. onAdd }) =>
  275. <>
  276. <input
  277. value={name}
  278. onChange={(e) => onChange({name:e.target.value, phone})}
  279. />
  280. <input
  281. type="number"
  282. value={phone}
  283. onChange={(e) => onChange({name, phone:e.target.value})}
  284. />
  285. <button
  286. onClick={() => onMoveUp()}
  287. >
  288. ^
  289. </button>
  290. <button
  291. onClick={()=>onMoveDown()}
  292. >
  293. v
  294. </button>
  295. <button
  296. onClick={() => onDelete({name, phone, key})}
  297. >
  298. x
  299. </button>
  300. <button
  301. onClick={() => onAdd()}
  302. >
  303. +
  304. </button>
  305. <br/>
  306. </>
  307. const defaultPeople = [
  308. { name: 'John', phone: '102' },
  309. { name: 'Paul', phone: '103' },
  310. { name: 'Bob', phone: '111' },
  311. { name: 'Marshall', phone: '313' }
  312. ]
  313. const PhoneBook = ({ people = defaultPeople}) => {
  314. const [ppl, setPpl] = useState(people)
  315. const [keys, setKeys] = useState(people.map(() => Math.random()))
  316. const [name, setName] = useState('')
  317. const [phone, setPhone] = useState('')
  318. useEffect(() => {
  319. setPpl(people)
  320. setKeys(keys)
  321. }, [keys, people])
  322. const onChange = (newData, i) => {
  323. let newPpl = []
  324. ppl.forEach((item, index) => {
  325. if(index === i) {
  326. newPpl.push(newData)
  327. } else {
  328. newPpl.push(item)
  329. }
  330. })
  331. setPpl(newPpl)
  332. }
  333. const onAdd = (newData, i) => {
  334. let newArr = []
  335. ppl.forEach(item => newArr.push(item))
  336. i === 'start'?
  337. newArr.unshift(newData)
  338. :
  339. newArr.splice(i+1, 0, newData)
  340. setPpl(newArr)
  341. keys.splice(i ,0, Math.random())
  342. }
  343. const onDelete = (i) => {
  344. setPpl(ppl.filter((item)=>
  345. item.name !== i.name && item.phone !== i.phone
  346. )
  347. )
  348. keys.splice(i.key,1)
  349. }
  350. const onMoveUp = (index) => {
  351. let newArr = []
  352. ppl.forEach(item => newArr.push(item))
  353. let temp = newArr[index]
  354. let tempKey = keys[index]
  355. if(index === 0) {
  356. newArr[index] = newArr[newArr.length - 1]
  357. newArr[newArr.length - 1] = temp
  358. keys[index] = keys[0]
  359. keys[0] = tempKey
  360. } else {
  361. newArr[index] = newArr[index - 1]
  362. newArr[index - 1] = temp
  363. keys[index] = keys[index - 1]
  364. keys[index - 1] = tempKey
  365. }
  366. setPpl(newArr)
  367. }
  368. const onMoveDown = (index) => {
  369. let newArr = []
  370. ppl.forEach(item => newArr.push(item))
  371. let temp = newArr[index]
  372. let tempKey = keys[index]
  373. if(index === newArr.length - 1) {
  374. newArr[index] = newArr[0]
  375. newArr[0] = temp
  376. keys[index] = keys[0]
  377. keys[0] = tempKey
  378. } else {
  379. newArr[index] = newArr[index + 1]
  380. newArr[index + 1] = temp
  381. keys[index] = keys[index + 1]
  382. keys[index + 1] = tempKey
  383. }
  384. setPpl(newArr)
  385. }
  386. console.log('keys',keys)
  387. console.log('people',ppl)
  388. return (
  389. <>
  390. <input
  391. placeholder='name to add'
  392. value={name}
  393. onChange={e => setName(e.target.value)}
  394. />
  395. <input
  396. type="number"
  397. placeholder='phone to add'
  398. value={phone}
  399. onChange={e => setPhone(e.target.value)}
  400. />
  401. <button
  402. onClick = {() => onAdd({name, phone}, 'start')}
  403. >
  404. +
  405. </button>
  406. <div className='PhoneBook'>
  407. {ppl.map((data,index) =>
  408. <PhoneBookEntry
  409. data={data}
  410. key={index}
  411. onChange={(data) => onChange(data, index)}
  412. onDelete={() => onDelete(data)}
  413. onAdd={() => onAdd({name, phone}, index)}
  414. onMoveDown={() => onMoveDown(index)}
  415. onMoveUp={() => onMoveUp(index)}
  416. />)
  417. }
  418. </div>
  419. <button onClick={() => console.log(ppl)}>Save</button>
  420. </>
  421. )
  422. }
  423. function App() {
  424. const [code, setCode] = useState("UA")
  425. return (
  426. <div className="App">
  427. <MySelect
  428. options={countries}
  429. value={code}
  430. onChange={item => setCode(item)}
  431. />
  432. <h2>{code}</h2>
  433. <PhoneBook />
  434. </div>
  435. );
  436. }
  437. export default App;