diff --git a/client/src/app/projects/preview/mock-response.ts b/client/src/app/projects/preview/mock-response.ts deleted file mode 100644 index b7fa1834..00000000 --- a/client/src/app/projects/preview/mock-response.ts +++ /dev/null @@ -1,932 +0,0 @@ -import { ACTIVITY } from "@shared/entities/activity.enum"; -import { Country } from "@shared/entities/country.entity"; -import { - CARBON_REVENUES_TO_COVER, - CustomProject as CustomProjectEntity, -} from "@shared/entities/custom-project.entity"; -import { ECOSYSTEM } from "@shared/entities/ecosystem.enum"; - -const mockResponse: { data: InstanceType } = { - data: { - projectName: "My custom project", - country: { - code: "IND", - name: "India", - } as Country, - totalCostNPV: 2503854.27918858, - totalCost: 3332201.598883546, - projectSize: 1000, - projectLength: 20, - ecosystem: "Mangrove" as ECOSYSTEM, - activity: "Conservation" as ACTIVITY, - output: { - lossRate: -0.0016, - carbonRevenuesToCover: "Opex" as CARBON_REVENUES_TO_COVER, - initialCarbonPrice: 1000, - emissionFactors: { - emissionFactor: null, - emissionFactorAgb: 67.7, - emissionFactorSoc: 85.5, - }, - totalProjectCost: { - total: { - total: 3332201.598883546, - capex: 1600616.6666666667, - opex: 1731584.9322168794, - }, - npv: { - total: 2503854.27918858, - capex: 1505525.2721514185, - opex: 998329.0070371614, - }, - }, - leftover: { - total: { - total: 30600000, - leftover: 4100000, - opex: 26500000, - }, - npv: { - total: 40600000, - leftover: 4106132, - opex: 36500000, - }, - }, - summary: { - "$/tCO2e (total cost, NPV)": 61.470410294099835, - "$/ha": 2503.8542791885798, - "NPV covering cost": -493830.3621939037, - "Leftover after OpEx / total cost": null, - "IRR when priced to cover OpEx": 0.0560813585617166, - "IRR when priced to cover total cost": 85683156958.8259, - "Total cost (NPV)": 2503854.27918858, - "Capital expenditure (NPV)": 1505525.2721514185, - "Operating expenditure (NPV)": 998329.0070371614, - "Credits issued": 26038.815407423757, - "Total revenue (NPV)": 504498.6448432577, - "Total revenue (non-discounted)": 956754.3382707891, - "Financing cost": 80030.83333333334, - "Funding gap": 493830.3621939037, - "Funding gap (NPV)": 493830.3621939037, - "Funding gap per tCO2e (NPV)": 18.965162372675024, - "Community benefit sharing fund": 0.5, - }, - costDetails: { - total: { - capitalExpenditure: 1600616.6666666667, - operationalExpenditure: 1731584.9322168794, - totalCost: 3201233.3333333335, - feasibilityAnalysis: 50000, - conservationPlanningAndAdmin: 667066.6666666666, - dataCollectionAndFieldCost: 80000, - communityRepresentation: 213550, - blueCarbonProjectPlanning: 300000, - establishingCarbonRights: 140000, - validation: 50000, - implementationLabor: 100000, - monitoring: 300000, - maintenance: 0, - communityBenefitSharingFund: 478377.16913539456, - carbonStandardFees: 5207.763081484753, - baselineReassessment: 120000, - mrv: 300000, - longTermProjectOperatingCost: 528000, - }, - npv: { - capitalExpenditure: 1505525.2721514185, - operationalExpenditure: 998329.0070371614, - totalCost: 2503854.27918858, - feasibilityAnalysis: 50000, - conservationPlanningAndAdmin: 629559.3479745106, - dataCollectionAndFieldCost: 76962.52465483235, - communityRepresentation: 197540.230048551, - blueCarbonProjectPlanning: 288609.4674556213, - establishingCarbonRights: 129504.24821726595, - validation: 44449.81793354574, - implementationLabor: 88899.63586709148, - monitoring: 181226.25950738514, - maintenance: 0, - communityBenefitSharingFund: 252249.32242162884, - carbonStandardFees: 2786.931340270489, - baselineReassessment: 75811.8711249392, - mrv: 167296.40590994002, - longTermProjectOperatingCost: 318958.2167329978, - }, - }, - yearlyBreakdown: [ - { - costName: "feasibilityAnalysis", - totalCost: -50000, - totalNPV: -50000, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": -50000, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "conservationPlanningAndAdmin", - totalCost: -667066.6666666666, - totalNPV: -629559.3479745106, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": -166766.66666666666, - "-3": -166766.66666666666, - "-2": -166766.66666666666, - "-1": -166766.66666666666, - }, - }, - { - costName: "dataCollectionAndFieldCost", - totalCost: -80000, - totalNPV: -76962.52465483235, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": -26666.666666666668, - "-3": -26666.666666666668, - "-2": -26666.666666666668, - "-1": 0, - }, - }, - { - costName: "blueCarbonProjectPlanning", - totalCost: -300000, - totalNPV: -288609.4674556213, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": -100000, - "-3": -100000, - "-2": -100000, - "-1": 0, - }, - }, - { - costName: "communityRepresentation", - totalCost: -213550, - totalNPV: -197540.230048551, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": 0, - "-3": -71183.33333333333, - "-2": -71183.33333333333, - "-1": -71183.33333333333, - }, - }, - { - costName: "establishingCarbonRights", - totalCost: -140000, - totalNPV: -129504.24821726595, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": 0, - "-3": -46666.666666666664, - "-2": -46666.666666666664, - "-1": -46666.666666666664, - }, - }, - { - costName: "validation", - totalCost: -50000, - totalNPV: -44449.81793354574, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": -50000, - }, - }, - { - costName: "implementationLabor", - totalCost: -100000, - totalNPV: -88899.63586709148, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": -100000, - }, - }, - { - costName: "monitoring", - totalCost: -300000, - totalNPV: -181226.25950738514, - costValues: { - "0": 0, - "1": -15000, - "2": -15000, - "3": -15000, - "4": -15000, - "5": -15000, - "6": -15000, - "7": -15000, - "8": -15000, - "9": -15000, - "10": -15000, - "11": -15000, - "12": -15000, - "13": -15000, - "14": -15000, - "15": -15000, - "16": -15000, - "17": -15000, - "18": -15000, - "19": -15000, - "20": -15000, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "maintenance", - totalCost: 0, - totalNPV: 0, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "communityBenefitSharingFund", - totalCost: -478377.16913539456, - totalNPV: -252249.32242162884, - costValues: { - "0": 0, - "1": -3101.320320000044, - "2": -4951.5160414005795, - "3": -6853.590667682226, - "4": -8808.645091381013, - "5": -10817.801034974973, - "6": -12882.20142107311, - "7": -15003.01074892755, - "8": -17181.41547737641, - "9": -19418.624414322498, - "10": -21715.869112862427, - "11": -24074.40427416561, - "12": -26495.508157226817, - "13": -28980.482995600523, - "14": -31530.655421230902, - "15": -34147.37689550309, - "16": -36832.024147625285, - "17": -39585.99962047368, - "18": -42410.73192401404, - "19": -45307.67629643649, - "20": -48278.31507311732, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "carbonStandardFees", - totalCost: -5207.763081484753, - totalNPV: -2786.931340270489, - costValues: { - "0": 0, - "1": -40.739840000000584, - "2": -64.08329625600338, - "3": -87.38940298199216, - "4": -110.65821993722336, - "5": -133.88980678532448, - "6": -157.08422309446732, - "7": -180.24152833751594, - "8": -203.3617818921798, - "9": -226.44504304114957, - "10": -249.49137097228606, - "11": -272.5008247787324, - "12": -295.473463459085, - "13": -318.4093459175526, - "14": -341.3085309640828, - "15": -364.17107731454234, - "16": -386.9970435908389, - "17": -409.7864883210964, - "18": -432.53946993977934, - "19": -455.25604678787926, - "20": -477.93627711301974, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "baselineReassessment", - totalCost: -120000, - totalNPV: -75811.8711249392, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": -40000, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": -40000, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": -40000, - }, - }, - { - costName: "mrv", - totalCost: -300000, - totalNPV: -167296.40590994002, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": -75000, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": -75000, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": -75000, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": -75000, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "longTermProjectOperatingCost", - totalCost: -528000, - totalNPV: -318958.2167329978, - costValues: { - "0": 0, - "1": -26400, - "2": -26400, - "3": -26400, - "4": -26400, - "5": -26400, - "6": -26400, - "7": -26400, - "8": -26400, - "9": -26400, - "10": -26400, - "11": -26400, - "12": -26400, - "13": -26400, - "14": -26400, - "15": -26400, - "16": -26400, - "17": -26400, - "18": -26400, - "19": -26400, - "20": -26400, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "opexTotalCostPlan", - totalCost: -1731584.9322168794, - totalNPV: -998329.0070371614, - costValues: { - "0": 0, - "1": -44542.060160000045, - "2": -46415.59933765658, - "3": -48340.98007066422, - "4": -50319.30331131823, - "5": -127351.6908417603, - "6": -54439.28564416758, - "7": -56583.25227726507, - "8": -58784.777259268594, - "9": -61045.06945736365, - "10": -178365.36048383472, - "11": -65746.90509894435, - "12": -68190.9816206859, - "13": -70698.89234151808, - "14": -73271.96395219499, - "15": -150911.5479728176, - "16": -78619.02119121613, - "17": -81395.78610879477, - "18": -84243.27139395382, - "19": -87162.93234322437, - "20": -205156.25135023033, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": -40000, - }, - }, - { - costName: "capexTotalCostPlan", - totalCost: -1600616.6666666667, - totalNPV: -1505525.2721514185, - costValues: { - "0": 0, - "1": 0, - "2": 0, - "3": 0, - "4": 0, - "5": 0, - "6": 0, - "7": 0, - "8": 0, - "9": 0, - "10": 0, - "11": 0, - "12": 0, - "13": 0, - "14": 0, - "15": 0, - "16": 0, - "17": 0, - "18": 0, - "19": 0, - "20": 0, - "-4": -343433.3333333333, - "-3": -411283.3333333333, - "-2": -411283.3333333333, - "-1": -434616.6666666667, - }, - }, - { - costName: "totalCostPlan", - totalCost: -3332201.598883546, - totalNPV: -2503854.27918858, - costValues: { - "0": 0, - "1": -44542.060160000045, - "2": -46415.59933765658, - "3": -48340.98007066422, - "4": -50319.30331131823, - "5": -127351.6908417603, - "6": -54439.28564416758, - "7": -56583.25227726507, - "8": -58784.777259268594, - "9": -61045.06945736365, - "10": -178365.36048383472, - "11": -65746.90509894435, - "12": -68190.9816206859, - "13": -70698.89234151808, - "14": -73271.96395219499, - "15": -150911.5479728176, - "16": -78619.02119121613, - "17": -81395.78610879477, - "18": -84243.27139395382, - "19": -87162.93234322437, - "20": -205156.25135023033, - "-4": -343433.3333333333, - "-3": -411283.3333333333, - "-2": -411283.3333333333, - "-1": -474616.6666666667, - }, - }, - { - costName: "estimatedRevenuePlan", - totalCost: 956754.3382707891, - totalNPV: 504498.6448432577, - costValues: { - "0": 0, - "1": 6202.640640000088, - "2": 9903.032082801159, - "3": 13707.181335364452, - "4": 17617.290182762026, - "5": 21635.602069949946, - "6": 25764.40284214622, - "7": 30006.0214978551, - "8": 34362.83095475282, - "9": 38837.248828644995, - "10": 43431.738225724854, - "11": 48148.80854833122, - "12": 52991.016314453635, - "13": 57960.96599120105, - "14": 63061.310842461804, - "15": 68294.75379100618, - "16": 73664.04829525057, - "17": 79171.99924094736, - "18": 84821.46384802808, - "19": 90615.35259287298, - "20": 96556.63014623465, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "creditsIssuedPlan", - totalCost: 26038.815407423757, - totalNPV: 13934.656701352445, - costValues: { - "0": 0, - "1": 203.6992000000029, - "2": 320.4164812800169, - "3": 436.9470149099608, - "4": 553.2910996861168, - "5": 669.4490339266224, - "6": 785.4211154723365, - "7": 901.2076416875797, - "8": 1016.808909460899, - "9": 1132.2252152057479, - "10": 1247.4568548614302, - "11": 1362.504123893662, - "12": 1477.367317295425, - "13": 1592.046729587763, - "14": 1706.542654820414, - "15": 1820.8553865727117, - "16": 1934.9852179541945, - "17": 2048.932441605482, - "18": 2162.6973496988967, - "19": 2276.280233939396, - "20": 2389.6813855650985, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 0, - }, - }, - { - costName: "cumulativeNetIncomePlan", - totalCost: 817876.6499772413, - totalNPV: 528307.1257344105, - costValues: { - "0": 0, - "1": 35559.85434683659, - "2": 35559.85434683659, - "3": 35559.85434683659, - "4": 35559.85434683659, - "5": 35559.85434683659, - "6": 35559.85434683659, - "7": 35559.85434683659, - "8": 35559.85434683659, - "9": 35559.85434683659, - "10": 35559.85434683659, - "11": 35559.85434683659, - "12": 35559.85434683659, - "13": 35559.85434683659, - "14": 35559.85434683659, - "15": 35559.85434683659, - "16": 35559.85434683659, - "17": 35559.85434683659, - "18": 35559.85434683659, - "19": 35559.85434683659, - "20": 35559.85434683659, - "-4": 0, - "-3": 35559.85434683659, - "-2": 35559.85434683659, - "-1": 35559.85434683659, - }, - }, - { - costName: "cumulativeNetIncomeCapexOpex", - totalCost: 43687357.90945987, - totalNPV: 28341425.709689442, - costValues: { - "0": 0, - "1": 1884518.4598315884, - "2": 1884518.4598315884, - "3": 1884518.4598315884, - "4": 1884518.4598315884, - "5": 1884518.4598315884, - "6": 1884518.4598315884, - "7": 1884518.4598315884, - "8": 1884518.4598315884, - "9": 1884518.4598315884, - "10": 1884518.4598315884, - "11": 1884518.4598315884, - "12": 1884518.4598315884, - "13": 1884518.4598315884, - "14": 1884518.4598315884, - "15": 1884518.4598315884, - "16": 1884518.4598315884, - "17": 1884518.4598315884, - "18": 1884518.4598315884, - "19": 1884518.4598315884, - "20": 1884518.4598315884, - "-4": 343433.3333333333, - "-3": 1884518.4598315884, - "-2": 1884518.4598315884, - "-1": 1884518.4598315884, - }, - }, - { - costName: "annualNetCashFlow", - totalCost: 4288955.937154336, - totalNPV: 3008352.9240318374, - costValues: { - "0": 0, - "1": 50744.70080000013, - "2": 56318.63142045774, - "3": 62048.161406028674, - "4": 67936.59349408025, - "5": 148987.29291171025, - "6": 80203.6884863138, - "7": 86589.27377512018, - "8": 93147.60821402141, - "9": 99882.31828600864, - "10": 221797.0987095596, - "11": 113895.71364727557, - "12": 121181.99793513953, - "13": 128659.85833271913, - "14": 136333.27479465678, - "15": 219206.30176382378, - "16": 152283.0694864667, - "17": 160567.78534974213, - "18": 169064.73524198192, - "19": 177778.28493609733, - "20": 301712.88149646495, - "-4": 343433.3333333333, - "-3": 411283.3333333333, - "-2": 411283.3333333333, - "-1": 474616.6666666667, - }, - }, - { - costName: "annualNetIncome", - totalCost: 2688339.2704876685, - totalNPV: 1502827.6518804193, - costValues: { - "0": 0, - "1": 50744.70080000013, - "2": 56318.63142045774, - "3": 62048.161406028674, - "4": 67936.59349408025, - "5": 148987.29291171025, - "6": 80203.6884863138, - "7": 86589.27377512018, - "8": 93147.60821402141, - "9": 99882.31828600864, - "10": 221797.0987095596, - "11": 113895.71364727557, - "12": 121181.99793513953, - "13": 128659.85833271913, - "14": 136333.27479465678, - "15": 219206.30176382378, - "16": 152283.0694864667, - "17": 160567.78534974213, - "18": 169064.73524198192, - "19": 177778.28493609733, - "20": 301712.88149646495, - "-4": 0, - "-3": 0, - "-2": 0, - "-1": 40000, - }, - }, - ], - }, - input: { - countryCode: "IND", - activity: "Conservation", - ecosystem: "Mangrove", - projectName: "My custom project", - projectSizeHa: 1000, - initialCarbonPriceAssumption: 1000, - carbonRevenuesToCover: "Opex", - parameters: { - lossRateUsed: "National average", - emissionFactorUsed: "Tier 2 - Country-specific emission factor", - ecosystem: "Mangrove", - }, - costInputs: { - feasibilityAnalysis: 50000, - conservationPlanningAndAdmin: 166766.66666666666, - dataCollectionAndFieldCost: 26666.666666666668, - communityRepresentation: 71183.33333333333, - blueCarbonProjectPlanning: 100000, - establishingCarbonRights: 46666.666666666664, - financingCost: 0.05, - validation: 50000, - implementationLaborHybrid: null, - implementationLabor: 100, - monitoring: 15000, - maintenance: 0.0833, - carbonStandardFees: 0.2, - communityBenefitSharingFund: 0.5, - baselineReassessment: 40000, - mrv: 75000, - longTermProjectOperatingCost: 26400, - }, - assumptions: { - verificationFrequency: 5, - baselineReassessmentFrequency: 10, - discountRate: 0.04, - restorationRate: 250, - carbonPriceIncrease: 0.015, - buffer: 0.2, - projectLength: 20, - }, - }, - }, -}; - -export default mockResponse; diff --git a/client/src/containers/auth/dialog/index.tsx b/client/src/containers/auth/dialog/index.tsx index de7aa93d..55d6476c 100644 --- a/client/src/containers/auth/dialog/index.tsx +++ b/client/src/containers/auth/dialog/index.tsx @@ -7,6 +7,7 @@ import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, + DialogDescription, DialogHeader, DialogTitle, DialogTrigger, @@ -31,6 +32,7 @@ const AuthDialog: FC = ({ dialogTrigger, onSignIn }) => { Sign in + {showSignin ? : } diff --git a/client/src/containers/projects/custom-project/header/index.tsx b/client/src/containers/projects/custom-project/header/index.tsx index 357e4448..f3e50b15 100644 --- a/client/src/containers/projects/custom-project/header/index.tsx +++ b/client/src/containers/projects/custom-project/header/index.tsx @@ -1,12 +1,15 @@ -import { FC } from "react"; +import { FC, useCallback } from "react"; + +import { useRouter } from "next/navigation"; import { CustomProject as CustomProjectEntity } from "@shared/entities/custom-project.entity"; import { useAtom } from "jotai"; import { LayoutListIcon } from "lucide-react"; -import { useSession } from "next-auth/react"; +import { Session } from "next-auth"; +import { getSession, useSession } from "next-auth/react"; import { client } from "@/lib/query-client"; -import { cn } from "@/lib/utils"; +import { cn, getAuthHeader } from "@/lib/utils"; import { projectsUIState } from "@/app/projects/store"; @@ -25,33 +28,43 @@ const CustomProjectHeader: FC = ({ data }) => { useAtom(projectsUIState); const { data: session } = useSession(); const { toast } = useToast(); - const handleSaveButtonClick = async () => { - try { - const { status, body } = - await client.customProjects.saveCustomProject.mutation({ - body: data, - extraHeaders: { - authorization: `Bearer ${session?.accessToken as string}`, - }, - }); + const router = useRouter(); + const SaveProject = useCallback( + async (arg: Session | null = session) => { + try { + const { status, body } = + await client.customProjects.saveCustomProject.mutation({ + body: data, + extraHeaders: { + ...getAuthHeader(arg?.accessToken as string), + }, + }); - if (status === 201) { - toast({ description: "Project updated successfully." }); - } + if (status === 201) { + toast({ description: "Project updated successfully." }); + router.push("/my-projects"); + } - if (body?.errors) { + if (body?.errors) { + toast({ + variant: "destructive", + description: body.errors[0].title, + }); + } + } catch (e) { toast({ variant: "destructive", - description: body.errors[0].title, + description: "Something went wrong saving the project", }); } - } catch (e) { - toast({ - variant: "destructive", - description: "Something went wrong saving the project", - }); - } - }; + }, + [session, data, toast, router], + ); + const handleOnSignIn = useCallback(async () => { + // session is undefined when onSignIn callback is called + const session = await getSession(); + SaveProject(session); + }, [SaveProject]); return ( @@ -73,13 +86,13 @@ const CustomProjectHeader: FC = ({ data }) => { {session ? ( - ) : ( Save project} - onSignIn={handleSaveButtonClick} + onSignIn={handleOnSignIn} /> )} diff --git a/client/src/containers/projects/custom-project/index.tsx b/client/src/containers/projects/custom-project/index.tsx index 2b26890b..41b6a940 100644 --- a/client/src/containers/projects/custom-project/index.tsx +++ b/client/src/containers/projects/custom-project/index.tsx @@ -2,6 +2,7 @@ import { FC, useMemo } from "react"; import { ConservationProjectOutput } from "@shared/dtos/custom-projects/custom-project-output.dto"; +import { CustomProject as CustomProjectEntity } from "@shared/entities/custom-project.entity"; import { motion } from "framer-motion"; import { useAtomValue } from "jotai"; @@ -34,11 +35,21 @@ interface CustomProjectProps { } const CustomProject: FC = ({ id }) => { + const data = useGetCustomProject(id); + + // TODO: Maybe add a spinner/skeleton? + if (!data) return null; + + return ; +}; + +const CustomProjectView: FC<{ + data: InstanceType; +}> = ({ data }) => { const [{ costRangeSelector }] = useCustomProjectFilters(); const costDetailsRangeSelector = useAtomValue(costDetailsFilterAtom); const { projectSummaryOpen } = useAtomValue(projectsUIState); const { open: navOpen } = useSidebar(); - const data = useGetCustomProject(id); // TODO: should be replaced with correct type when available; const output = data.output as ConservationProjectOutput; const projectCostProps = output.totalProjectCost[costRangeSelector]; diff --git a/client/src/hooks/use-get-custom-project.ts b/client/src/hooks/use-get-custom-project.ts index e445984d..cd75a1e4 100644 --- a/client/src/hooks/use-get-custom-project.ts +++ b/client/src/hooks/use-get-custom-project.ts @@ -41,5 +41,5 @@ export function useGetCustomProject(id?: string) { } }, [queryCache, pathname, router]); - return data as CustomProjectEntity; + return data; }