Skip to content

Commit

Permalink
Merge pull request #93 from muniz95/#89-simulate-calls
Browse files Browse the repository at this point in the history
#89 simulate calls
  • Loading branch information
muniz95 authored Dec 22, 2023
2 parents fbcf62f + e56eae4 commit 50b7a32
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 343 deletions.
656 changes: 327 additions & 329 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
"predeploy": "npm run build"
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/plugin-transform-private-property-in-object": "^7.23.4",
"@babel/preset-env": "^7.23.3",
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
Expand Down
65 changes: 65 additions & 0 deletions src/components/VoiceCall/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from "react";
import { useTranslation } from "react-i18next";
import service from "services/call.service";
import S from "./styled";
import { formatTime } from "utils/helpers";

interface IProps {
type: "outgoing" | "received"
contactName: string;
phone: string;
}

const VoiceCall = ({ contactName, type, phone }: IProps) => {
const { t } = useTranslation();
const currentTimer = React.useRef<NodeJS.Timeout>();
const [isCallActive, setIsCallActive] = React.useState(false);
const [isConnecting, setIsConnecting] = React.useState(false);
const [callTime, setCallTime] = React.useState<number>(0);
const [callStartDate, setCallStartDate] = React.useState<number>(0);

const startCall = () => {
setIsConnecting(true);
setIsCallActive(true);
setTimeout(() => {
currentTimer.current = setInterval(() => {
setCallTime(state => state += 1);
}, 1000);
setCallStartDate(Date.now());
setIsConnecting(false);
}, 3000)
}

const endCall = () => {
clearInterval(currentTimer.current);
setIsCallActive(false);
service.insertCallRecord({
contactName,
startDate: callStartDate,
endDate: Date.now(),
phone,
type
});
}

return (
<>
<S.MainContainer>
{
isCallActive
? isConnecting ? `Calling ${contactName}...` : formatTime(callTime)
: formatTime(callTime)
}
</S.MainContainer>
<S.ButtonContainer>
{
isCallActive
? <button onClick={() => endCall()}>{t("end")}</button>
: <button onClick={() => startCall()}>{t("start")}</button>
}
</S.ButtonContainer>
</>
)
}

export default VoiceCall;
46 changes: 46 additions & 0 deletions src/components/VoiceCall/styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import styled from "styled-components";

export const MainContainer = styled.div`
height: 80%;
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
overflow: auto;
`;

export const ButtonContainer = styled.div`
height: 20%;
display: flex;
align-items: center;
justify-content: center;
`;

export const ResultsBox = styled.div`
min-height: 30%;
width: 100%;
align-items: center;
justify-content: center;
display: flex;
`;

export const Item = styled.button`
min-height: 30%;
width: 100%;
border: none;
&:active, &:focus {
font-weight: bold;
background-color: #000000c6;
color: #ffffffc6;
}
`;

const S = {
ButtonContainer,
MainContainer,
Item,
ResultsBox,
}

export default S;
9 changes: 9 additions & 0 deletions src/interfaces/callRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { StorageEntity } from "./storageEntity";

export interface CallRecord extends StorageEntity {
contactName: string
type: "received" | "outgoing"
phone: string
startDate: number
endDate: number
}
8 changes: 5 additions & 3 deletions src/interfaces/storageEntity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface StorageEntity {
id: string
date: string
import { generateId } from "utils/helpers"

export class StorageEntity {
id?: string = generateId().toString();
date?: number = Date.now();
}
4 changes: 3 additions & 1 deletion src/locales/en/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"no": "No",
"done": "Done",
"play": "Play",
"call": "Call"
"call": "Call",
"start": "Start",
"end": "End"
}
2 changes: 1 addition & 1 deletion src/redux/actions/profile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Profile } from "@interfaces/profile";
import { Profile } from "interfaces/profile";

export const setCurrentProfile = (payload: Profile) =>
({ type: "SET_CURRENT_PROFILE", payload });
12 changes: 12 additions & 0 deletions src/services/call.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CallRecord } from "interfaces/callRecord";
import db from "../utils/db";

export const insertCallRecord = (callRecord: CallRecord) => {
db.insert<CallRecord>("callRecords", callRecord);
}

const service = {
insertCallRecord,
}

export default service;
14 changes: 10 additions & 4 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export const isOn = () => {
export const checkDb = () =>
Boolean(localStorage.getItem("simNumbers")) &&
Boolean(localStorage.getItem("profiles")) &&
Boolean(localStorage.getItem("contacts"));
Boolean(localStorage.getItem("contacts")) &&
Boolean(localStorage.getItem("callRecords"));

export const initDb = () => {
localStorage.setItem("contacts", JSON.stringify(seed.contacts));
localStorage.setItem("simNumbers", JSON.stringify(seed.simNumbers));
localStorage.setItem("profiles", JSON.stringify(seed.profiles));
localStorage.setItem("color", defaults.color);
localStorage.setItem("currentProfile", JSON.stringify(defaults.profile));
localStorage.setItem("callRecords", "[]");
}

export function getPlain(key: string) {
Expand All @@ -35,9 +37,13 @@ export function set<T>(key: string, value: T) {
}

export function insert<T>(key: string, item: T) {
const collection = get<T[]>(key);
collection.push(item);
localStorage.setItem(key, JSON.stringify(collection));
try {
const collection = get<T[]>(key);
collection.push(item);
localStorage.setItem(key, JSON.stringify(collection));
} catch (error) {
throw error;
}
}

export function update<T extends StorageEntity>(key: string, item: T) {
Expand Down
9 changes: 6 additions & 3 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ export const isPlainObject = (value: any) =>
typeof value === 'boolean' ||
typeof value === 'object';

export const generateId = () => {
crypto.randomUUID ? crypto.randomUUID() : crypto.getRandomValues(new Uint16Array(16))
}
export const generateId = () =>
crypto.randomUUID ? crypto.randomUUID() : crypto.getRandomValues(new Uint16Array(16));

export const formatTime = (seconds: number) =>
(seconds - (seconds %= 60)) / 60 + (9 < seconds ? ':' : ':0') + seconds;

const helpers = {
isBetween,
isPlainObject,
generateId,
formatTime,
};

export default helpers;
2 changes: 0 additions & 2 deletions src/views/PhoneBook/PhoneBookAddName/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ const PhoneBookAddName = () => {
service.insertContact({
name,
number: Date.now().toString(),
id: crypto.randomUUID(),
date: Date.now().toLocaleString(),
isServiceNumber: false,
});
vibration.success();
Expand Down

0 comments on commit 50b7a32

Please sign in to comment.