Best JavaScript code snippet using devicefarmer-stf
Projects.js
Source:Projects.js
1import Project from "../models/ProjectModel.js";2import Tasks from "../models/TaskModel.js";3import Notifications from "../models/Notifications.js";4export const allProjects = async (req, res) =>{5 try {6 let response = await Project.findAll();7 res.status(200).json(response);8 } catch (error) {9 res.status(500).json({msg: error.message});10 }11}12export const getProjects = async (req, res) =>{13 try {14 let response;15 if(req.role === "admin"){16 response = await Project.findAll();17 }else{18 response = await Project.findAll();19 }20 res.status(200).json(response);21 } catch (error) {22 res.status(500).json({msg: error.message});23 }24}25export const getProjectById = async(req, res) =>{26 try {27 const project = await Project.findOne({28 where:{29 projectid: req.params.id30 }31 });32 if(!project) return res.status(404).json({msg: "Data tidak ditemukan"});33 let response;34 if(req.role === "admin"){35 response = await Project.findOne({36 where:{37 id: project.id38 }39 });40 }else{41 response = await Project.findOne({42 attributes:['uuid','name','price'],43 where:{44 id: project.id45 }46 });47 }48 res.status(200).json(response);49 } catch (error) {50 res.status(500).json({msg: error.message});51 }52}53export const getProjectByStatus = async(req, res) =>{54 try {55 const project = await Project.findAll({56 where:{57 status: req.params.status58 }59 });60 if(!project) return res.status(404).json({msg: "Data tidak ditemukan"});61 res.status(200).json(project);62 } catch (error) {63 res.status(500).json({msg: error.message});64 }65}66export const createProject = async(req, res) =>{67 const {projectname, description, bagdept, subbagdept, participant, location, ttlitem, ttlbobot, startproj, endproj, status, inputby, uid} = req.body;68 const projectid = Date.now();69 try {70 await Project.create({71 projectid: projectid,72 projectname: projectname,73 description: description,74 bagdept: bagdept,75 subbagdept: subbagdept,76 pic: participant,77 location: JSON.stringify(location),78 ttlitem: ttlitem,79 ttlbobot: ttlbobot,80 startproj: startproj,81 endproj: endproj,82 status: status,83 inputby: inputby84 });85 let task_id = Date.now();86 for (let index = 0; index < location.length; index++) {87 Tasks.create({88 taskid: task_id + index,89 projectid: projectid,90 task_name: location[index].task,91 progress: 0,92 bobot: location[index].bobot,93 pic: location[index].pic.value,94 due_date: location[index].duedate,95 t_status: false,96 startdate: location[index].startdate97 });98 let nid = 'ND'+Date.now()+index;99 Notifications.create({100 id: nid,101 description: 'New Task' +': '+ location[index].task + '. Project ID: '+ projectid,102 taskfrom: uid,103 taskto: location[index].pic.uid,104 trxtype: 'New Task',105 foreignid: projectid,106 read_status: false107 });108 }109 res.status(201).json({msg: "Project Created Successfuly"});110 } catch (error) {111 res.status(500).json({msg: error.message});112 }113}114export const updateProject = async(req, res) =>{115 try {116 const project = await Project.findOne({117 where:{118 projectid: req.params.id119 }120 });121 if(!project) return res.status(404).json({msg: "Data tidak ditemukan"});122 const {projectname, description, bagdept, subbagdept, pic, location, ttlitem, ttlbobot, startproj, endproj, status, inputby} = req.body;123 if(req.role === "admin"){124 await Project.update({projectname, description, bagdept, subbagdept, pic, location, ttlitem, ttlbobot, startproj, endproj, status, inputby},{125 where:{126 id: project.id127 }128 });129 }else{130 if(req.userId !== project.userId) return res.status(403).json({msg: "Akses terlarang"});131 await Project.update({projectname, description, bagdept, subbagdept, pic, location, ttlitem, ttlbobot, startproj, endproj, status, inputby},{132 where:{133 id: project.id134 }135 });136 }137 res.status(200).json({msg: "Project updated successfuly"});138 } catch (error) {139 res.status(500).json({msg: error.message});140 }141}142export const deleteProject = async(req, res) =>{143 try {144 const project = await Project.findOne({145 where:{146 projectid: req.params.id147 }148 });149 if(!project) return res.status(404).json({msg: "Data tidak ditemukan"});150 const {name, price} = req.body;151 if(req.role === "admin"){152 await Project.destroy({153 where:{154 id: project.id155 }156 });157 }else{158 if(req.userId !== project.userId) return res.status(403).json({msg: "Akses terlarang"});159 await Project.destroy({160 where:{161 id: project.id162 }163 });164 }165 res.status(200).json({msg: "Project deleted successfuly"});166 } catch (error) {167 res.status(500).json({msg: error.message});168 }...
Header.js
Source:Header.js
1import React, { useEffect } from "react";2import { Navbar, Container, Nav } from "react-bootstrap";3import { LinkContainer } from "react-router-bootstrap";4import { useDispatch, useSelector } from "react-redux";5import { fecthCategory } from "../pages/category/categoryAction";6import { userLogout } from "../pages/userData/userAction";7const Header = () => {8 const dispatch = useDispatch();9 const { categories } = useSelector((state) => state.category);10 const { cartList } = useSelector((state) => state.cart);11 const { isLoggedIn } = useSelector((state) => state.user);12 useEffect(() => {13 if (!categories.length) {14 dispatch(fecthCategory());15 }16 }, [dispatch, categories]);17 return (18 <Navbar collapseOnSelect expand="lg" bg="dark" variant="dark" fixed="top">19 <Container>20 <LinkContainer to="/" style={{ letterSpacing: "2px" }}>21 <Navbar.Brand>E-Shop</Navbar.Brand>22 </LinkContainer>23 <Navbar.Toggle aria-controls="responsive-navbar-nav" />24 <Navbar.Collapse id="responsive-navbar-nav">25 <Nav className="me-auto">26 {categories?.map(27 (value) =>28 !value.parentCat &&29 value.status === "active" && (30 <LinkContainer31 to={`/${value.slug}`}32 key={value._id}33 style={{ letterSpacing: "2px" }}34 >35 <Nav.Link key={value._id}>{value.name}</Nav.Link>36 </LinkContainer>37 )38 )}39 </Nav>40 {isLoggedIn ? (41 <Nav>42 <LinkContainer to="/profile" style={{ letterSpacing: "2px" }}>43 <Nav.Link>44 My Profile <i class="fas fa-user-circle"></i>45 </Nav.Link>46 </LinkContainer>47 <LinkContainer to="/cart" style={{ letterSpacing: "2px" }}>48 <Nav.Link>49 Cart <i className="fas fa-shopping-cart"></i> (50 {cartList?.reduce(51 (ttlItem, value) => (ttlItem = ttlItem + value?.qty),52 053 )}54 )55 </Nav.Link>56 </LinkContainer>57 <LinkContainer to="/home" style={{ letterSpacing: "2px" }}>58 <Nav.Link onClick={() => dispatch(userLogout())}>59 Log Out60 </Nav.Link>61 </LinkContainer>62 </Nav>63 ) : (64 <Nav>65 <LinkContainer to="/cart" style={{ letterSpacing: "2px" }}>66 <Nav.Link>67 Cart <i className="fas fa-shopping-cart"></i> (68 {cartList?.reduce(69 (ttlItem, value) => (ttlItem = ttlItem + value?.qty),70 071 )}72 )73 </Nav.Link>74 </LinkContainer>75 <LinkContainer to="/login" style={{ letterSpacing: "2px" }}>76 <Nav.Link>77 Login <i className="fas fa-user"></i>78 </Nav.Link>79 </LinkContainer>80 <LinkContainer81 to="/registration"82 style={{ letterSpacing: "2px" }}83 >84 <Nav.Link>85 Sign Up <i className="fas fa-user-plus"></i>86 </Nav.Link>87 </LinkContainer>88 </Nav>89 )}90 </Navbar.Collapse>91 </Container>92 </Navbar>93 );94};...
storage.helper.ts
Source:storage.helper.ts
1import localForage from 'localforage';2import { Config } from '../config';3type TTLItem<T = any> = { value: T; timestamp: number; ttl?: number };4export class StorageHelper {5 private static readonly storage: LocalForage = localForage.createInstance({ name: 'storage' });6 static async setItem<T = any>(key: string, value: T, ttl?: number): Promise<T> {7 if (this.storage && !Config.isServer) {8 await this.storage.setItem<TTLItem<T>>(key, { value, timestamp: Date.now(), ttl });9 }10 return value;11 }12 static async getItem<T = any>(key: string): Promise<T | undefined> {13 if (this.storage && !Config.isServer) {14 const item = await this.storage.getItem<TTLItem<T>>(key);15 if (item) {16 const { value, timestamp, ttl } = item;17 if (!ttl || Date.now() - timestamp >= ttl) {18 return value;19 }20 }21 await this.storage.removeItem(key);22 }23 return undefined;24 }...
Using AI Code Generation
1var TtlItem = require('devicefarmer-stf').TtlItem;2var ttlItem = new TtlItem(1000);3ttlItem.on('expired', function() {4 console.log('expired');5});6ttlItem.touch();7ttlItem.touch();8ttlItem.touch();9ttlItem.touch();10ttlItem.touch();
Using AI Code Generation
1var TtlItem = require('devicefarmer-stf').TtlItem;2var ttlItem = new TtlItem(1000);3var TtlItem = require('devicefarmer-stf').TtlItem;4var ttlItem = new TtlItem(1000);5var TtlItem = require('devicefarmer-stf').TtlItem;6var ttlItem = new TtlItem(1000);7var TtlItem = require('devicefarmer-stf').TtlItem;8var ttlItem = new TtlItem(1000);9var TtlItem = require('devicefarmer-stf').TtlItem;10var ttlItem = new TtlItem(1000);11var TtlItem = require('devicefarmer-stf').TtlItem;12var ttlItem = new TtlItem(1000);13var TtlItem = require('devicefarmer-stf').TtlItem;14var ttlItem = new TtlItem(1000);15var TtlItem = require('devicefarmer-stf').TtlItem;16var ttlItem = new TtlItem(1000);17var TtlItem = require('devicefarmer-stf').TtlItem;18var ttlItem = new TtlItem(1000);19var TtlItem = require('devicefarmer-stf').TtlItem;20var ttlItem = new TtlItem(1000);21var TtlItem = require('devicefarmer-stf').TtlItem;22var ttlItem = new TtlItem(1000);23var TtlItem = require('devicefarmer-stf').T
Using AI Code Generation
1var TtlItem = require('devicefarmer-stf').TtlItem;2var item = new TtlItem('test', 1000);3item.getTtl();4item.setTtl(2000);5item.getTtl();6item.resetTtl();7item.getTtl();8var TtlItem = require('devicefarmer-stf').TtlItem;9var item = new TtlItem('test', 1000);10item.getTtl();11item.setTtl(2000);12item.getTtl();13item.resetTtl();14item.getTtl();15var TtlItem = require('devicefarmer-stf').TtlItem;16var item = new TtlItem('test', 1000);17item.getTtl();18item.setTtl(2000);19item.getTtl();20item.resetTtl();21item.getTtl();22var TtlItem = require('devicefarmer-stf').TtlItem;23var item = new TtlItem('test', 1000);24item.getTtl();25item.setTtl(2000);26item.getTtl();27item.resetTtl();28item.getTtl();29var TtlItem = require('devicefarmer-stf').TtlItem;30var item = new TtlItem('test', 1000);31item.getTtl();32item.setTtl(2000);33item.getTtl();34item.resetTtl();35item.getTtl();36var TtlItem = require('devicefarmer-stf').TtlItem;37var item = new TtlItem('test', 1000);38item.getTtl();39item.setTtl(2000);40item.getTtl();41item.resetTtl();42item.getTtl();43var TtlItem = require('devicefarmer-stf').TtlItem;
Using AI Code Generation
1var TtlItem = require('devicefarmer-stf').TtlItem;2var ttlItem = new TtlItem();3ttlItem.setTtl(1000);4ttlItem.getTtl();5var DeviceFarmer = require('devicefarmer-stf').DeviceFarmer;6var deviceFarmer = new DeviceFarmer();7deviceFarmer.getDevices();8deviceFarmer.getDevice('12345');9deviceFarmer.getDevice('12345').getTtl();10deviceFarmer.getDevice('12345').setTtl(1000);
Using AI Code Generation
1var deviceDb = require('devicefarmer-stf-device-db')2var db = deviceDb({3})4db.TtlItem('1234567890ABCDEF', function (err, data) {5 console.log('TtlItem: ', data)6})7db.TtlItem('1234567890ABCDEF', 12345, function (err, data) {8 console.log('TtlItem: ', data)9})10var deviceDb = require('devicefarmer-stf-device-db')11var db = deviceDb({12})13db.TtlItem('1234567890ABCDEF', function (err, data) {14 console.log('TtlItem: ', data)15})16db.TtlItem('1234567890ABCDEF', 12345, function (err, data) {17 console.log('TtlItem: ', data)18})19var deviceDb = require('devicefarmer-stf-device-db')20var db = deviceDb({21})22db.TtlItem('1234567890ABCDEF', function (err, data) {23 console.log('TtlItem: ', data)24})25db.TtlItem('1234567890ABCDEF', 12345, function (err, data) {26 console.log('TtlItem: ', data)27})28var deviceDb = require('devicefarmer-stf-device-db')29var db = deviceDb({30})
Using AI Code Generation
1var TtlItem = require("devicefarmer-stf-client").TtlItem;2var item = new TtlItem();3item.setTtl(1000);4item.setCallback(function(){5 console.log("I am a callback");6});7item.startTtl();8var stf = require("devicefarmer-stf-client");9var client = new stf.Client();10client.connect()11 .then(function(){12 return client.getDevices();13 })14 .then(function(devices){15 console.log(devices);16 client.disconnect();17 })18 .catch(function(err){19 console.log(err);20 })21[ { serial: '0123456789ABCDEF',22 provider: { name: 'local' },
Using AI Code Generation
1var deviceFarmer = require('devicefarmer-stf');2var ttlItem = new deviceFarmer.TtlItem();3ttlItem.setTtl(1);4ttlItem.setTtl(1);5console.log(ttlItem.getTtl());6var deviceFarmer = require('devicefarmer-stf');7var deviceFarmer = new deviceFarmer.DeviceFarmer();8deviceFarmer.connect();9deviceFarmer.getDevices();10deviceFarmer.disconnect();11var deviceFarmer = require('devicefarmer-stf');12var deviceFarmer = new deviceFarmer.DeviceFarmer();13deviceFarmer.connect();14deviceFarmer.getDevices();15deviceFarmer.disconnect();16var deviceFarmer = require('devicefarmer-stf');17var deviceFarmer = new deviceFarmer.DeviceFarmer();18deviceFarmer.connect();19deviceFarmer.getDevices();20deviceFarmer.disconnect();21var deviceFarmer = require('devicefarmer-stf');22var deviceFarmer = new deviceFarmer.DeviceFarmer();23deviceFarmer.connect();24deviceFarmer.getDevices();25deviceFarmer.disconnect();26var deviceFarmer = require('devicefarmer-stf');27var deviceFarmer = new deviceFarmer.DeviceFarmer();28deviceFarmer.connect();29deviceFarmer.getDevices();30deviceFarmer.disconnect();31var deviceFarmer = require('devicefarmer-stf');32var deviceFarmer = new deviceFarmer.DeviceFarmer();33deviceFarmer.connect();34deviceFarmer.getDevices();35deviceFarmer.disconnect();36var deviceFarmer = require('devicefarmer-stf');37var deviceFarmer = new deviceFarmer.DeviceFarmer();38deviceFarmer.connect();39deviceFarmer.getDevices();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!