Best JavaScript code snippet using best
SendProjectInformation.js
Source:SendProjectInformation.js
1import React, { Component } from 'react';2import { View, Text, ScrollView, RefreshControl, TouchableOpacity, Alert } from 'react-native';3import { styles } from './css';4import Button from '../../../Components/Elements/Button/Button'5import Checkbox from '../../../Components/Elements/Checkbox/Checkbox';6import { loading } from '../../../Helpers/Functions'7import { blue } from '../../../Components/Elements/Color/Color';8import postSendInfoProject from '../../../Api/postSendInfoProject';9import NetInfo from "@react-native-community/netinfo";10import InternetConnecNotification from '../../InternetConnecNotification/InternetConnecNotification';11import SingleSelect from '../../../Components/Elements/SingleSelect/SingleSelect';12import SingleSelectProject from '../../../Components/Elements/SingleSelect/SingleSelectProject';13import getListEmailCanSend from '../../../Api/getListEmailCanSend';14import { getReduceProjectList } from '../../../Api/getProjectList';15import CustomInput from '../../../Components/Elements/CustomInput/CustomInput';16import { width } from '../../../Components/Elements/Dimensions/Dimensions';17import AsyncStorage from '@react-native-community/async-storage';18import RequestOpenAppAgain from '../../RequestOpenAppAgain/RequestOpenAppAgain';19export default class SendProjectInformation extends Component {20 constructor(props) {21 super(props);22 this.state = {23 loaded: false,24 fromEmail: '',25 listProject: this.props.route.params.listProject,26 token: this.props.route.params.token,27 listEmailCanSend: this.props.route.params.listEmailCanSend,28 isInternetReachable: false,29 refreshScreen: false,30 requestOpenAppAgain: false,31 items: [],32 selectProject: {},33 selectEmail: {},34 chooseProject: false,35 chooseEmail: false,36 };37 this._isMounted = false;38 this.onRefresh = this.onRefresh.bind(this);39 this.returnEmailData = this.returnEmailData.bind(this);40 this.returnProjectData = this.returnProjectData.bind(this);41 this.setChecked = this.setChecked.bind(this);42 this.send = this.send.bind(this);43 }44 componentDidMount() {45 NetInfo.addEventListener(state => {46 this.setState({ isInternetReachable: state.isInternetReachable });47 if(state.isInternetReachable) {48 const items = [];49 if(this.state.listEmailCanSend) {50 if(this.state.listEmailCanSend.length > 0) {51 this.state.listEmailCanSend.map((email, key) => {52 if(email.sent && email.sent < 100) {53 items.push({54 name: email.email,55 id: email.id,56 type: 2,57 sent: email.sent58 })59 }60 else {61 items.push({62 name: email.email,63 id: email.id,64 type: email.sent ? 2 : 1,65 sent: email.sent ? email.sent : null66 })67 }68 }); 69 }70 } 71 this.setState({ items: items })72 }73 });74 this._isMounted = true;75 setTimeout(() => {76 this._isMounted && this.setState({ loaded: true });77 }, 500);78 }79 componentWillUnmount() {80 this._isMounted = false;81 }82 setValue = (name, value) => {83 this.setState({ [name]: value }, () => { })84 }85 returnEmailData(selectEmail) { 86 this.setState({ selectEmail })87 }88 returnProjectData(selectProject) { 89 this.setState({ 90 selectProject,91 chooseProject: true 92 })93 }94 async send() {95 this.setState({ loaded: false });96 if(Object.keys(this.state.selectEmail).length == 0) {97 this.setState({ loaded: true });98 Alert.alert('Thông báo', 'Vui lòng chá»n email')99 }100 else if(Object.keys(this.state.selectProject).length == 0) {101 this.setState({ loaded: true });102 Alert.alert('Thông báo', 'Vui lòng chá»n dá»± án')103 }104 else {105 var file_contract = 0;106 if(this.state.file_contract == true) {107 file_contract = 1;108 } else {109 file_contract = 0;110 }111 var file_design = 0;112 if(this.state.file_design == true) {113 file_design = 1;114 } else {115 file_design = 0;116 }117 var file_marketing = 0;118 if(this.state.file_marketing == true) {119 file_marketing = 1;120 } else {121 file_marketing = 0;122 }123 const res = await postSendInfoProject(124 this.state.token,125 this.state.selectProject.id,126 this.state.selectEmail.id,127 file_contract,128 file_design,129 file_marketing,130 this.state.selectEmail.type,131 this.props.route.params.customer.id132 );133 134 setTimeout(async() => { 135 this.setState({ loaded: true });136 if(res.status == 200) {137 const resJson = await res.json();138 if(resJson.code == 200) {139 this.props.route.params.refreshList();140 this.childEmail.refreshAfterCreate();141 this.childProject.refreshAfterCreate();142 Alert.alert(143 'Thông báo', 144 resJson.message, 145 [146 {text: 'OK', onPress: () => {147 this.props.route.params.navigation.goBack()148 }}149 ]150 ); 151 } 152 else if(resJson.code == 204) {153 Alert.alert('Error !!!', resJson.message)154 }155 else if(resJson.errors) {156 console.log(resJson) 157 }158 } 159 else if(res.status == 401) {160 Alert.alert('Thông báo', 'Phiên là m viá»c của bạn Äã hết hạn. Vui lòng ÄÄng nháºp lại !!!');161 AsyncStorage162 .clear()163 .then(() => this.props.route.params.navigation.navigate('UserNavigation', {screen: 'Login'}))164 }165 else if(res.status == 500) {166 this.setState({ requestOpenAppAgain: true });167 Alert.alert('Error !!!', 'Yêu cầu không thá» thá»±c hiá»n. Vui lòng khá»i Äá»ng lại app !!!');168 } 169 }, 1000);170 } 171 }172 setChecked = (name, value) => {173 this.setState({ [name]: value }, () => {})174 }175 async refreshListEmail() {176 const items = [];177 const res = await getListEmailCanSend(this.state.token);178 if(res.status == 200) {179 const resJson = await res.json();180 if(resJson.code == 200) {181 resJson.data.map((email, key) => {182 if(resJson.data.length > 0) {183 if(email.sent && email.sent < 100) {184 items.push({185 name: email.email,186 id: email.id,187 type: 2,188 sent: email.sent189 })190 }191 else {192 items.push({193 name: email.email,194 id: email.id,195 type: email.sent ? 2 : 1,196 sent: email.sent ? email.sent : null197 })198 }199 }200 });201 this.setState({ items });202 } 203 else if(resJson.code == 204) {204 console.log('Error !!!', resJson.message)205 }206 else if(resJson.errors) {207 console.log(resJson) 208 }209 this.childEmail.refreshListItems(items);210 }211 else if(res.status == 401) {212 Alert.alert('Thông báo', 'Phiên là m viá»c của bạn Äã hết hạn. Vui lòng ÄÄng nháºp lại !!!');213 AsyncStorage214 .clear()215 .then(() => this.props.route.params.navigation.navigate('UserNavigation', {screen: 'Login'}))216 }217 else if(res.status == 500) {218 this.setState({ requestOpenAppAgain: true })219 Alert.alert('Error !!!', 'Yêu cầu không thá» thá»±c hiá»n. Vui lòng khá»i Äá»ng lại app !!!');220 } 221 }222 async refreshReduceProjectList() {223 const res = await getReduceProjectList(this.state.token);224 if(res.status == 200) {225 const resJson = await res.json();226 if(resJson.code == 200) {227 this.childProject.refreshListItems(resJson.data.data);228 this.setState({ listProject: resJson.data.data });229 } 230 else if(resJson.code == 204) {231 console.log('Error !!!', resJson.message)232 }233 else if(resJson.errors) {234 console.log(resJson) 235 }236 }237 else if(res.status == 401) {238 Alert.alert('Thông báo', 'Phiên là m viá»c của bạn Äã hết hạn. Vui lòng ÄÄng nháºp lại !!!');239 AsyncStorage240 .clear()241 .then(() => this.props.route.params.navigation.navigate('UserNavigation', {screen: 'Login'}))242 }243 else if(res.status == 500) {244 this.setState({ requestOpenAppAgain: true })245 Alert.alert('Error !!!', 'Yêu cầu không thá» thá»±c hiá»n. Vui lòng khá»i Äá»ng lại app !!!');246 } 247 }248 wait(timeout) {249 return new Promise(resolve => {250 setTimeout(resolve, timeout);251 });252 }253 async onRefresh() {254 this.refreshListEmail();255 this.refreshReduceProjectList();256 this.setState({ refreshScreen: true });257 this.wait(1000).then(() => this.setState({ refreshScreen: false }))258 }259 render() {260 return (261 <View>262 { !this.state.loaded ? loading() : null }263 <View style={ styles.container }>264 {this.state.isInternetReachable ?265 (this.state.requestOpenAppAgain ? 266 <View style={{ marginTop: -70 }}>267 <RequestOpenAppAgain/> 268 </View> 269 :270 <ScrollView271 refreshControl={272 <RefreshControl refreshing={this.state.refreshScreen} onRefresh={this.onRefresh} />273 }274 style={{ width: width }}275 > 276 <View style={{ alignItems: 'center' }}>277 <View style={styles.firstInput}>278 <CustomInput279 label='Tên khách hà ng'280 oldValue={this.props.route.params.customer.fullname}281 editable={false}282 setValue={this.setValue}283 width={width/1.16 - 20}284 marginLeft={10}285 />286 </View>287 <View style={styles.input}>288 <CustomInput289 label='Email'290 oldValue={this.props.route.params.customer.email}291 editable={false}292 setValue={this.setValue}293 width={width/1.16 - 10}294 marginLeft={10}295 />296 </View>297 <View style={styles.picker}>298 <Text style={styles.label}>Chá»n Email</Text>299 <SingleSelect300 selectText="Chá»n email..."301 placeholder='Tìm email...'302 items={this.state.items}303 returnData={this.returnEmailData}304 onRef={ref => (this.childEmail = ref)}305 />306 </View>307 <View style={styles.picker}>308 <Text style={styles.label}>Chá»n dá»± án</Text>309 <SingleSelectProject310 selectText="Chá»n dá»± án..."311 placeholder='Tìm dá»± án...'312 items={this.state.listProject}313 returnData={this.returnProjectData}314 token={this.state.token}315 onRef={ref => (this.childProject = ref)}316 />317 </View>318 <View style={styles.checkboxWrapper}>319 { this.state.selectProject.data == null ||320 Object.keys(this.state.selectProject).length == 0 || 321 (Object.keys(this.state.selectProject).length > 0 &&322 (!this.state.selectProject.data.file_contract || this.state.selectProject.data.file_contract.length == 0) && 323 (!this.state.selectProject.data.file_design || this.state.selectProject.data.file_design == 0) &&324 (!this.state.selectProject.data.file_marketing || this.state.selectProject.data.file_marketing == 0)) ? null : <Text style={styles.checkboxTitle}>Loại tà i liá»u gá»i</Text> }325 { this.state.selectProject.data == null ? null :326 Object.keys(this.state.selectProject).length > 0 && this.state.selectProject.data.file_contract ? 327 this.state.selectProject.data.file_contract.length > 0 ?328 <View style={styles.checkbox}>329 <Checkbox330 text='Tên dá»± án'331 setChecked={this.setChecked}332 name='file_contract'333 checked={false}334 />335 </View> : null : null336 }337 338 { this.state.selectProject.data == null ? null : 339 Object.keys(this.state.selectProject).length > 0 && this.state.selectProject.data.file_design ? 340 this.state.selectProject.data.file_design.length > 0 ?341 <View style={styles.checkbox}>342 <Checkbox343 text='Bảng giá'344 setChecked={this.setChecked}345 name='file_design'346 checked={false}347 />348 </View> : null : null349 }350 351 { this.state.selectProject.data == null ? null : 352 Object.keys(this.state.selectProject).length > 0 && this.state.selectProject.data.file_marketing ? 353 this.state.selectProject.data.file_marketing.length > 0 ?354 <View style={styles.checkbox}>355 <Checkbox356 text='ChÃnh sách bán hà ng'357 setChecked={this.setChecked}358 name='file_marketing'359 checked={false}360 />361 </View> : null : null362 }363 364 </View>365 <View style={styles.button}>366 <Button367 label='Gá»i tà i liá»u dá»± án cho khách hà ng'368 color={blue}369 action={this.send}370 />371 </View>372 </View>373 </ScrollView>)374 :375 <InternetConnecNotification/>376 }377 </View>378 </View>379 );380 }...
ViewProject.js
Source:ViewProject.js
1import React, { useState, useEffect, Fragment } from "react";2import { connect } from "react-redux";3import { Row, Col, Button, Collapse } from "reactstrap";4import {5 getSelectProject,6 editProject,7} from "../../redux/actions/projects/projects.action";8import { getEmpList } from "../../redux/actions/employee/employee.action";9import { getSkill } from "../../redux/actions/adminSettings/adminSettings.action";10import {11 DescpProject,12 UploadedFilesProject,13 TaskTabsProject,14 DetailsProject,15 LeadersPoject,16 TeamProject,17 TechnologiesProject,18 AddEditFormProject,19} from "../../components/projects/index";20import api from "../../apis/api";21const ViewProject = (props) => {22 let projectId = props.match.params.projectId;23 const { getSelectProject, getEmpList, getSkill, editProject } = props;24 const { skillList } = props.skillList;25 const { selectProject } = props.selectProject;26 const { empList } = props.empList;27 const [isOpenEditForm, setIsOpenEditForm] = useState(false);28 const [projectDetails, setProjectDetails] = useState({});29 useEffect(() => {30 const fetchApi = async () => {31 let tempArr = await api.project().getProjectById(projectId);32 console.log(tempArr.data);33 let selectedProject = tempArr.data[0];34 setProjectDetails(selectedProject);35 };36 fetchApi();37 }, []);38 useEffect(() => {39 getSelectProject(projectId);40 getEmpList();41 getSkill();42 }, [getSelectProject, getEmpList, projectId, getSkill]);43 const toggleForm = React.useCallback(() => {44 setIsOpenEditForm((prevState) => !prevState);45 }, [setIsOpenEditForm]);46 // handle click in AddEditFormProject.js 'update'.47 const handleUpdateProject = React.useCallback(48 (projectData) => {49 console.log(projectData);50 // will get the selected project id from the selectedProject.51 editProject(projectData);52 },53 [editProject]54 );55 return selectProject ? (56 <Fragment>57 {console.log(projectDetails)}58 <Row className="mb-4">59 <Col xs={12} sm={9} md={9} lg={9}>60 <h4>{projectDetails.projectName}</h4>61 </Col>62 <Col xs={12} sm={3} md={3} lg={3}>63 <Button64 color=""65 className="btn-admin-settings w-100"66 onClick={toggleForm}67 >68 Edit Project69 </Button>{" "}70 </Col>71 </Row>72 <Collapse isOpen={isOpenEditForm}>73 <AddEditFormProject74 empList={empList}75 skillList={skillList}76 selectedProject={selectProject}77 // selectedProject={projectDetails}78 handleUpdateProject={handleUpdateProject}79 toogleFromProjectAddEditForm={toggleForm}80 ></AddEditFormProject>81 </Collapse>82 <Collapse isOpen={!isOpenEditForm}>83 <Row className="project-box">84 <Col xs={12} sm={9} md={9} lg={9}>85 <DescpProject86 projectName={projectDetails.projectName}87 projectDescp={projectDetails.projectDescription}88 // projectDescp={selectProject.projectDescription}89 ></DescpProject>90 <UploadedFilesProject></UploadedFilesProject>91 <TaskTabsProject></TaskTabsProject>92 </Col>93 <Col xs={12} sm={3} md={3} lg={3}>94 <DetailsProject projectDetails={projectDetails}></DetailsProject>95 <TechnologiesProject96 technologies={selectProject.projectTechnologyList}97 ></TechnologiesProject>98 <LeadersPoject99 leaders={{100 managerId: selectProject.managerID,101 managerName: selectProject.managerName,102 managerPicture: selectProject.managerPicture,103 }}104 empList={empList}105 ></LeadersPoject>106 <TeamProject107 team={selectProject.projectMembersList}108 empList={empList}109 ></TeamProject>110 </Col>111 </Row>112 </Collapse>113 </Fragment>114 ) : null;115};116const mapStateToProps = (state) => ({117 selectProject: state.projectReducer,118 empList: state.empReducer,119 skillList: state.adminSettingReducer,120});121export default connect(mapStateToProps, {122 getSelectProject,123 editProject,124 getEmpList,125 getSkill,...
newTask.test.js
Source:newTask.test.js
1/* eslint-disable camelcase */2"use strict";3const $ = require(`jquery`);4const newTask = require(`../public/js/newTask.js`);5describe(`addProjectsToOptions`, () => {6 it(`should not create child element for undefined input`, () => {7 const selectProject = $(`<select>`);8 newTask.addProjectsToOptions(undefined, selectProject);9 expect(selectProject.children().length).toEqual(0);10 });11 it(`should not create child element for empty array input`, () => {12 const selectProject = $(`<select>`);13 newTask.addProjectsToOptions([], selectProject);14 expect(selectProject.children().length).toEqual(0);15 });16 it(`should set inner text to expected name on child option element`, () => {17 const expectedName = `name`;18 const arr = [19 {20 projectId: 1,21 teamId: 1,22 name: expectedName23 }24 ];25 const selectProject = $(`<select>`);26 newTask.addProjectsToOptions(arr, selectProject);27 expect(28 selectProject29 .children()30 .eq(0)31 .text()32 ).toEqual(expectedName);33 });34 it(`should set data-project-id to expected ID on child option element`, () => {35 const expectedId = 2;36 const arr = [37 {38 id: expectedId,39 team_id: 1,40 name: `name`41 }42 ];43 const selectProject = $(`<select>`);44 newTask.addProjectsToOptions(arr, selectProject);45 expect(46 selectProject47 .children()48 .eq(0)49 .data(`project-id`)50 ).toEqual(expectedId);51 });52 it(`should set data-team-id to expected ID on child option element`, () => {53 const expectedTeamId = 4;54 const arr = [55 {56 id: 3,57 // eslint-disable-next-line camelcase58 team_id: expectedTeamId,59 name: `name`60 }61 ];62 const selectProject = $(`<select>`);63 newTask.addProjectsToOptions(arr, selectProject);64 expect(65 selectProject66 .children()67 .eq(0)68 .data(`team-id`)69 ).toEqual(expectedTeamId);70 });71 it(`should have two options when two items passed in arr`, () => {72 const arr = [73 {74 id: 1,75 team_id: 1,76 name: `name1`77 },78 {79 id: 2,80 team_id: 2,81 name: `name2`82 }83 ];84 const selectProject = $(`<select>`);85 newTask.addProjectsToOptions(arr, selectProject);86 expect(selectProject.children().length).toEqual(2);87 });88});89describe(`addUsersToOptions`, () => {90 it(`should not create child element for undefined input`, () => {91 const selectUser = $(`<select>`);92 newTask.addUsersToOptions(undefined, selectUser);93 expect(selectUser.children().length).toEqual(0);94 });95 it(`should not create child element for empty array input`, () => {96 const selectUser = $(`<select>`);97 newTask.addUsersToOptions([], selectUser);98 expect(selectUser.children().length).toEqual(0);99 });100 it(`should set inner text to expected name on child option element`, () => {101 const expectedName = `name`;102 const arr = [103 {104 user_id: 1,105 user_name: expectedName106 }107 ];108 const selectUser = $(`<select>`);109 newTask.addUsersToOptions(arr, selectUser);110 expect(111 selectUser112 .children()113 .eq(0)114 .text()115 ).toEqual(expectedName);116 });117 it(`should set data-user-id to expected ID on child option element`, () => {118 const expectedId = 1;119 const arr = [120 {121 user_id: expectedId,122 user_name: `name`123 }124 ];125 const selectUser = $(`<select>`);126 newTask.addUsersToOptions(arr, selectUser);127 expect(128 selectUser129 .children()130 .eq(0)131 .data(`user-id`)132 ).toEqual(expectedId);133 });134 it(`should have two options when two items passed in arr`, () => {135 const arr = [136 {137 user_id: 1,138 user_name: `name1`139 },140 {141 user_id: 2,142 user_name: `name2`143 }144 ];145 const selectUser = $(`<select>`);146 newTask.addUsersToOptions(arr, selectUser);147 expect(selectUser.children().length).toEqual(2);148 });...
Using AI Code Generation
1var BestProject = require('./BestProject');2var bestProject = new BestProject();3var project = bestProject.selectProject();4console.log(project);5module.exports = function BestProject() {6 this.selectProject = function() {7 return "Selected project";8 }9}10var fs = require("fs");11var data = fs.readFileSync('test.txt');12console.log(data.toString());13console.log("Program Ended");14var fs = require("fs");15fs.readFile('test.txt', function (err, data) {16 if (err) return console.error(err);17 console.log(data.toString());18});19console.log("Program Ended");20var fs = require("fs");21fs.readFile('test.txt', function (err, data) {22 if (err) return console.error(err);23 console.log(data.toString());24});25console.log("Program Ended");26The fs.readFile() function is used to read the file test.txt
Using AI Code Generation
1var BestProject = require('./BestProject');2var bestProject = new BestProject();3var project = bestProject.selectProject();4console.log(project);5var BestProject = function() {6 this.projects = ["Project 1", "Project 2", "Project 3"];7};8BestProject.prototype.selectProject = function() {9 var index = Math.floor(Math.random() * this.projects.length);10 return this.projects[index];11};12module.exports = BestProject;13var BestProject = require('./BestProject');14var bestProject = new BestProject();15var project = bestProject.selectProject();16console.log(project);17var BestProject = require('./BestProject');18var bestProject = new BestProject();19var project = bestProject.selectProject();20console.log(project);
Using AI Code Generation
1var BestProjectSelector = require(./bestProjectSelector.js);2var projectSelector = new BestProjectSelector();3var project = projectSelector.selectProject();4var BestProjectSelector = function() {5 this.selectProject = function() {6 return bestProject;7 }8}9module.exports = BestProjectSelector;10We can also import a file that is not in the same folder as the file that is importing it. For example, if we have a folder named utils that contains the bestProjectSelector.js file, we can import it in the test.js file using the following statement:11var BestProjectSelector = require(./utils/bestProjectSelector.js);12We can use the require() function to import
Using AI Code Generation
1var BestPractices = require("./BestPractices");2var bp = new BestPractices();3bp.selectProject("project1");4module.exports = BestPractices;5function BestPractices() {6 this.selectProject = function (projectName) {7 }8}
Using AI Code Generation
1var BestPractices = require('./BestPractices.js');2var bestPractices = new BestPractices();3var project = bestPractices.selectProject('Java');4console.log(project);5var Project = require('./Project.js');6function BestPractices() {7 this.selectProject = function (language) {8 var project = new Project();9 project.language = language;10 return project;11 }12}13module.exports = BestPractices;14function Project() {15 this.language = '';16}17module.exports = Project;18Project { language: 'Java' }
Using AI Code Generation
1var BestBuyApi = require('./BestBuyApi');2var api = new BestBuyApi();3api.selectProject('1234').then(function(result) {4 console.log(result);5});6var request = require('request');7var BestBuyApi = function() {8 this.selectProject = function(projectId) {9 return new Promise(function(resolve, reject) {10 request(url, function(err, response, body) {11 if (err) {12 reject(err);13 } else {14 resolve(body);15 }16 });17 });18 };19};20module.exports = BestBuyApi;
Using AI Code Generation
1var request = require('request');2var config = require('./config');3var async = require('async');4var fs = require('fs');5var readline = require('readline');6var http = require('http');7var https = require('https');8var zlib = require('zlib');9var url = require('url');10var path = require('path');11var csv = require('fast-csv');12var csv = require('csv');13var csv = require('csvtojson');
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!!