Skip to content

Commit

Permalink
Merge pull request #24 from yourssu/feat/#14
Browse files Browse the repository at this point in the history
initialLog 함수 추가 및 Logger.ts 로직 분리
  • Loading branch information
Hanna922 authored Feb 9, 2024
2 parents fa92945 + 1167263 commit f7bd4b6
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 60 deletions.
72 changes: 23 additions & 49 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { postLog } from './apis/postLog';
import { LogPayloadParams, LogType } from './types/LogType';
import { SetLocalStorage } from './SetLocalStorage';
import { LogPayloadParams, ServiceNameType } from './types/LogType';
import CryptoJS from 'crypto-js';

const createRandomId = () => {
Expand Down Expand Up @@ -38,62 +38,36 @@ const createTimestamp = () => {
return now.toISOString();
};

const setLocalStorageClear = () => {
const list: any[] = [];
localStorage.setItem('yls-web', JSON.stringify(list));
};
const initialLog = (
userId: string,
serviceName: ServiceNameType,
name: string,
path: string | undefined
) => {
const loggerType: LogPayloadParams = {
userId: userId,
path: '/',
serviceName: serviceName,
name: '',
message: '',
};

const setLocalStorage = async (logger: LogType) => {
if (window.localStorage.getItem('yls-web') == undefined) {
const list: any[] = [];
list.push(logger);
localStorage.setItem('yls-web', JSON.stringify(list));
} else {
const remainList: any[] = JSON.parse(localStorage.getItem('yls-web') as string) || [];
if (remainList.length < 10) {
const updateList = [...remainList, logger];
localStorage.setItem('yls-web', JSON.stringify(updateList));
} else {
setLocalStorageClear();
const res = await postLog();
}
}
const logger = Logger(loggerType);

logger.event.name = name;
logger.event.path = path;

SetLocalStorage(logger);
};

export const useYLSLogger = () => {
const screen = ({ userId, serviceName, name, path }: LogPayloadParams) => {
const loggerType: LogPayloadParams = {
userId: userId,
path: '/',
serviceName: serviceName,
name: '',
message: '',
};
const logger = Logger(loggerType);
console.log(`Logging screen information for path: ${serviceName}`);
logger.event.name = name;
logger.event.path = path;

setLocalStorage(logger);
initialLog(userId, serviceName, name, path);
};

const click = ({ userId, serviceName, name, path }: LogPayloadParams) => {
console.log(`Logging click information for button: ${name}`);
//사용자에서 path,name,message를 넣어줌
const loggerType: LogPayloadParams = {
userId: userId,
path: '/',
serviceName: serviceName,
name: '',
message: '',
};
const logger = Logger(loggerType);

logger.event.name = name;
logger.event.path = path;
setLocalStorage(logger);
initialLog(userId, serviceName, name, path);
};
// todo: 로컬스토리지 로그 개수가 10개 넘어가면 postLog.ts호출

return {
screen,
Expand Down
4 changes: 2 additions & 2 deletions src/LogClick.tsx → src/components/LogClick.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Children, cloneElement } from 'react';
import { useYLSLogger } from '.';
import { LogPayloadParams } from './types/LogType';
import { useYLSLogger } from '..';
import { LogPayloadParams } from '../types/LogType';

interface Props {
children: React.ReactElement;
Expand Down
4 changes: 2 additions & 2 deletions src/LogScreen.tsx → src/components/LogScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useYLSLogger } from '.';
import { useYLSLogger } from '..';
import { useEffect } from 'react';
import { LogPayloadParams } from './types/LogType';
import { LogPayloadParams } from '../types/LogType';

interface Props {
children: React.ReactNode;
Expand Down
4 changes: 2 additions & 2 deletions src/demo/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BrowserRouter } from 'react-router-dom';
import { Routes, Route } from 'react-router-dom';
import { Routes, Route, BrowserRouter } from 'react-router-dom';
import { Home } from './Home';
import { Drawer } from './Drawer';

export const App = () => {
return (
<>
Expand Down
5 changes: 3 additions & 2 deletions src/demo/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState } from 'react';
import { LogClick } from '../LogClick';
import { LogClick } from '../components/LogClick';
import { useLocation } from 'react-router-dom';
import { LogScreen } from '../LogScreen';
import { LogScreen } from '../components/LogScreen';

export const Drawer = () => {
const [count, setCount] = useState(0);
const router = useLocation();
Expand Down
4 changes: 2 additions & 2 deletions src/demo/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { LogClick } from '../LogClick';
import { LogClick } from '../components/LogClick';
import { useLocation } from 'react-router-dom';
import { LogScreen } from '../LogScreen';
import { LogScreen } from '../components/LogScreen';

export const Home = () => {
const [count, setCount] = useState(0);
Expand Down
1 change: 0 additions & 1 deletion src/demo/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { App } from './App';

Expand Down
24 changes: 24 additions & 0 deletions src/setLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { postLog } from './apis/postLog';
import { LogType } from './types/LogType';

const SetLocalStorageClear = () => {
const list: any[] = [];
localStorage.setItem('yls-web', JSON.stringify(list));
};

export const SetLocalStorage = async (logger: LogType) => {
if (window.localStorage.getItem('yls-web') == undefined) {
const list: any[] = [];
list.push(logger);
localStorage.setItem('yls-web', JSON.stringify(list));
} else {
const remainList: any[] = JSON.parse(localStorage.getItem('yls-web') as string) || [];
if (remainList.length < 10) {
const updateList = [...remainList, logger];
localStorage.setItem('yls-web', JSON.stringify(updateList));
} else {
SetLocalStorageClear();
const res = await postLog();
}
}
};

0 comments on commit f7bd4b6

Please sign in to comment.