Skip to content

Commit 738cd5d

Browse files
committed
[PRAC/cont] Add logic ID gen for "storage" clients
Generating custom ID num's for "new" clients (continued order). Worth noting: - working through the server array length. core: B-3 / JS-BL
1 parent 69fb170 commit 738cd5d

File tree

1 file changed

+17
-1
lines changed
  • core-courses/3-js-basic-level/practicum-js-basic-level/sb-crm-client/js

1 file changed

+17
-1
lines changed

core-courses/3-js-basic-level/practicum-js-basic-level/sb-crm-client/js/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
// !! [FOR DEMO - sessionStorage] фиксация исходных данных/серверных данных, для сохранения в sessionStorage
281281
let clientsDataArrWithIds;
282282
let updateClientsDataArr = [];
283+
let clientsServerDataArr = []; // потом/будет использоваться для генерации storage ID
283284

284285
async function addClientsDataToStorage() {
285286
try {
@@ -293,6 +294,8 @@
293294
const data = await response.json(); // преобразование данных в JSON-формат
294295
sessionStorage.setItem('demoClients', JSON.stringify(data)); // добавление/сохранение в Session storage
295296

297+
clientsServerDataArr = data; // "дополнительно" фиксируем входящий серверный массив
298+
296299
return data; // возврат массива данных
297300
} catch (error) {
298301
console.error('Ошибка загрузки списка клиентов с сервера!', error);
@@ -2865,7 +2868,7 @@
28652868

28662869
// !! [FOR DEMO - sessionStorage] "расширенный" объект client (т.к. нет серверных полей.. добавление самостоятельно)
28672870
const client = {
2868-
id: type === 'edit' ? clientData.id : Date.now().toString(), // генерация ID
2871+
id: type === 'edit' ? clientData.id : generateClientStorageId(), // генерация custom ID
28692872
surname: formInSurname,
28702873
name: formInName,
28712874
patronymic: formInPatronymic,
@@ -2954,6 +2957,19 @@
29542957
);
29552958
}
29562959

2960+
// !! [FOR DEMO - sessionStorage] генерация custom ID номеров для "новых" клиентов (продолжение порядка.. до 99 потом нужно/будет корректировать)
2961+
function generateClientStorageId() {
2962+
const orderPrefix = clientsServerDataArr.length; // фиксация длинны серверного массива (ранее/выше приходящего)
2963+
2964+
// генерация случайных/дополнительных цифр для ID (т.е. генерация 5-ти цифр, если в массиве < 10 клиентов, если больше.. то генерация 4-х цифр)
2965+
const randomDigits =
2966+
clientsServerDataArr.length < 10
2967+
? Math.floor(10000 + Math.random() * 90000) // 5 цифр
2968+
: Math.floor(1000 + Math.random() * 9000); // 4 цифры
2969+
2970+
return `${orderPrefix}${randomDigits}`; // возврат сформированного ID
2971+
}
2972+
29572973
// ** [СЕРВЕР] отправка данных/добавление клиентов на сервер, получение обратно (проверка статуса)
29582974
// ?? следует раскомментировать (полностью)
29592975
// async function addClientToServer(clientData) {

0 commit comments

Comments
 (0)