Best JavaScript code snippet using storybook-root
actions.js
Source:actions.js
1import {2 CLEAR_LIST,3 FETCH_LIST,4 FETCH_LIST_ERROR,5 FETCH_LIST_SUCCESS,6} from "../actions";7import { list } from "../../API";8/***********************9 * ACTION CREATORS10 ***********************/11export const clearList = (index = null) => {12 return {13 type: CLEAR_LIST,14 payload: {15 selectedIndex: index,16 },17 };18};19export const getList = () => {20 return {21 type: FETCH_LIST,22 };23};24export const getListError = (index, error) => {25 return {26 type: FETCH_LIST_ERROR,27 error,28 payload: {29 selectedIndex: index30 }31 };32};33export const getListSuccess = (index, {list, listByIds}) => {34 return {35 type: FETCH_LIST_SUCCESS,36 payload: {37 selectedIndex: index,38 list,39 listByIds,40 },41 };42};43/***********************44 * API WITH DISPATCH45 ***********************/46const reformatList = (list) => {47 let formattedList = {48 list: {},49 listByIds: [],50 };51 list.map((item) => {52 formattedList = {53 list: {54 ...formattedList.list,55 [item.id_code]: {56 id: item.code,57 value: item.id_code,58 label: item.description59 },60 },61 listByIds: [...formattedList.listByIds, item.id_code],62 };63 });64 return formattedList;65};66export const getBankAsync = () => {67 return (dispatch) => {68 return list69 .getBank()70 .then(({ data: { data, msg: message, status } }) => {71 const { ErrorMsg: errorMessage, ReturnCode: returnCode, banks} = data["Account.Info"];72 if (returnCode == 0 && banks) {73 if (banks.i instanceof Array) {74 let formattedList = {75 list: {},76 listByIds: [],77 }78 banks.i.map((bank) => {79 formattedList = {80 list: {81 ...formattedList.list,82 [bank.code]: {83 id: bank.code,84 value: bank.code,85 label: bank.name86 },87 },88 listByIds: [...formattedList.listByIds, bank.code],89 };90 });91 dispatch(getListSuccess("bank", formattedList));92 }93 } 94 })95 .catch((error) => {96 dispatch(getListError("bank", error));97 });98 };99};100export const getBarangayAsync = (cityCode) => {101 return (dispatch) => {102 return list103 .getBarangay(cityCode)104 .then(({ data: { data: list, msg: message, status } }) => {105 if (status === "ok" && list) {106 if (Array.isArray(list)) {107 const formattedList = reformatList(list);108 dispatch(getListSuccess("barangay", formattedList));109 }110 }111 })112 .catch((error) => {113 dispatch(getListError("barangay", error));114 });115 };116};117export const getCityAsync = (citySearchValue) => {118 return (dispatch) => {119 return list120 .getCity(citySearchValue)121 .then(({ data: { data: list, msg: message, status } }) => {122 if (status === "ok" && list) {123 if (Array.isArray(list)) {124 const formattedList = reformatList(list);125 dispatch(getListSuccess("city", formattedList));126 }127 }128 })129 .catch((error) => {130 dispatch(getListError("city", error));131 });132 };133};134export const getCivilStatusAsync = () => {135 return (dispatch) => {136 return list137 .getCivilStatus()138 .then(({ data: { data: list, msg: message, status } }) => {139 if (status === "ok" && list) {140 if (Array.isArray(list)) {141 const formattedList = reformatList(list);142 dispatch(getListSuccess("civilStatus", formattedList));143 }144 }145 })146 .catch((error) => {147 dispatch(getListError("civilStatus", error));148 });149 };150};151export const getHomeOwnershipAsync = () => {152 return (dispatch) => {153 dispatch(getList());154 return list155 .getHomeOwnership()156 .then(({ data: { data: list, msg: message, status } }) => {157 if (status === "ok" && list) {158 if (Array.isArray(list)) {159 const formattedList = reformatList(list);160 dispatch(getListSuccess("homeOwnership", formattedList));161 }162 }163 })164 .catch((error) => {165 dispatch(getListError("homeOwnership", error));166 });167 };168};169export const getIdListAsync = () => {170 return (dispatch) => {171 dispatch(getList());172 return list173 .getIdList()174 .then(({ data: { data: list, msg: message, status } }) => {175 if (status === "ok" && list) {176 if (Array.isArray(list)) {177 const formattedList = reformatList(list);178 dispatch(getListSuccess("idList", formattedList));179 }180 }181 })182 .catch((error) => {183 dispatch(getListError("idList", error));184 });185 };186};187export const getJobTitleAsync = () => {188 return (dispatch) => {189 dispatch(getList());190 return list191 .getJobTitle()192 .then(({ data: { data: list, msg: message, status } }) => {193 if (status === "ok" && list) {194 if (Array.isArray(list)) {195 const formattedList = reformatList(list);196 dispatch(getListSuccess("jobTitle", formattedList));197 }198 }199 })200 .catch((error) => {201 dispatch(getListError("jobTitle", error));202 });203 };204};205export const getNationalityAsync = () => {206 return (dispatch) => {207 dispatch(getList());208 return list209 .getNationality()210 .then(({ data: { data: list, msg: message, status } }) => {211 if (status === "ok" && list) {212 if (Array.isArray(list)) {213 const formattedList = reformatList(list);214 dispatch(getListSuccess("nationality", formattedList));215 }216 }217 })218 .catch((error) => {219 dispatch(getListError("jobTitle", error));220 });221 };222};223export const getSourceOfFundAsync = () => {224 return (dispatch) => {225 dispatch(getList());226 return list227 .getSourceOfFund()228 .then(({ data: { data: list, msg: message, status } }) => {229 if (status === "ok" && list) {230 if (Array.isArray(list)) {231 const formattedList = reformatList(list);232 dispatch(getListSuccess("sourceOfFund", formattedList));233 }234 }235 })236 .catch((error) => {237 dispatch(getListError("sourceOfFund", error));238 });239 };...
balance.controller.js
Source:balance.controller.js
1const Balance = require('../schemas/balance');2const axios = require('axios');3exports.getById = (req, res) => {4 let balanceId = req.params.balanceId;5 Balance.find(query,(err, data) => {6 if (err) return res.json({ success: false, error: err });7 return res.json({ success: true, data: data })8 })9 .catch(err=>{10 console.log(err)11 return res.json({ success: false, error: err });12 });13};14exports.getAll = (req, res) => {15 let query = {};16 let sort = { createdAt : 1 };17 Balance.find(query).sort(sort).then((data,err) => {18 if (err) return res.json({ success: false, error: err });19 return res.json({ success: true, data: data })20 })21};22exports.getTotalGains = (req, res) => {23 let firstRecords;24 let lastRecords;25 let firstRecordOfEachVault = [26 {27 "$group": {28 "_id": "$priceId",29 "doc": { "$first": "$$ROOT" }30 }31 }32 ];33 Balance.aggregate(firstRecordOfEachVault).then((data,err) => {34 const url = "https://api.coingecko.com/api/v3/simple/price?ids=usd-coin,dai,true-usd,tether,usd-coin,chainlink,yearn-finance,binance-usd,wrapped-bitcoin,ethereum,nusd,chainlink,aave-link,lp-sbtc-curve,lp-bcurve,curve-fi-ydai-yusdc-yusdt-ytusd,lp-3pool-curve,gemini-dollar,curve-dao-token&vs_currencies=usd,eth";35 axios.get(url).then(geckoPrices => {36 firstRecords = data;37 let result = [];38 if (err) return res.json({ success: false, error: err });39 let lastRecordOfEachVault = [40 {41 "$group": {42 "_id": "$priceId",43 "doc": { "$last": "$$ROOT" }44 }45 }46 ];47 Balance.aggregate(lastRecordOfEachVault).then((data,err) => {48 lastRecords = data;49 let difference = {};50 firstRecords.forEach(r => {51 52 difference = {53 priceId: r.doc.priceId,54 underlyingGain: r.doc.underlyingBalance55 };56 console.log(geckoPrices.data[r.doc.priceId])57 if(geckoPrices.data[r.doc.priceId]) difference.usdPriceUnderlying = geckoPrices.data[r.doc.priceId].usd;58 lastRecords.forEach(x =>{59 if(r._id === x._id){60 difference.underlyingGain = x.doc.underlyingBalance - r.doc.underlyingBalance;61 difference.underlyingBalance = x.doc.underlyingBalance;62 difference.underlyingBalanceOriginal = r.doc.underlyingBalance;63 }64 });65 difference.usdGain = difference.underlyingGain * difference.usdPriceUnderlying;66 result.push(difference);67 })68 if (err) return res.json({ success: false, error: err });69 return res.json({ success: true, data: result })70 }).catch(err=>{71 console.log(err)72 throw err;73 }); 74 //return res.json({ success: true, data: data })75 })76 })77};78exports.calculateGains = (req, res) => {79 let query = {};80 let sort = { createdAt : 1 };81 let formattedList = {};82 Balance.find(query).sort(sort).then((data,err) => {83 data.forEach(d=>{84 if(d.priceId && !formattedList[d.priceId]){85 formattedList[d.priceId] = {86 low: undefined,87 high: undefined88 };89 }90 91 if(d.priceId && formattedList[d.priceId]){92 if(!formattedList[d.priceId].low) formattedList[d.priceId].low = d.underlyingBalance;93 if(!formattedList[d.priceId].high) formattedList[d.priceId].high = d.underlyingBalance;94 }95 if(d.underlyingBalance>formattedList[d.priceId].high) formattedList[d.priceId].high = d.underlyingBalance;96 if(d.underlyingBalance<formattedList[d.priceId].low) formattedList[d.priceId].low = d.underlyingBalance;97 })98 Object.keys(formattedList).forEach(key=>{99 let gain = formattedList[key].high - formattedList[key].low;100 formattedList[key].gain = gain;101 })102 if (err) return res.json({ success: false, error: "An error has occurred." });103 return res.json({ success: true, data: formattedList })104 })105};106exports.getLast = (req, res) => {107 let sort = { createdAt : -1 };108 let query = {};109 Balance.findOne(query).sort(sort).then((data,err) => {110 if (err) return res.json({ success: false, error: err });111 return res.json({ success: true, data: data })112 })113 .catch(err=>console.log(err))114};115exports.getFirst = (req, res) => {116 let sort = { createdAt : 1 };117 let query = {};118 Balance.findOne(query).sort(sort).then((data,err) => {119 if (err) return res.json({ success: false, error: err });120 return res.json({ success: true, data: data })121 })122 .catch(err=>console.log(err))123};124exports.addBalance = (req, res) => {125 let b = req.body.balanceId;126 console.log(b)127 if(o.extra && o.extra.done_reason=="canceled"){128 o.status = "canceled";129 }130 let balanceRecord = new BalanceRecord({131 accountAddress: b.accountAddress,132 vaultName: b.vaultName,133 vaultNameFriendly: b.vaultNameFriendly,134 vaultAddress: b.vaultAddress,135 underlyingBalance: b.underlyingBalance,136 yTokenBalance: b.yTokenBalance,137 contractBalance: b.contractBalance,138 pricePerFullShare: b.pricePerFullShare,139 underlyingUsdValue: b.underlyingUsdValue,140 yTokenUsdValue: b.yTokenUsdValue141 })142 balanceRecord.save((err)=>{143 if(err) return res.json({ success: false, error: err });144 return res.json({ 145 success: true,146 message: "Balance written successfully." 147 });148 });...
updateAction.js
Source:updateAction.js
1import axios from 'axios';2import * as SecureStore from 'expo-secure-store';3import AsyncStorage from '@react-native-async-storage/async-storage';4import { BASE_URL } from '../Constants/Api';5const updateAction = async ({ item, action, userId, listId, setData = () => {}, setModalVisible}) => {6 7 //action on a particular item has to be object of personId and 8 //action of either reserved or purchased9 const itemId = item._id;10 const token = await SecureStore.getItemAsync('token')11 12 if (item.actions.personId === '') {13 // console.log(userId)14 // console.log(action)15 try {16 const editedList = await axios.patch(`${BASE_URL}/lists/${listId}/listItem/${itemId}/actions`, {17 personId: userId,18 action: action19 }, {20 headers: {21 Authorization: `Bearer ${token}`22 }23 })24 const formattedList = {25 list: editedList.data.listName,26 id: editedList.data._id,27 data: editedList.data.listItems28 }29 setData(formattedList)30 setModalVisible(false)31 32 33 } catch (e) {34 console.log(e)35 }36 } else if (item.actions.personId === userId && item.actions.action === action) {37 try {38 const editedList = await axios.patch(`${BASE_URL}/lists/${listId}/listItem/${itemId}/actions`, {39 personId: '',40 action: ''41 }, {42 headers: {43 Authorization: `Bearer ${token}`44 }45 })46 const formattedList = {47 list: editedList.data.listName,48 id: editedList.data._id,49 data: editedList.data.listItems50 }51 setData(formattedList)52 setModalVisible(false)53 } catch (e) {54 console.log(e)55 }56 } else if (item.actions.personId === userId && item.actions.action !== action) {57 try {58 const editedList = await axios.patch(`${BASE_URL}/lists/${listId}/listItem/${itemId}/actions`, {59 personId: userId,60 action: action61 }, {62 headers: {63 Authorization: `Bearer ${token}`64 }65 })66 const formattedList = {67 list: editedList.data.listName,68 id: editedList.data._id,69 data: editedList.data.listItems70 }71 setData(formattedList)72 setModalVisible(false)73 } catch (e) {74 console.log(e)75 }76 }77}...
Using AI Code Generation
1import { formattedList } from 'storybook-root';2import { formattedList } from 'storybook-child';3import { formattedList } from 'storybook-root';4import { formattedList } from 'storybook-child';5import { formattedList } from 'storybook-root';6import { formattedList } from 'storybook-child';7import { formattedList } from 'storybook-root';8import { formattedList } from 'storybook-child';9import { formattedList } from 'storybook-root';10import { formattedList } from 'storybook-child';11import { formattedList } from 'storybook-root';12import { formattedList } from 'storybook-child';13import { formattedList } from 'storybook-root';14import { formattedList } from 'storybook-child';15import { formattedList } from 'storybook-root';16import { formattedList } from 'storybook-child';17import { formattedList } from 'storybook-root';18import { formattedList } from 'storybook-child';19import { formattedList } from 'storybook-root';20import { formattedList } from 'storybook-child';
Using AI Code Generation
1import React from 'react';2import { FormattedList } from 'storybook-root';3export default class Test extends React.Component {4 render() {5 return (6 items={['one', 'two', 'three']}7 format={(item, index) => {8 return (9 <div key={index}>10 {item}11 );12 }}13 );14 }15}16import FormattedList from './FormattedList';17export {18};19import React from 'react';20export default class FormattedList extends React.Component {21 render() {22 return (23 {this.props.items.map(this.props.format)}24 );25 }26}27FormattedList.propTypes = {28};29FormattedList.defaultProps = {30 format: (item, index) => {31 return (32 <div key={index}>33 {item}34 );35 }36};37import React from 'react';38import { FormattedList } from 'storybook-root';39export default class App extends React.Component {40 render() {41 return (42 items={['one', 'two', 'three']}43 format={(item, index) => {44 return (45 <div key={index}>46 {item}47 );48 }}49 );50 }51}52import React from 'react';
Using AI Code Generation
1import { formattedList } from 'storybook-root';2import { formattedList } from 'storybook-root';3import { formattedList } from 'storybook-root/src';4import { formattedList } from 'storybook-root/src/utils/formattedList';5import { formattedList } from 'storybook-root/src/utils/formattedList';6import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList';7import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList';8import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList/formattedList';9import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList/formattedList';10import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList/formattedList/formattedList';11import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList/formattedList/formattedList';12import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList/formattedList/formattedList/formattedList';13import { formattedList } from 'storybook-root/src/utils/formattedList/formattedList/formattedList/formattedList/formattedList';14import { formattedList } from 'storybook-root/src/utils/formattedList/formatted
Using AI Code Generation
1import { formattedList } from 'storybook-root';2console.log(formattedList(['a', 'b', 'c']));3import { formattedList } from 'storybook-root';4console.log(formattedList(['a', 'b', 'c']));5import { formattedList } from 'storybook-root';6console.log(formattedList(['a', 'b', 'c']));7import { formattedList } from 'storybook-root';8console.log(formattedList(['a', 'b', 'c']));9import { formattedList } from 'storybook-root';10console.log(formattedList(['a', 'b', 'c']));11import { formattedList } from 'storybook-root';12console.log(formattedList(['a', 'b', 'c']));13import { formattedList } from 'storybook-root';14console.log(formattedList(['a', 'b', 'c']));15import { formattedList } from 'storybook-root';16console.log(formattedList(['a', 'b', 'c']));17import { formattedList } from 'storybook-root';18console.log(formattedList(['a', 'b', 'c']));
Using AI Code Generation
1import { formattedList } from 'storybook-root';2export default function test() {3 const list = [1, 2, 3];4 return <div>{formattedList(list)}</div>;5}6export { formattedList } from './components/formattedList';7export const formattedList = (list) => {8 return list.join(', ');9};10import { formattedList } from './formattedList';11describe('formattedList', () => {12 it('should return formatted list', () => {13 const list = [1, 2, 3];14 const formattedListValue = formattedList(list);15 expect(formattedListValue).toEqual('1, 2, 3');16 });17});
Using AI Code Generation
1import { formattedList } from "storybook-root";2import { formattedList } from "storybook-root";3import { formattedList } from "storybook-root";4import { formattedList } from "storybook-root";5import { formattedList } from "storybook-root";6import { formattedList } from "storybook-root";7import { formattedList } from "storybook-root";8import { formattedList } from "storybook-root";9import { formattedList } from "storybook-root";10import { formattedList } from "storybook-root";11import { formattedList } from "storybook-root";
Using AI Code Generation
1import { formattedList } from 'storybook-root';2console.log(formattedList(['a', 'b', 'c']));3import { configure } from '@storybook/react';4import { setOptions } from '@storybook/addon-options';5import { addDecorator } from '@storybook/react';6import { withInfo } from '@storybook/addon-info';7import { withKnobs } from '@storybook/addon-knobs';8import 'storybook-root';9addDecorator(withKnobs);10setOptions({
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!!